Skip to content

Instantly share code, notes, and snippets.

@chaos95
Created May 2, 2011 00:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chaos95/951050 to your computer and use it in GitHub Desktop.
Save chaos95/951050 to your computer and use it in GitHub Desktop.
Get Terminal Size via python
# found at http://stackoverflow.com/questions/566746/how-to-get-console-window-width-in-python/566752#566752
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