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-14 18:46:20 (GMT)
committer C. Scott Ananian <cscott@laptop.org>2007-12-14 19:49:16 (GMT)
commitf8f205f93f22b2d92a8ecc074f225deffc68dfc8 (patch)
tree88336787d4c24d5b5f1354b6aff9b8fc1ecdfa41 /library
parent71c9a57d4fbc4713b9f5892e1e9140d12cb256fc (diff)
Add more exciting features to the console library!
Cursor movement commands, get/set cursor position, and show/hide cursor. We use (x, y) uniformly in the console routines (bucking decades of ncurses precedent).
Diffstat (limited to 'library')
-rw-r--r--library/pippy/console.py58
1 files changed, 57 insertions, 1 deletions
diff --git a/library/pippy/console.py b/library/pippy/console.py
index a83b756..a3e8515 100644
--- a/library/pippy/console.py
+++ b/library/pippy/console.py
@@ -25,7 +25,55 @@ def size():
rows = int(read_to_delimit(';'))
cols = int(read_to_delimit('t'))
termios.tcsetattr(fd, termios.TCSANOW, oldattr) # reset tty
- return rows, cols
+ return cols, rows
+
+def getpos():
+ """Return the current x, y position of the cursor on the screen.
+
+ The top-left corner is 1,1."""
+ # xterm magic! see http://rtfm.etla.org/xterm/ctlseq.html
+ sys.stdout.flush() # ensure that writes to the terminal have finished
+ 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[6n') # Report Cursor Position
+ read_to_delimit('\x1b') # parse response.
+ read_to_delimit('[')
+ row = int(read_to_delimit(';'))
+ col = int(read_to_delimit('R'))
+ termios.tcsetattr(fd, termios.TCSANOW, oldattr) # reset tty
+ return col, row
+
+def setpos(column, row):
+ """Move to the given position on the screen.
+
+ The top-left corner is 1,1"""
+ # xterm magic! see http://rtfm.etla.org/xterm/ctlseq.html
+ sys.stdout.write('\x1B[%d;%dH' % (row, column))
+
+def up(count=1):
+ """Move the cursor up the given number of rows."""
+ sys.stdout.write('\x1B[%dA' % count)
+
+def down(count=1):
+ """Move the cursor down the given number of rows."""
+ sys.stdout.write('\x1B[%dB' % count)
+
+def forward(count=1):
+ """Move the cursor forward the given number of columns."""
+ sys.stdout.write('\x1B[%dC' % count)
+
+def backward(count=1):
+ """Move the cursor backward the given number of columns."""
+ sys.stdout.write('\x1B[%dD' % count)
def normal():
"""Switch to normal text."""
@@ -83,8 +131,16 @@ def white():
# magic escape sequence.
sys.stdout.write('\x1B[37m')
+def hide_cursor():
+ """Hide the cursor."""
+ sys.stdout.write('\x1B[?25l')
+
+def show_cursor():
+ """Show the cursor."""
+ sys.stdout.write('\x1B[?25h')
def reset():
"""Clear screen and reset text color."""
clear()
+ show_cursor()
sys.stdout.write('\x1B[0;39m')