471,339 Members | 1,239 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,339 software developers and data experts.

Re printing on same line.

I want to print a count down timer on the same line. I tried

print '\r', timeLeft,
which just appends to the same line.
thanx,

jh

Jun 15 '07 #1
5 4970
En Fri, 15 Jun 2007 12:21:01 -0300, HMS Surprise <jo**@datavoiceint.com>
escribió:
I want to print a count down timer on the same line. I tried

print '\r', timeLeft,
which just appends to the same line.
It works OK for me. Don't you have any other print statement ruining the
output?

--
Gabriel Genellina

Jun 15 '07 #2
>... ruining the
output?
? None.

Jun 15 '07 #3
On 6/15/07, HMS Surprise <jo**@datavoiceint.comwrote:
I want to print a count down timer on the same line. I tried

print '\r', timeLeft,

which just appends to the same line.
Sounds to me like whatever you're printing to doesn't do what you
expect when it encounters a carriage return (\r). Is your program
running in a terminal? Both the windows cmd.exe shell and bash under
linux seem to do the right thing when encountering a '\r'.

On the other hand, running the same code in IDLE doesn't work right,
presumably because the text window doesn't really know what to do with
a bare carriage return.

--
Jerry
Jun 15 '07 #4
Jerry Hill wrote:
On 6/15/07, HMS Surprise <jo**@datavoiceint.comwrote:
>I want to print a count down timer on the same line. I tried

print '\r', timeLeft,

which just appends to the same line.

Sounds to me like whatever you're printing to doesn't do what you
expect when it encounters a carriage return (\r). Is your program
running in a terminal? Both the windows cmd.exe shell and bash under
linux seem to do the right thing when encountering a '\r'.
Actually, bash has nothing to do with how the terminal handles \r. The job
of the shell (bash, ksh, csh, sh ...) is to execute your script when you
type its name.

Outputting a \r might or might not move the cursor to the beginning of the
line. It's completely system specific, and even on the same OS, it depends
on the capabilities of the actual terminal the programs run on, and on some
terminal emulators it might depend on configuration settings.

If you need to explore the capabilities of the terminal, curses will be a
good place to start.

--
rbh
Jun 15 '07 #5
Robert Bauck Hamar <ro**********@ifi.uio.nowrote:
Jerry Hill wrote:
>On 6/15/07, HMS Surprise <jo**@datavoiceint.comwrote:
>>I want to print a count down timer on the same line. I tried
>> print '\r', timeLeft,
>>which just appends to the same line.
>Sounds to me like whatever you're printing to doesn't do what you
expect when it encounters a carriage return (\r). Is your program
running in a terminal? Both the windows cmd.exe shell and bash under
linux seem to do the right thing when encountering a '\r'.
Actually, bash has nothing to do with how the terminal handles \r. The job
of the shell (bash, ksh, csh, sh ...) is to execute your script when you
type its name.
Outputting a \r might or might not move the cursor to the beginning of the
line. It's completely system specific, and even on the same OS, it depends
on the capabilities of the actual terminal the programs run on, and on some
terminal emulators it might depend on configuration settings.
If you need to explore the capabilities of the terminal, curses will be a
good place to start.
--
rbh
Sometimes you don't want to full curses mode switch and other baggage.
You can use the 'tput' utility to get the terminal escape sequences
for various cursor movements for your terminal. Read the tput man
page for some information, and the terminfo(5) man page for more.

In particular you can do things like (from a shell prompt):

tput sc; tput cup 0 30;echo Front and Center; tput rc

... which saves the current cursor location (sc), executes a cursor
position to the first line, 30th column (cup 0 30), writes some text
there, and then restores the cursor location (rc).

The trick, in Python, is that you can read these sequences into
your program via popen() calls and then print them wherever you
want to use them. You can even set colors (foreground and background),
and text attributes (dim, bright, blinking, underline) and perform
various other curses like applications without actually doing the
curses mode changes if you're really a bit masochistic.

However, for the original poster's purposes the easiest option would
probably be to print something like:

print chr(0x08) * 80, "new line stuff" + " " * 66

(Print lots of backspaces followed by whatever information you wanted to
over-write the line with and padding with enough spaces to clear any
other stuff you wanted to over-write).

Yes, this is sloppy. Yes, it might not work on some terminals or under
some terminal settings. However, it should work under most circumstances
on most terminals --- including some which wouldn't support the curses
module. You can improve it somewhat by keeping track of how many
characters you've printed to the current (last) line and dynamically
printing the precise number of backspaces and padding spaces that are
needed.

--
Jim Dennis,
Starshine: Signed, Sealed, Delivered

Jun 20 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Jody Gelowitz | last post: by
3 posts views Thread by Grim Reaper | last post: by
5 posts views Thread by Patrick De Ridder | last post: by
4 posts views Thread by Rob T | last post: by
6 posts views Thread by Bill | last post: by
4 posts views Thread by Lucas Ponzo | last post: by
2 posts views Thread by Brad Pears | last post: by
18 posts views Thread by Brett | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.