|
| 1 | +""" |
| 2 | +get_terminal_size() -- return width and height of console as a tuple |
| 3 | +
|
| 4 | +code from: |
| 5 | +http://stackoverflow.com/questions/566746/how-to-get-console- window-width-in- |
| 6 | +python |
| 7 | +
|
| 8 | +written by |
| 9 | +Harco Kuppens (http://stackoverflow.com/users/825214/harco-kuppens) |
| 10 | +
|
| 11 | +It is mentioned in the stackoverflow response that this code works |
| 12 | +on linux, os x, windows and cygwin (windows). |
| 13 | +""" |
| 14 | + |
| 15 | +__all__=['get_terminal_size'] |
| 16 | + |
| 17 | + |
| 18 | +def get_terminal_size(): |
| 19 | + import platform |
| 20 | + current_os = platform.system() |
| 21 | + tuple_xy=None |
| 22 | + if current_os == 'Windows': |
| 23 | + tuple_xy = _get_terminal_size_windows() |
| 24 | + if tuple_xy is None: |
| 25 | + tuple_xy = _get_terminal_size_tput() |
| 26 | + # needed for window's python in cygwin's xterm! |
| 27 | + if current_os == 'Linux' or \ |
| 28 | + current_os == 'Darwin' or \ |
| 29 | + current_os.startswith('CYGWIN'): |
| 30 | + tuple_xy = _get_terminal_size_linux() |
| 31 | + if tuple_xy is None: |
| 32 | + tuple_xy = (80, 25) # default value |
| 33 | + return tuple_xy |
| 34 | + |
| 35 | +def _get_terminal_size_windows(): |
| 36 | + res=None |
| 37 | + try: |
| 38 | + from ctypes import windll, create_string_buffer |
| 39 | + |
| 40 | + # stdin handle is -10 |
| 41 | + # stdout handle is -11 |
| 42 | + # stderr handle is -12 |
| 43 | + |
| 44 | + h = windll.kernel32.GetStdHandle(-12) |
| 45 | + csbi = create_string_buffer(22) |
| 46 | + res = windll.kernel32.GetConsoleScreenBufferInfo(h, csbi) |
| 47 | + except: |
| 48 | + return None |
| 49 | + if res: |
| 50 | + import struct |
| 51 | + (bufx, bufy, curx, cury, wattr, left, top, right, bottom, maxx, |
| 52 | + maxy) = struct.unpack("hhhhHhhhhhh", csbi.raw) |
| 53 | + sizex = right - left + 1 |
| 54 | + sizey = bottom - top + 1 |
| 55 | + return sizex, sizey |
| 56 | + else: |
| 57 | + return None |
| 58 | + |
| 59 | +def _get_terminal_size_tput(): |
| 60 | + # get terminal width |
| 61 | + # src: http://stackoverflow.com/questions/263890/how-do-i-find-the-width |
| 62 | + # -height-of-a-terminal-window |
| 63 | + try: |
| 64 | + import subprocess |
| 65 | + proc = subprocess.Popen(["tput", "cols"], |
| 66 | + stdin=subprocess.PIPE, |
| 67 | + stdout=subprocess.PIPE) |
| 68 | + output=proc.communicate(input=None) |
| 69 | + cols=int(output[0]) |
| 70 | + proc=subprocess.Popen(["tput", "lines"], |
| 71 | + stdin=subprocess.PIPE, |
| 72 | + stdout=subprocess.PIPE) |
| 73 | + output=proc.communicate(input=None) |
| 74 | + rows=int(output[0]) |
| 75 | + return (cols,rows) |
| 76 | + except: |
| 77 | + return None |
| 78 | + |
| 79 | + |
| 80 | +def _get_terminal_size_linux(): |
| 81 | + def ioctl_GWINSZ(fd): |
| 82 | + try: |
| 83 | + import fcntl, termios, struct, os |
| 84 | + cr = struct.unpack('hh', fcntl.ioctl(fd, termios.TIOCGWINSZ,'1234')) |
| 85 | + except: |
| 86 | + return None |
| 87 | + return cr |
| 88 | + cr = ioctl_GWINSZ(0) or ioctl_GWINSZ(1) or ioctl_GWINSZ(2) |
| 89 | + if not cr: |
| 90 | + try: |
| 91 | + fd = os.open(os.ctermid(), os.O_RDONLY) |
| 92 | + cr = ioctl_GWINSZ(fd) |
| 93 | + os.close(fd) |
| 94 | + except: |
| 95 | + pass |
| 96 | + if not cr: |
| 97 | + try: |
| 98 | + cr = (env['LINES'], env['COLUMNS']) |
| 99 | + except: |
| 100 | + return None |
| 101 | + return int(cr[1]), int(cr[0]) |
| 102 | + |
| 103 | +if __name__ == "__main__": |
| 104 | + sizex, sizey = get_terminal_size() |
| 105 | + print 'width =', sizex, 'height =', sizey |
0 commit comments