def get_terminal_size(fd=1, defaultx=80, defaulty=25):
"""Returns height and width of current terminal. First tries to get
size via termios.TIOCGWINSZ, then from environment. Defaults to 25
lines x 80 columns if both methods fail.
:param fd: file descriptor (default: 1=stdout)
:defaultx: The value to return for x if unable to determine
(default: 80)
:param fd: The value to return for y if unable to determine
(default: 80)
"""
try:
import fcntl, termios, struct
hw = struct.unpack('hh', fcntl.ioctl(fd, termios.TIOCGWINSZ, '1234'))
wh = (hw[1], hw[0])
except:
try:
wh = (os.environ['COLUMNS'], os.environ['LINES'])
except:
wh = (80, 25)
return wh
评论列表
文章目录