OT: Status Display

Hello,

I am very new to ruby, so please dont hit me for my (off-topic) question. :wink:

I am about to write a program, which performs various database actions
(mysql). During these actions, I want to display the current status.

Now my Q: Is it possible with ruby to diplay the percentage of the
current status always on the same position on the screen instead of
seperated by newlines or something like that?

This program will be executed on Linux systems as a commandlinetool.

So what I want is just a “refresh” of the status display, not something
like:

1%
3%
10%

Thank you very much in advance.

···


Juergen Lang j-l@gmx.at www.getinspired.at

Hey,

Juergen Lang wrote:

Now my Q: Is it possible with ruby to diplay the percentage of the
current status always on the same position on the screen instead of
seperated by newlines or something like that?

Try this in IRB:

100.times { |x| print "#{x}%\r"; sleep 1 }

Hope that helps,

···


Laurent

Saluton!

  • Juergen Lang; 2003-06-30, 12:44 UTC:

Now my Q: Is it possible with ruby to diplay the percentage of the
current status always on the same position on the screen instead of
seperated by newlines or something like that?

A very portable way is this:

rubout = 8.chr * 4
0.upto(100) {|i|
print rubout unless i == 0
printf “%3d%%”, i
$stdout.flush
sleep 1
}

rubout is backspace characters that remove the percentage text. At
first run it is not written. Afterwards it removes the text already
visible (you could output a dummy 0% so that the if statement is not
needed). printf does output a three digit percentage (see man 3
printf for details on printf). The most important statement is the
flush statement.

On Unix the output only takes place if you output a “\n” character -
unless you manually flush the buffer.

I use this kind of output for my mail downloader and it works
flawlessly with one execption: If you press enter the output
continues in the following line but that problem is present with many
programs so I don’t care.

Gis,

Josef ‘Jupp’ Schugt

···


Someone even submitted a fingerprint for Debian Linux running on the
Microsoft Xbox. You have to love that irony :).
– Fyodor on nmap-hackers@insecure.org

Someone pointed out to me that in some environments
(perhaps in emacs, for example?) a backspace will
actually be printed as a ^H.

I’ve always preferred just to do a single carriage
return at the end, and ensure that my output strings
are all the same length (formatted output) so as not
to leave any “trash” when a short line is printed
over a longer one…

Hal

···

----- Original Message -----
From: “Josef ‘Jupp’ Schugt” jupp@gmx.de
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Monday, June 30, 2003 3:39 PM
Subject: Re: OT: Status Display

Saluton!

  • Juergen Lang; 2003-06-30, 12:44 UTC:

Now my Q: Is it possible with ruby to diplay the percentage of the
current status always on the same position on the screen instead of
seperated by newlines or something like that?

A very portable way is this:

rubout = 8.chr * 4
0.upto(100) {|i|
print rubout unless i == 0
printf “%3d%%”, i
$stdout.flush
sleep 1
}

rubout is backspace characters that remove the percentage text. At
first run it is not written. Afterwards it removes the text already
visible (you could output a dummy 0% so that the if statement is not
needed). printf does output a three digit percentage (see man 3
printf for details on printf). The most important statement is the
flush statement.


Hal Fulton
hal9000@hypermetrics.com

He[a]llo Josef!

A very portable way is this:

rubout = 8.chr * 4
0.upto(100) {|i|
print rubout unless i == 0
printf “%3d%%”, i
$stdout.flush
sleep 1
}

That’s what I was looking for. Works great for me.

Thank you and thanks to the others who squeezed their brain for my problem.

···


Juergen Lang j-l@gmx.at www.getinspired.at

More portable:

rubout = “\b” * 4

\b = backspace; maybe somebody runs Ruby on an old IBM mainframe or
something which doesn’t use ASCII :slight_smile:

But if you’re just trying to move the cursor back to the start of the line,
“\r” is easier as you don’t have to count characters.

Regards,

Brian.

···

On Tue, Jul 01, 2003 at 05:39:51AM +0900, Josef ‘Jupp’ Schugt wrote:

A very portable way is this:

rubout = 8.chr * 4

Saluton!

  • Brian Candler; 2003-07-01, 12:04 UTC:

A very portable way is this:

rubout = 8.chr * 4

More portable:

rubout = “\b” * 4

Thank you, changed in my program :->

But if you’re just trying to move the cursor back to the start of
the line, “\r” is easier as you don’t have to count characters.

I am not sure if this works on a Mac. Besides that: I do not update
the whole line.

Gis,

Josef ‘Jupp’ Schugt

···

On Tue, Jul 01, 2003 at 05:39:51AM +0900, Josef ‘Jupp’ Schugt wrote:

Someone even submitted a fingerprint for Debian Linux running on the
Microsoft Xbox. You have to love that irony :).
– Fyodor on nmap-hackers@insecure.org