Created
September 10, 2014 10:07
-
-
Save acaranta/e4fbfbbd25a9cd720ef0 to your computer and use it in GitHub Desktop.
get Terminal size in python 2.7
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
##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