Source

Trite Snippets / tput / raw_sample.py

#!/usr/bin/env python
'''Example of how to do some very simple "tput"-like operations using the
   Python curses module (without calling initscr() and mangling the current
   display contents).
'''

if __name__ == '__main__':
    import sys, curses
    from time import sleep

    if not sys.stdin.isatty():
        print >> sys.stderr, "No way, dude!"
        sys.exit(1)

    spinner = '-/-\\-|'
    curses.setupterm()  # call this in lieu of initscr()
    cols = curses.tigetnum('cols') 
    rows = curses.tigetnum('lines') 
    red = curses.tparm(curses.tigetstr('setaf'),curses.COLOR_RED)
    treset = curses.tparm(curses.tigetstr('op'))

    print "::%sx%s:: " % (cols,rows), 
    sys.stdout.write(red)
    for i in range(20):
        sys.stdout.write(spinner[i%len(spinner)])
        sleep(0.25)
        sys.stdout.write('\b')
        sys.stdout.flush()
    sys.stdout.write(treset + " ")
    print ""