2011/11/16

最近の雑多なコマンド(gauche)

某所で見たTwitterのfollow関係を表示するスクリプトのコマンド版。
$ ./twitter-friendship valvallow hoge
#!/usr/local/bin/gosh
(use rfc.json)
(use rfc.http)
(use util.list) ; assoc-ref
(define (call-twitter-api api-uri username)
(receive (status header body)
(http-get "api.twitter.com"
(string-append api-uri username ".json"))
(when (equal? status "404")
(print #`",username not found.")
(exit 1))
(parse-json-string body)))
(define (get-followee-ids username)
(assoc-ref (call-twitter-api "/1/friends/ids/" username) "ids"))
(define (get-user-id username)
(assoc-ref (call-twitter-api "/1/users/show/" username) "id"))
(define (main args)
(let ((args (cdr args)))
(let ((username1 (if (null? args)
(symbol->string (read))
(car args)))
(username2 (if (or (null? args)
(null? (cdr args)))
(symbol->string (read))
(cadr args))))
(let ((uid1 (get-user-id username1))
(uid2 (get-user-id username2))
(ids1 (vector->list (get-followee-ids username1)))
(ids2 (vector->list (get-followee-ids username2))))
(let ((1->2 (find (pa$ = uid2) ids1))
(2->1 (find (pa$ = uid1) ids2)))
(print username1 (cond ((and 1->2 2->1) "<->")
(1->2 "->")
(2->1 "<-")
(else "|"))
username2))))))
プログレス付きのタイマーコマンド。結果はnotify-sendか何かに渡すということで。←変更が必要そう。←プログレスをエラーポートに出力するようにした。
$ ./progress-timer -t hoge -s 10s timer
$ notify-send `progress-timer -s 3s -t hoge fuga`
#!/usr/local/bin/gosh
(use srfi-13)
(use text.progress)
(use gauche.parseopt)
(define (usage)
(print "Usage: progress-timer [options ...] message")
(print " - t|title : default empty")
(print " - s|sleep : 3m = 3 minutes")
(print " 3s = 3 seconds")
(print " 3 = 3 milliseconds")
(print " defualt 0")
(print " - h|help : usage")
(exit 2))
(define (num-format cur max)
(format "~3d%" (round->exact (/. (* cur 100) max))))
(define (decompose-unit unit)
(let1 len (string-length unit)
(rxmatch-if (#/[0-9]+$/ unit)
(num)
(values (x->integer num) 'default)
(values (x->integer (string-take unit (- len 1)))
(string->symbol (string-drop unit (- len 1)))))))
(define (unit->millisecond unit)
(receive (num suffix)
(decompose-unit unit)
(* num (case suffix
((s) 1000)
((m)(* 1000 60))
(else 1)))))
(define (main args)
(let-args (cdr args)
((title "t|title=s" "timer")
(n "s|sleep=s" "0")
(h "h|help" => usage)
. rest)
(let ((p (make-text-progress-bar :header title
:header-width (+ (string-length title) 1)
:num-format num-format
:num-width 5
:max-value 100
:port (current-error-port)))
(interval (/ (unit->millisecond n) 100))
(message (and (not (null? rest))
(car rest))))
(dotimes (i 100)
(p 'inc 1)
(sys-nanosleep (* interval 1000000)))
(p 'finish)
(when message
(print message)))))


プログラミングGauche

0 件のコメント:

コメントを投稿