From a6c08bff8480640f41c5f70e3617498cb1d5108f Mon Sep 17 00:00:00 2001 From: C. Scott Ananian Date: Thu, 13 Dec 2007 20:28:03 +0000 Subject: Add additional escape sequences to pippy.console library. Also tweaked size() so that it returns a pair of ints; and made 'thanks' match. --- (limited to 'library') diff --git a/library/pippy/console.py b/library/pippy/console.py index 3500093..a83b756 100644 --- a/library/pippy/console.py +++ b/library/pippy/console.py @@ -1,57 +1,90 @@ """Console helpers for pippy.""" -import sys, commands +import sys def clear(): """Clear screen on console.""" # magic escape sequence sys.stdout.write('\x1B[H\x1B[J') def size(): - """Return the number of rows/columns in the current VTE widget.""" - return commands.getoutput("stty size") + """Return the number of rows/columns in the current terminal widget.""" + # xterm magic! see http://rtfm.etla.org/xterm/ctlseq.html + import os, tty, termios + fd = os.open('/dev/tty', os.O_RDWR|os.O_APPEND) + def read_to_delimit(delimit): + buf = [] + while True: + c = os.read(fd, 1) + if c==delimit: break + buf.append(c) + return ''.join(buf) + oldattr = termios.tcgetattr(fd) # make sure we can restore tty state + tty.setraw(fd, termios.TCSANOW) # set to raw mode. + os.write(fd, '\x1B[18t') # write the 'query screen size' command + read_to_delimit('\x1b') # parse response. + read_to_delimit('[') + rows = int(read_to_delimit(';')) + cols = int(read_to_delimit('t')) + termios.tcsetattr(fd, termios.TCSANOW, oldattr) # reset tty + return rows, cols + +def normal(): + """Switch to normal text.""" + sys.stdout.write('\x1B[0m') + +def bold(): + """Switch to bold text.""" + sys.stdout.write('\x1B[1m') + +def underlined(): + """Switch to underlined text.""" + sys.stdout.write('\x1B[4m') + +def inverse(): + """Switch to underlined text.""" + sys.stdout.write('\x1B[7m') + +def black(): + """Change text color to black.""" + # magic escape sequence. + sys.stdout.write('\x1B[30m') def red(): """Change text color to red.""" # magic escape sequence. - sys.stdout.write('\x1B[0;31m') + sys.stdout.write('\x1B[31m') def green(): """Change text color to green.""" # magic escape sequence. - sys.stdout.write('\x1B[0;32m') + sys.stdout.write('\x1B[32m') -def orange(): - """Change text color to orange.""" +def yellow(): + """Change text color to yellow.""" # magic escape sequence. - sys.stdout.write('\x1B[0;33m') + sys.stdout.write('\x1B[33m') def blue(): """Change text color to blue.""" # magic escape sequence. - sys.stdout.write('\x1B[0;34m') + sys.stdout.write('\x1B[34m') -def purple(): - """Change text color to purple.""" +def magenta(): + """Change text color to magenta.""" # magic escape sequence. - sys.stdout.write('\x1B[0;35m') + sys.stdout.write('\x1B[35m') def cyan(): """Change text color to cyan.""" # magic escape sequence. - sys.stdout.write('\x1B[0;36m') + sys.stdout.write('\x1B[36m') -def grey(): - """Change text color to grey.""" +def white(): + """Change text color to white.""" # magic escape sequence. - sys.stdout.write('\x1B[0;37m') -gray=grey + sys.stdout.write('\x1B[37m') -def black(): - """Change text color to blue.""" - # magic escape sequence. - # ;38m seems to be identical to this one. - sys.stdout.write('\x1B[0;39m') def reset(): - """Clear screen and reset text color to black.""" + """Clear screen and reset text color.""" clear() - black() + sys.stdout.write('\x1B[0;39m') -- cgit v0.9.1