1
0

console.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 ctypes
  12. import fcntl
  13. import os
  14. import platform
  15. import struct
  16. import subprocess
  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 (
  28. current_os == "Linux"
  29. or current_os == "Darwin"
  30. or current_os.startswith("CYGWIN")
  31. ):
  32. tuple_xy = _getTerminalSize_linux()
  33. if tuple_xy is None:
  34. tuple_xy = (80, 25) # default value
  35. return tuple_xy
  36. def _getTerminalSize_windows():
  37. res = None
  38. try:
  39. # stdin handle is -10
  40. # stdout handle is -11
  41. # stderr handle is -12
  42. h = ctypes.windll.kernel32.GetStdHandle(-12)
  43. csbi = ctypes.create_string_buffer(22)
  44. res = ctypes.windll.kernel32.GetConsoleScreenBufferInfo(h, csbi)
  45. except Exception: # pylint: disable=broad-except
  46. return None
  47. if res:
  48. (
  49. bufx,
  50. bufy,
  51. curx,
  52. cury,
  53. wattr,
  54. left,
  55. top,
  56. right,
  57. bottom,
  58. maxx,
  59. maxy,
  60. ) = struct.unpack(b"hhhhHhhhhhh", csbi.raw)
  61. sizex = right - left + 1
  62. sizey = bottom - top + 1
  63. return sizex, sizey
  64. else:
  65. return None
  66. def _getTerminalSize_tput():
  67. # get terminal width
  68. # src: http://stackoverflow.com/questions/263890/how-do-i-find-the-width-height-of-a-terminal-window
  69. try:
  70. proc = subprocess.Popen(
  71. ["tput", "cols"], stdin=subprocess.PIPE, stdout=subprocess.PIPE
  72. )
  73. output = proc.communicate(input=None)
  74. cols = int(output[0])
  75. proc = subprocess.Popen(
  76. ["tput", "lines"], stdin=subprocess.PIPE, stdout=subprocess.PIPE
  77. )
  78. output = proc.communicate(input=None)
  79. rows = int(output[0])
  80. return (cols, rows)
  81. except Exception: # pylint: disable=broad-except
  82. return None
  83. def _getTerminalSize_linux():
  84. def ioctl_GWINSZ(fd):
  85. try:
  86. cr = struct.unpack(b"hh", fcntl.ioctl(fd, termios.TIOCGWINSZ, "1234"))
  87. except Exception: # pylint: disable=broad-except
  88. return None
  89. return cr
  90. cr = ioctl_GWINSZ(0) or ioctl_GWINSZ(1) or ioctl_GWINSZ(2)
  91. if not cr:
  92. try:
  93. fd = os.open(os.ctermid(), os.O_RDONLY)
  94. cr = ioctl_GWINSZ(fd)
  95. os.close(fd)
  96. except Exception: # pylint: disable=broad-except
  97. pass
  98. if not cr:
  99. try:
  100. cr = (os.environ["LINES"], os.environ["COLUMNS"])
  101. except Exception: # pylint: disable=broad-except
  102. return None
  103. return int(cr[1]), int(cr[0])
  104. if __name__ == "__main__":
  105. sizex, sizey = getTerminalSize()
  106. print("width = {0} height = {1}".format(sizex, sizey))