From: Rob Biedenharn Date: 2008-11-24T07:22:22+09:00 Subject: Re: Printing right arrow vs. a sane progressbar library On Nov 23, 2008, at 12:30 PM, Shot (Piotr Szotkowski) wrote: > Rob Biedenharn: >> 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. […] > >>> print '.' * 25; (0...25).sort_by{rand}.each { |n| print "\r"; >>> print "\c[[C" * n; print 'o'; sleep 0.5 }; print "\n" > >> status='.'*25; print status; (0...25).sort_by{rand}.each{|n| >> status[n]='o'; >> print "\r#{status}"; sleep(0.1+rand/10.0)};puts > > 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: > > shot@devielle:~$ irb -rubygems >>> require 'forkoff' > => true >>> status='.'*25; print status; (0...25).sort_by{rand}.forkoff{|n| >>> status[n]='o'; print "\r#{status}"; sleep(0.1+rand/10.0)};puts > ..................o...... > => nil > > 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. > > >> You could, perhaps, add a clear-to-EOL sequence, too. > > I’m sorry, but I’m a bit braindead today; where would I want to use > this? (And, when we’re at it, what’s the escape sentence for it?) That sequence and other ANSI control sequences can be found via the web, for example, at: http://en.wikipedia.org/wiki/ANSI_escape_code > Thanks a lot for your interest in this thread and your reply! > > -- Shot > -- > A distributed system is one in which I cannot get something done > because > a machine I've never heard of is down. -- Leslie > Lamport Perhaps a sequence of: "\[[s\[[#{n+1}Go\[[u" if your terminal supports the save/restore cursor position print '.' * 25; (0...25).sort_by{rand}.forkoff { |n| sleep(0.25+rand/ 10.0); print "\033[s\033[#{n+1}Go\033[u" }; print "\n" Or manually go back to the "end" of the line each time: "\033[#{n+1}Go\033[#{25+1}G" print '.' * 25; (0...25).sort_by{rand}.each { |n| sleep(0.2+rand/2.0); print "\033[#{n+1}Go\033[26G" }; print "\n" Perhaps get the status via some other mechanism in another process (but that's going to fly away from a "simple way" even faster). -Rob Rob Biedenharn http://agileconsultingllc.com Rob@AgileConsultingLLC.com