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()

Attachment: pgpzfL5bt0IvZ.pgp
Description: PGP signature

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to