def terminal_size():
"""Get the width and height of the terminal.
http://code.activestate.com/recipes/440694-determine-size-of-console-window-on-windows/
http://stackoverflow.com/questions/17993814/why-the-irrelevant-code-made-a-difference
:return: Width (number of characters) and height (number of lines) of the terminal.
:rtype: tuple
"""
if hasattr(ctypes, 'windll'):
# Only works on Microsoft Windows platforms.
string_buffer = ctypes.create_string_buffer(22) # To be written to by GetConsoleScreenBufferInfo.
ctypes.windll.kernel32.GetConsoleScreenBufferInfo(ctypes.windll.kernel32.GetStdHandle(-11), string_buffer)
left, top, right, bottom = struct.unpack('hhhhHhhhhhh', string_buffer.raw)[5:-2]
width, height = right - left, bottom - top
if width < 1 or height < 1:
return DEFAULT_WIDTH, DEFAULT_HEIGHT
return width, height
try:
device = fcntl.ioctl(0, termios.TIOCGWINSZ, '\0' * 8)
except IOError:
return DEFAULT_WIDTH, DEFAULT_HEIGHT
height, width = struct.unpack('hhhh', device)[:2]
return width, height
评论列表
文章目录