From: Rob Biedenharn Date: 2008-11-23T10:52:11+09:00 Subject: Re: Printing right arrow vs. a sane progressbar library On Nov 22, 2008, at 8:18 PM, Shot (Piotr Szotkowski) wrote: > I want to track the progress of a set of (forkoff’d) processes in > a simple way. What I have in mind is a line of dots equal in length > to the number of processes, and once a process number X finishes, > it prints \r, then advances X characters to the right and prints o > – so after three of eight processes finish, it could look > like ..o..oo. > > The closest I got is the below (in one line, so easily pastable to > IRB): > > print '.' * 25; (0...25).sort_by{rand}.each { |n| print "\r"; print > "\c[[C" * n; print 'o'; sleep 0.5 }; print "\n" > > It seems to work in IRB and in a script (after $stdout.sync = true), > unless the number of processes is larger than the terminal window’s > width. > > Is this a sane method of doing what I want to do, or does > it just ‘happens to work’ in GNOME Terminal on my Ubuntu? > > Is there a saner way to advance by a char than printing the right > arrow? status='.'*25; print status; (0...25).sort_by{rand}.each{|n| status[n]='o'; print "\r#{status}"; sleep(0.1+rand/10.0)};puts While it still has trouble with more values that screen columns, it should be a bit better with race conditions. You could, perhaps, add a clear-to-EOL sequence, too. -Rob > > > On a similar note: Is there a simple progressbar library for Ruby > (ideally if itcan be somehow used with forkoff’d processes, but I’m > aware it’d require some kind of passing of state to the outside of the > block)? > > Is there anything else than Ruby/ProgressBar¹ (say, in a gem…)? > > ¹ http://0xcc.net/ruby-progressbar/ > > (I’m aware the above is prone to a race condition if two processes > finish at the same time and the second one of them prints its \r > before the first one prints its o, but let’s disregard this for now.) > > -- Shot > -- > If I was to use Bugzilla when I was a tester, I'd quit > and become a gardener. -- a Syllable OS developer Rob Biedenharn http://agileconsultingllc.com Rob@AgileConsultingLLC.com