Skip to content

Instantly share code, notes, and snippets.

@valvallow
Created November 15, 2011 15:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save valvallow/1367299 to your computer and use it in GitHub Desktop.
Save valvallow/1367299 to your computer and use it in GitHub Desktop.
progress-timer.scm
#!/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)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment