Skip to content

Instantly share code, notes, and snippets.

@xianghuzhao
Created November 2, 2016 10:05
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 xianghuzhao/21867316888616a6ce79f3e94bdcc167 to your computer and use it in GitHub Desktop.
Save xianghuzhao/21867316888616a6ce79f3e94bdcc167 to your computer and use it in GitHub Desktop.
Terminal Size
def get_terminal_size_linux():
def ioctl_GWINSZ(fd):
try:
import fcntl, termios, struct
cr = struct.unpack('hh',
fcntl.ioctl(fd, termios.TIOCGWINSZ, '1234'))
return cr
except:
pass
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 = (os.environ['LINES'], os.environ['COLUMNS'])
except:
return None
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