Rob B.:
On Nov 23, 2008, at 12:30 PM, Shot (Piotr S.) wrote:
A very good idea, but unfortunately it doesn’t work in a forkoff’d
block; every block gets its own copy of the original status and so
generates a line with 24 dots and one o […]
Hence my need for a right arrow, or for printing a ‘transparent’
character that doesn’t overwrite what’s already under the cursor.
I took a quick look at the code for forkoff and unless you’re
increasing the processes (or just looking for one that is “slow”),
this doesn’t seem to help much.
Erm, I’m not sure what doesn’t seem to help much.
Using the right
arrow trick works as expected (as opposed to editing a string that’s
unfortunately not the same object across the processes):
shot@devielle:~$ irb -rubygems
require ‘forkoff’
=> true
print ‘.’ * 25; (0…25).sort_by{rand}.forkoff { |n| print “\r”; print “\c[[C” * n; print ‘o’; sleep 0.5 }; print “\n”
ooooooooooooooooooooooooo
=> nil
(Although it’s more prone to race conditions than your solutions below.)
In my actual case, I want a general mechanism that runs parallel
processes in some way (forkoff for now, maybe DRb in the future)
and report back some kind of a visual information on how much is
being done over time.
As I assume each of such parallel runs won’t be able to access the
actual objects outside the forked-off block (only copies of them),
I believe I can’t employ a simple, tick-based progressbar; the above
was my idea to side-step the need of communication with the parent
(although I could perhaps make the code that collects¹ the returned
values trigger/tick the progressbar as soon as a result comes in…).
¹ forkoff has this nice feature that it handles the collecting of the
returned values transparently, so I simply don’t have any code that
collects the results at the moment…
That sequence and other ANSI control sequences can be found via the
web, for example, at: ANSI escape code - Wikipedia
Ah, thank you! I started this quest by recalling how \b and \r work,
which led me to the ASCII control characters; I totally forgot to
look at ANSI stuff, just recalled I can come up with the sequence
for the right arrow by cat
ing stdin to a file and then looking
at it. I should’ve thought ‘ANSI!’ when I saw ^[[C. 
Perhaps a sequence of:
“[[s[[#{n+1}Go[[u”
if your terminal supports the save/restore cursor position
Or manually go back to the “end” of the line each time:
“\033[#{n+1}Go\033[#{25+1}G”
Actually, a simple “\033[#{n+1}Go” works well enough; thanks! The trick
with going to the end of the line each time makes it easier on the eyes
if the cursor is visible, so I’ll most probably end up using it. Thanks
again!
(And just before sending this email I found out that “\033[?25l” makes
the cursor disappear, and so seems to be the cleanest solution without
any output-related race conditions whatsoever.) 
– Shot