Terminal application with non-standard print

R

Rémi

Hello everyone,

I would like to do a Python application that prints data to stdout, but
not the common way. I do not want the lines to be printed after each
other, but the old lines to be replaced with the new ones, like wget
does it for example (when downloading a file you can see the percentage
increasing on a same line).

I looked into the curses module, but this seems adapted only to do a
whole application, and the terminal history is not visible anymore when
the application starts.

Any ideas?

Thanks,

Remi
 
G

Grant Edwards

I would like to do a Python application that prints data to stdout, but
not the common way. I do not want the lines to be printed after each
other, but the old lines to be replaced with the new ones, like wget
does it for example (when downloading a file you can see the percentage
increasing on a same line).

sys.stdout.write("Here's the first line")
time.sleep(1)
sys.stdout.write("\rAnd this line replaces it.")
 
R

Rémi

Thank you for your answer, but that does not work : the second line is
printed after the first one.
 
R

Rémi

My apologies, I did not run the Python script properly.
Thanks, that works great.

If I understand well, \r erases the last line. How about erasing the
previous lines?

For example when writing
sys.stdout.write("1\n2\n")
sys.stdout.write("\r3")
the "1" is still visible.
 
R

Rémi

My apologies, I did not run the lines properly.
Thanks, that works great now.

If I understand well, \r erases the last line. How about erasing the
previous lines?

For example when writing
sys.stdout.write("1\n2\n")
sys.stdout.write("\r3")
the "1" is still visible.
 
H

Hans Mulder

Grant said:
sys.stdout.write("Here's the first line")
time.sleep(1)
sys.stdout.write("\rAnd this line replaces it.")

That does not work on my system, because sys.stdout is line buffered.
This causes both strings to be written when sys.stdout is closed because
Python is shutting down.

This works better:

import sys, time

sys.stdout.write("Here's the first line")
sys.stdout.flush()
time.sleep(1)
sys.stdout.write("\rAnd this line replaces it.")
sys.stdout.flush()


Hope this helps,

-- HansM
 
D

Dennis Lee Bieber

If I understand well, \r erases the last line. How about erasing the
previous lines?
No... "\r" is a carriage-return (move to beginning of line). "\n" is
a line-feed (which on most systems is translated as a <CR><LF>, in order
to implement a "new line").

Full screen control requires having a terminal that understands
advanced control codes [many of which seem to have been based upon DEC
VT-52/VT-100 codes] (Which, unfortunately, the Window's command line
doesn't commonly do -- hence curses and various schemes mapping logical
operations to specific terminal types... After all, the most basic mode
is a terminal that supports: carriage return, line feed, and clear
screen; by using a library that maintains an "image" of the entire
screen, any operation that modifies a line other than the last or new
can be emulated by clear screen and write modified image text to screen)
 
G

Grant Edwards

That does not work on my system, because sys.stdout is line buffered.

That's correct of course.
This causes both strings to be written when sys.stdout is closed because
Python is shutting down.

This works better:

import sys, time

sys.stdout.write("Here's the first line")
sys.stdout.flush()
time.sleep(1)
sys.stdout.write("\rAnd this line replaces it.")
sys.stdout.flush()

Or you can tell Python to do unbuffered output:

#!/usr/bin/python -u
 
S

Sean DiZazzo

Hello everyone,

I would like to do a Python application that prints data to stdout, but
not the common way. I do not want the lines to be printed after each
other, but the old lines to be replaced with the new ones, like wget
does it for example (when downloading a file you can see the percentage
increasing on a same line).

I looked into the curses module, but this seems adapted only to do a
whole application, and the terminal history is not visible anymore when
the application starts.

Any ideas?

Thanks,

Remi

You might want to take a look at the readline module.

~Sean
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top