2012年4月3日火曜日

RacketのGuiでテキストに色をつける

RacketのGUIのテキストに色をつけたりアンダーラインを引いたりします。
"Racket"が赤で表示され、"world"にはアンダーラインが引かれます。
#lang racket
(require racket/gui/base)

(define frame
(new frame%
[label "Example"]
[width 300]
[height 300]))

(define ecanvas
(new editor-canvas% [parent frame]))

(define txt-hello (new text%))

(send txt-hello insert "hello ")
(send txt-hello change-style
(send (make-object style-delta%)
set-delta-foreground "red"))
(send txt-hello insert "Racket ")
(send txt-hello change-style
(send (make-object style-delta%)
set-delta 'change-underline #t))
(send txt-hello insert "world")

(send ecanvas set-editor txt-hello)

(send frame show #t)

外部プロセスの標準出力を取得する


1 Racket

 1:  ;; racket
2: #!lang racket
3:
4: (define (system->string-list cmd . args)
5: (match-define
6: (list in out pid err procedure)
7: (apply process* cmd args))
8: (procedure 'wait)
9: (unless (eq? (procedure 'status) 'done-ok)
10: (raise 'exit-failure))
11: (port->lines in))
12:
13: ;; (sytem->string-list "/bin/ls")

2 Chicken

1:  ;; Chicken
2: (require 'posix)
3:
4: (call-with-input-pipe "ls"
5: (lambda (p)
6: (read-lines p)))

3 Gauche

1:  ;; Gauche
2: (use gauche.process)
3: (process-output->string-list '(ls))

4 Guile

1:  ;; Guile
2: (use-modules (ice-9 popen))
3: (use-modules (ice-9 rdelim))
4:
5: (let ((p (open-input-pipe "ls")))
6: (let ((lines (read-delimited "" p)))
7: (close-pipe p)
8: (string-split lines #\newline)))

5 Ruby

1:  `ls`.split

6 Python

1:  import subprocess
2: p = subprocess.Popen("ls", stdin=subprocess.PIPE, stdout=subprocess.PIPE,
3: close_fds=True)
4:
5: p.stdout.readlines()