2013/03/22

tputのサンプル

tputでterminalサイズいっぱいにランダムに背景色を設定し続けるサンプル。わさおKPFのLTでちらっと見せてくれたのを思い出して真似してみた。あれはコレ↓を使ったやつなんだろうけども。

tput colsとlinesでterminalのサイズを取得し、ランダムな座標にランダムな背景色を指定し続ける。

こんな感じ。

スクリプトはGauche。tputの実行はgauche.processモジュールのrun-process、tputの実行結果取得はprocess-output->stringで。

下のはtput colsでterminalの幅を取得してランダムな背景色を指定しつつ行末まで行ったらreturnして行頭に戻ってループ。

#!/usr/local/bin/gosh
(use gauche.process)
(use math.mt-random)
(define (main args)
(define rand
(let1 m (make <mersenne-twister> :seed (sys-time))
(^n (mt-random-integer m n))))
(define (get-tput-val . args)
(process-output->string `(tput ,@args)))
(define (tput . args)
(run-process `(tput ,@args)))
(define (make-random-color)
#`",(string #\\escape)[4,(+ (rand 8) 1)m ,(string #\\escape)[0m")
(let ((cols (x->integer (get-tput-val 'cols)))
(lines (x->integer (get-tput-val 'lines))))
(dynamic-wind
(^ _ (tput 'civis)(tput 'clear))
(^ _ (let1 size (iota (* cols lines))
(display (apply string-append (map (^_ (make-random-color)) size)))
(while #t
(for-each (^_ (tput 'cup (rand lines)(rand cols))
(display (make-random-color))
(sys-nanosleep 1000000)
(flush))
size))))
(^ _ (tput 'cnorm)))))
#!/usr/local/bin/gosh
(use gauche.process)
(use math.mt-random)
(define (main args)
(define rand
(let1 m (make <mersenne-twister> :seed (sys-time))
(^n (mt-random-integer m n))))
(define (get-tput-val . args)
(process-output->string `(tput ,@args)))
(define (tput . args)
(run-process `(tput ,@args)))
(define (make-random-color)
#`",(string #\\escape)[4,(+ (rand 8) 1)m ,(string #\\escape)[0m")
(let1 cols (x->integer (get-tput-val 'cols))
(while #t
(for-each (^_ (display (make-random-color))
(sys-nanosleep 10000000)
(flush))
(iota cols))
(display "\r"))))

まあ、両方ともあんまり意味はない。ヒマだったんだ。

0 件のコメント:

コメントを投稿