terminal.py 3.2 KB

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