Created
November 2, 2016 10:05
-
-
Save xianghuzhao/21867316888616a6ce79f3e94bdcc167 to your computer and use it in GitHub Desktop.
Terminal Size
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
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