Skip to content

Instantly share code, notes, and snippets.

@acaranta
Created September 10, 2014 10:07
Show Gist options
  • Save acaranta/e4fbfbbd25a9cd720ef0 to your computer and use it in GitHub Desktop.
Save acaranta/e4fbfbbd25a9cd720ef0 to your computer and use it in GitHub Desktop.
get Terminal size in python 2.7
##Returns the terminal size WxH
#Found on http://stackoverflow.com/a/566752/2646228
def getTerminalSize():
import os
env = os.environ
def ioctl_GWINSZ(fd):
try:
import fcntl, termios, struct, os
cr = struct.unpack('hh', fcntl.ioctl(fd, termios.TIOCGWINSZ,
'1234'))
except:
return
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:
cr = (env.get('LINES', 25), env.get('COLUMNS', 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