Forum: Ruby How to make a cycling counter from commandline?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
05337a184356a88384c7afffa907ae07?d=identicon&s=25 darenbell@gmail.com (Guest)
on 2006-11-07 17:02
(Received via mailing list)
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
-------------------------------------


I want this to print out in a Linux shell.  However, I'd prefer it if
there was a way to have this counter increment without printing a new
line.  As in, from the user's perspective the 'count' would just
increment until it matched the total... all taking place on the same
line.

I know I've seen this done, but can Ruby do this?
97550977337c9f0a0e1a9553e55bfaa0?d=identicon&s=25 Jan Svitok (Guest)
on 2006-11-07 17:16
(Received via mailing list)
On 11/7/06, darenbell@gmail.com <darenbell@gmail.com> wrote:
> end
> -------------------------------------
>
>
> I want this to print out in a Linux shell.  However, I'd prefer it if
> there was a way to have this counter increment without printing a new
> line.  As in, from the user's perspective the 'count' would just
> increment until it matched the total... all taking place on the same
> line.
>
> I know I've seen this done, but can Ruby do this?

Search the archives for count or counter. It was discussed last week or
so.
4d5b5dd4e263d780a5dfe7ac8b8ac98c?d=identicon&s=25 Tim Pease (Guest)
on 2006-11-07 17:18
(Received via mailing list)
On 11/7/06, darenbell@gmail.com <darenbell@gmail.com> wrote:
> 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.

Blessings,
TwP
4d5b5dd4e263d780a5dfe7ac8b8ac98c?d=identicon&s=25 Tim Pease (Guest)
on 2006-11-07 17:20
(Received via mailing list)
On 11/7/06, Tim Pease <tim.pease@gmail.com> wrote:
> >   count+=1
> > end
> > -------------------------------------
> >
> >
>
> $ cat tmp.rb
>
> STDOUT.sync = true
>
> total = ARGV[0] || 15

total = ARGV[0] || 15
total = Integer(total)    # need to turn any passed in arguments into an
integer
Cb48ca5059faf7409a5ab3745a964696?d=identicon&s=25 unknown (Guest)
on 2006-11-07 18:39
(Received via mailing list)
On Wed, 8 Nov 2006, Tim Pease wrote:

> sleep 0.25
> Blessings,
> TwP

man, you learn something every day!  that's pretty sweet.

-a
C2cd72c24873556e278b44b5b3c7ef33?d=identicon&s=25 Carlos (Guest)
on 2006-11-07 18:59
(Received via mailing list)
Tim Pease wrote:

>>   print "Record #{count} of #{total} processed."
> total = ARGV[0] || 15
>
> 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.
4d5b5dd4e263d780a5dfe7ac8b8ac98c?d=identicon&s=25 Tim Pease (Guest)
on 2006-11-07 19:09
(Received via mailing list)
On 11/7/06, Carlos <angus@quovadis.com.ar> wrote:
>
>  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.)
>

Yeah, that was the fatal flaw in my little post there. That is one
very cool ANSI code though.  I've used the color codes, but did not
know about that one :)

I just tried it out, and that is the correct code :)

clear = "\e[K"
print "a very long line of text that is longer than the next line"
sleep 1
print "\rshort line of text#{clear}"
sleep 1
print "\n"

By the way "\e" == "\033"

Blessings,
TwP
430ea1cba106cc65b7687d66e9df4f06?d=identicon&s=25 David Vallner (Guest)
on 2006-11-08 00:16
(Received via mailing list)
Tim Pease wrote:
> 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.
>

Smells unportable between Linux, Windows, and Mac Classic. Then again, I
don't know what approach Ruby takes to newline-handling on non-Unix
platforms.

Ah, the good old days of high-school Turbo Pascal, when gotoxy() Just
Worked (tm). Nary a control character or escape sequence in sight.

David Vallner
0fec0d4e1f4cbcfd4b37af52e50e347f?d=identicon&s=25 Gabriele Marrone (Guest)
on 2006-11-08 00:36
(Received via mailing list)
Il giorno 08/nov/06, alle ore 00:15, David Vallner ha scritto:

> Smells unportable between Linux, Windows, and Mac Classic. Then
> again, I
> don't know what approach Ruby takes to newline-handling on non-Unix
> platforms.

A portable way (but maybe overkill for this) is by using the ncurses
library.
I never tried ncurses-ruby, though.

> Ah, the good old days of high-school Turbo Pascal, when gotoxy() Just
> Worked (tm). Nary a control character or escape sequence in sight.

ncurses also has gotoxy :)
4d5b5dd4e263d780a5dfe7ac8b8ac98c?d=identicon&s=25 Tim Pease (Guest)
on 2006-11-08 03:25
(Received via mailing list)
On 11/7/06, David Vallner <david@vallner.net> wrote:
>
"\r" is completely portable.  win32 platforms use a "\r\n" for the end
of a line, and *NIX uses just a single "\n" -- hence the dos2unix
utility.

Now, the ANSI control character to clear to the end of a line
(mentioned earlier in this thread) is definitely not portable to the
win32 cmd shell.

TwP
6087a044557d6b59ab52e7dd20f94da8?d=identicon&s=25 Peña, Botp (Guest)
on 2006-11-08 04:39
(Received via mailing list)
David Vallner [mailto:david@vallner.net]
# Tim Pease wrote:
# > 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.
#
# Smells unportable between Linux, Windows, and Mac Classic.
# Then again, I
# don't know what approach Ruby takes to newline-handling on non-Unix
# platforms.

fyi, code posted by TwP runs on linux and windows.

kind regards -botp
6087a044557d6b59ab52e7dd20f94da8?d=identicon&s=25 Peña, Botp (Guest)
on 2006-11-08 04:53
(Received via mailing list)
Tim Pease [mailto:tim.pease@gmail.com]
# I just tried it out, and that is the correct code :)
# clear = "\e[K"
# print "a very long line of text that is longer than the next line"
# sleep 1
# print "\rshort line of text#{clear}"
# sleep 1
# print "\n"

i just do something like

MAX=foo_integer||80
CR="\r"
s=foo_string[0..MAX].ljust(MAX)  #or rjust or center

print CR+s

kind regards -botp
This topic is locked and can not be replied to.