Skip to content

Instantly share code, notes, and snippets.

@saitoha
Created December 5, 2012 10:24
Show Gist options
  • Select an option

  • Save saitoha/4214542 to your computer and use it in GitHub Desktop.

Select an option

Save saitoha/4214542 to your computer and use it in GitHub Desktop.
screen dump on xterm#286
#!/usr/bin/env python
import sys, os, termios, select
def readcell(y, x):
import sys
sys.stdout.write("\x1b[;1;%d;%d;%d;%d*y" % (y, x, y, x))
sys.stdout.flush()
rfd, wfd, xfd = select.select([0], [], [], 2)
if rfd:
data = os.read(0, 32)
return unichr(int(data[5:-2], 16))
def dumpscreen():
import termios, fcntl, struct
winsize = fcntl.ioctl(0, termios.TIOCGWINSZ, 'hhhh')
height, width = struct.unpack('hh', winsize)
for y in xrange(1, height + 1):
for x in xrange(1, width + 1):
yield readcell(y, x)
yield u"\n"
vdisable = os.fpathconf(0, 'PC_VDISABLE')
backup = termios.tcgetattr(0)
new = termios.tcgetattr(0)
new[3] &= ~(termios.ECHO | termios.ICANON)
new[6][termios.VMIN] = 1
new[6][termios.VTIME] = 0
termios.tcsetattr(0, termios.TCSANOW, new)
try:
print u"".join([c for c in dumpscreen()])
finally:
termios.tcsetattr(0, termios.TCSANOW, backup)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment