On Nov 22, 2008, at 8:18 PM, Shot (Piotr Szotkowski) wrote:

> I want to track the progress of a set of (forkoff=92d) 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
> =96 so after three of eight processes finish, it could look =20
> like ..o..oo.
>
> The closest I got is the below (in one line, so easily pastable to =20
> IRB):
>
> print '.' * 25; (0...25).sort_by{rand}.each { |n| print "\r"; print =20=

> "\c[[C" * n; print 'o'; sleep 0.5 }; print "\n"
>
> It seems to work in IRB and in a script (after $stdout.sync =3D true),
> unless the number of processes is larger than the terminal window=92s
> width.
>
> Is this a sane method of doing what I want to do, or does
> it just =91happens to work=92 in GNOME Terminal on my Ubuntu?
>
> Is there a saner way to advance by a char than printing the right =20
> arrow?


status=3D'.'*25; print status; (0...25).sort_by{rand}.each{|n| =20
status[n]=3D'o'; print "\r#{status}"; sleep(0.1+rand/10.0)};puts

While it still has trouble with more values that screen columns, it =20
should be a bit better with race conditions.  You could, perhaps, add =20=

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=92d processes, but I=92m
> aware it=92d require some kind of passing of state to the outside of =
the
> block)?
>
> Is there anything else than Ruby/ProgressBar=B9 (say, in a gem=85)?
>
> =B9 http://0xcc.net/ruby-progressbar/
>
> (I=92m 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=92s disregard this for =
now.)
>
> -- Shot
> --=20
> 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