Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/library
diff options
context:
space:
mode:
authorC. Scott Ananian <cscott@laptop.org>2007-12-13 20:28:03 (GMT)
committer C. Scott Ananian <cscott@laptop.org>2007-12-13 20:28:03 (GMT)
commita6c08bff8480640f41c5f70e3617498cb1d5108f (patch)
tree4744e50da30dbefbf4c301b098d0967f0c2e729d /library
parent79b160ce0cde1503e0564cfe9e7357e54b497bad (diff)
Add additional escape sequences to pippy.console library.
Also tweaked size() so that it returns a pair of ints; and made 'thanks' match.
Diffstat (limited to 'library')
-rw-r--r--library/pippy/console.py81
1 files changed, 57 insertions, 24 deletions
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')