"Modulok" <modu...@gmail.com> wrote

Assume I'm working in a command shell on a terminal. Something like
tcsh on xterm, for example. I have a program which does *something*.
Let's say it counts down from 10. How do I print a value, and then
erase that value, replacing it with another value?

This is one of those things that sounds like it should be easy but is in
fact deceptively difficult to get right!

It all depends on your terminal, and there are dozens of different terminal
types and settings. Thats why Unix has a terminfo (or in some Unices
termcap - even they can't agree! :-) library to define the different terminal
types and try to bring some sanity to proceedings.But you need to
learn the basic terminal control codes.

In this case you need the backspace code which is Ctrl-H or chr(8).

Try this

print "spam" + "chr(8)*2+"ear"

If your terminal recognises chr(8) as backspace you should
see "spear". If it doesn't you'll see someting like:

spam^h^hear

The only foolproof way to do this kind of thing is to use a library like
curses which allows you to positioon the cursor and overwrite whatever
was there. But curses is non-trivial to use - not ridiculously hard, but
non trivial.

You can get a basic intro to curses in the event-driven topic
of my tutorial.

BTW there is quite a good writeup on terminfo here:
http://tldp.org/HOWTO/Text-Terminal-HOWTO-16.html
and, as usual, Wikipedia has a fair writeup.

--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/


_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to