Clojure Console Progress Bar

180 views
Skip to first unread message

Kasim

unread,
Dec 15, 2009, 7:22:21 PM12/15/09
to Clojure
I have following script to show the progress status in Console. But I
am having an issue where print only prints final string (after 100
times loop finished) not those in between thread sleeps but println
prints out all in between. I am pretty new to Clojure ( Lisp for the
matter) and have no idea why. Can someone point out what is the
problem?

(defn progress-string
[i]
(for [x (range 50)] (if (<= (/ i 2) x) " " "=")))

(defn show-progress-string
[t]
(dotimes [percent 100 ]
(do
(Thread/sleep t)
(println "\r" (str-join "" (seq (progress-string (inc
percent))))))))


Thanks

Stephen C. Gilardi

unread,
Dec 15, 2009, 9:23:20 PM12/15/09
to clo...@googlegroups.com
The output is kept in an output buffer until it's flushed (either because the buffer is full or by request). println includes a call to (flush) (if *flush-on-newline* is true, which is the default). In your case, you can use an explicit call to (flush) when you want to be sure the user has seen what's been printed so far.

--Steve

Kasim

unread,
Dec 16, 2009, 12:21:43 PM12/16/09
to Clojure
Thank you. Here is my final progress bar code for anyone interested:
(defn progress-string
[i]
(str-join "" (seq (for [x (range 50)] (if (<= (/ i 2) x) " "
"=")))))

(defn show-progress-string
[t]
(dotimes [percent 100 ]
(do
(Thread/sleep t)
(print "\r|" (progress-string (inc percent)) "|" (inc percent)
"% done" )
(flush))))

(show-progress-string 1200)

Just adding (flush) solved it.
>  smime.p7s
> 3KViewDownload
Reply all
Reply to author
Forward
0 new messages