Re printing on same line.
James T. Dennis
jadestar at idiom.com
Tue Jun 19 20:18:41 EDT 2007
Robert Bauck Hamar <roberth+news at ifi.uio.no> wrote:
> Jerry Hill wrote:
>> On 6/15/07, HMS Surprise <john at datavoiceint.com> wrote:
>>> 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
More information about the Python-list
mailing list