1
0

console.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. # -*- coding: utf-8 -*-
  2. # vim: sw=4 ts=4 fenc=utf-8
  3. '''
  4. getTerminalSize()
  5. - get width and height of console
  6. - works on linux,os x,windows,cygwin(windows)
  7. - taken from http://stackoverflow.com/questions/566746/how-to-get-console-window-width-in-python
  8. '''
  9. # Import python libs
  10. from __future__ import absolute_import, print_function
  11. import os
  12. import platform
  13. import struct
  14. import ctypes
  15. import subprocess
  16. import fcntl
  17. import termios
  18. __all__ = ['getTerminalSize']
  19. def getTerminalSize():
  20. current_os = platform.system()
  21. tuple_xy = None
  22. if current_os == 'Windows':
  23. tuple_xy = _getTerminalSize_windows()
  24. if tuple_xy is None:
  25. tuple_xy = _getTerminalSize_tput()
  26. # needed for window's python in cygwin's xterm!
  27. if current_os == 'Linux' or current_os == 'Darwin' or \
  28. current_os.startswith('CYGWIN'):
  29. tuple_xy = _getTerminalSize_linux()
  30. if tuple_xy is None:
  31. tuple_xy = (80, 25) # default value
  32. return tuple_xy
  33. def _getTerminalSize_windows():
  34. res = None
  35. try:
  36. # stdin handle is -10
  37. # stdout handle is -11
  38. # stderr handle is -12
  39. h = ctypes.windll.kernel32.GetStdHandle(-12)
  40. csbi = ctypes.create_string_buffer(22)
  41. res = ctypes.windll.kernel32.GetConsoleScreenBufferInfo(h, csbi)
  42. except Exception:
  43. return None
  44. if res:
  45. (bufx, bufy, curx, cury, wattr,
  46. left, top, right, bottom, maxx, maxy) = struct.unpack(
  47. b'hhhhHhhhhhh', csbi.raw)
  48. sizex = right - left + 1
  49. sizey = bottom - top + 1
  50. return sizex, sizey
  51. else:
  52. return None
  53. def _getTerminalSize_tput():
  54. # get terminal width
  55. # src: http://stackoverflow.com/questions/263890/how-do-i-find-the-width-height-of-a-terminal-window
  56. try:
  57. proc = subprocess.Popen(
  58. ['tput', 'cols'], stdin=subprocess.PIPE, stdout=subprocess.PIPE
  59. )
  60. output = proc.communicate(input=None)
  61. cols = int(output[0])
  62. proc = subprocess.Popen(
  63. ['tput', 'lines'], stdin=subprocess.PIPE, stdout=subprocess.PIPE
  64. )
  65. output = proc.communicate(input=None)
  66. rows = int(output[0])
  67. return (cols, rows)
  68. except Exception:
  69. return None
  70. def _getTerminalSize_linux():
  71. def ioctl_GWINSZ(fd):
  72. try:
  73. cr = struct.unpack(
  74. b'hh', fcntl.ioctl(fd, termios.TIOCGWINSZ, '1234')
  75. )
  76. except Exception:
  77. return None
  78. return cr
  79. cr = ioctl_GWINSZ(0) or ioctl_GWINSZ(1) or ioctl_GWINSZ(2)
  80. if not cr:
  81. try:
  82. fd = os.open(os.ctermid(), os.O_RDONLY)
  83. cr = ioctl_GWINSZ(fd)
  84. os.close(fd)
  85. except Exception:
  86. pass
  87. if not cr:
  88. try:
  89. cr = (os.environ['LINES'], os.environ['COLUMNS'])
  90. except Exception:
  91. return None
  92. return int(cr[1]), int(cr[0])
  93. if __name__ == '__main__':
  94. sizex, sizey = getTerminalSize()
  95. print('width = {0} height = {1}'.format(sizex, sizey))