cursor positioning

Jeff Epler jepler at unpythonic.net
Mon Jul 11 11:36:15 EDT 2005


Here's a simple module for doing progress reporting.  On systems without
curses, it simply uses "\r" to return the cursor to the first column.
On systems with curses, it also clears to the end of the line.  This
means that when the progress message gets shorter, there aren't droppings
left from the longer ones.

You have to take care that the progress message isn't wider than the
screen, but I don't know a nice way to *find* the width of the screen
that will work on windows and unix.  Hardcoding 80 ain't it either.

import sys

def progress(s):
    sys.stderr.write(s + CLEAR_EOL + "\r"); sys.stderr.flush()

try:
    import curses
except ImportError:
    CLEAR_EOL = ''
else:
    curses.setupterm()
    CLEAR_EOL = curses.tigetstr("el") or ''

def test():
    import time
    for j in range(2):
        for i in range(100):
            progress("Doing item %d, %d%%" % (i * 100, i))
            time.sleep(.01)
if __name__ == '__main__': test()
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20050711/14752f7c/attachment.sig>


More information about the Python-list mailing list