terminal.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. # encoding: utf-8
  2. """
  3. Utilities for working with terminals.
  4. Authors:
  5. * Brian E. Granger
  6. * Fernando Perez
  7. * Alexander Belchenko (e-mail: bialix AT ukr.net)
  8. """
  9. from __future__ import absolute_import
  10. # Copyright (c) IPython Development Team.
  11. # Distributed under the terms of the Modified BSD License.
  12. import os
  13. import sys
  14. import warnings
  15. try:
  16. from shutil import get_terminal_size as _get_terminal_size
  17. except ImportError:
  18. # use backport on Python 2
  19. try:
  20. from backports.shutil_get_terminal_size import get_terminal_size as _get_terminal_size
  21. except ImportError:
  22. from ._get_terminal_size import get_terminal_size as _get_terminal_size
  23. from . import py3compat
  24. #-----------------------------------------------------------------------------
  25. # Code
  26. #-----------------------------------------------------------------------------
  27. # This variable is part of the expected API of the module:
  28. ignore_termtitle = True
  29. if os.name == 'posix':
  30. def _term_clear():
  31. os.system('clear')
  32. elif sys.platform == 'win32':
  33. def _term_clear():
  34. os.system('cls')
  35. else:
  36. def _term_clear():
  37. pass
  38. def toggle_set_term_title(val):
  39. """Control whether set_term_title is active or not.
  40. set_term_title() allows writing to the console titlebar. In embedded
  41. widgets this can cause problems, so this call can be used to toggle it on
  42. or off as needed.
  43. The default state of the module is for the function to be disabled.
  44. Parameters
  45. ----------
  46. val : bool
  47. If True, set_term_title() actually writes to the terminal (using the
  48. appropriate platform-specific module). If False, it is a no-op.
  49. """
  50. global ignore_termtitle
  51. ignore_termtitle = not(val)
  52. def _set_term_title(*args,**kw):
  53. """Dummy no-op."""
  54. pass
  55. def _set_term_title_xterm(title):
  56. """ Change virtual terminal title in xterm-workalikes """
  57. sys.stdout.write('\033]0;%s\007' % title)
  58. if os.name == 'posix':
  59. TERM = os.environ.get('TERM','')
  60. if TERM.startswith('xterm'):
  61. _set_term_title = _set_term_title_xterm
  62. elif sys.platform == 'win32':
  63. try:
  64. import ctypes
  65. SetConsoleTitleW = ctypes.windll.kernel32.SetConsoleTitleW
  66. SetConsoleTitleW.argtypes = [ctypes.c_wchar_p]
  67. def _set_term_title(title):
  68. """Set terminal title using ctypes to access the Win32 APIs."""
  69. SetConsoleTitleW(title)
  70. except ImportError:
  71. def _set_term_title(title):
  72. """Set terminal title using the 'title' command."""
  73. global ignore_termtitle
  74. try:
  75. # Cannot be on network share when issuing system commands
  76. curr = py3compat.getcwd()
  77. os.chdir("C:")
  78. ret = os.system("title " + title)
  79. finally:
  80. os.chdir(curr)
  81. if ret:
  82. # non-zero return code signals error, don't try again
  83. ignore_termtitle = True
  84. def set_term_title(title):
  85. """Set terminal title using the necessary platform-dependent calls."""
  86. if ignore_termtitle:
  87. return
  88. _set_term_title(title)
  89. def freeze_term_title():
  90. warnings.warn("This function is deprecated, use toggle_set_term_title()")
  91. global ignore_termtitle
  92. ignore_termtitle = True
  93. def get_terminal_size(defaultx=80, defaulty=25):
  94. return _get_terminal_size((defaultx, defaulty))