/pandas/util/terminal.py

https://github.com/hoffstein/pandas · Python · 120 lines · 81 code · 13 blank · 26 comment · 19 complexity · f1f957149241b734f39cbdefcec36bf5 MD5 · raw file

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