From: Carlos Date: 2006-11-08T02:57:57+09:00 Subject: Re: How to make a cycling counter from commandline? Tim Pease wrote: > On 11/7/06, darenbell@gmail.com wrote: > >> Hi, I'm looking for a way to implement this idea: >> >> >> ------------------------------------- >> count=1 >> total=15 >> >> until count>total >> print "Record #{count} of #{total} processed." >> count+=1 >> end >> ------------------------------------- >> >> > > $ cat tmp.rb > > STDOUT.sync = true > > total = ARGV[0] || 15 > count = 1 > > until count>total > print "\rRecord #{count} of #{total} processed" > count += 1 > sleep 0.25 > end > > print "\n" > > > That should do you what you want. The secret is the carriage return > "\r" at the beginning of the print string. That will bring the cursor > back to the beginning of the line and then overwrite anything > currently on that line. From my BBS years I remember adding... mmm... �"\033[K"? at the end of the string when I did this kind of overwriting. That ANSI code (if I remembered it correctly) clears everything until the end of line --useful in case the new string is shorter than the overwritten one. (This doesn't happen in this case, but maybe the OP wants to print "#{total} record processed" afterwards.) Greetings. --