Skip to content

Instantly share code, notes, and snippets.

@joshkehn
Created December 24, 2012 06:11
Show Gist options
  • Save joshkehn/4367980 to your computer and use it in GitHub Desktop.
Save joshkehn/4367980 to your computer and use it in GitHub Desktop.
def getTerminalSize():
def ioctl_GWINSZ(fd):
try:
import fcntl, termios, struct, os
cr = struct.unpack('hh', fcntl.ioctl(fd, termios.TIOCGWINSZ,
'1234'))
except:
return None
return cr
cr = ioctl_GWINSZ(0) or ioctl_GWINSZ(1) or ioctl_GWINSZ(2)
if not cr:
try:
fd = os.open(os.ctermid(), os.O_RDONLY)
cr = ioctl_GWINSZ(fd)
os.close(fd)
except:
pass
if not cr:
try:
cr = (env['LINES'], env['COLUMNS'])
except:
cr = (25, 80)
return int(cr[1]), int(cr[0])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment