terminal.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. from shutil import get_terminal_size as _get_terminal_size
  15. # This variable is part of the expected API of the module:
  16. ignore_termtitle = True
  17. if os.name == 'posix':
  18. def _term_clear():
  19. os.system('clear')
  20. elif sys.platform == 'win32':
  21. def _term_clear():
  22. os.system('cls')
  23. else:
  24. def _term_clear():
  25. pass
  26. def toggle_set_term_title(val):
  27. """Control whether set_term_title is active or not.
  28. set_term_title() allows writing to the console titlebar. In embedded
  29. widgets this can cause problems, so this call can be used to toggle it on
  30. or off as needed.
  31. The default state of the module is for the function to be disabled.
  32. Parameters
  33. ----------
  34. val : bool
  35. If True, set_term_title() actually writes to the terminal (using the
  36. appropriate platform-specific module). If False, it is a no-op.
  37. """
  38. global ignore_termtitle
  39. ignore_termtitle = not(val)
  40. def _set_term_title(*args,**kw):
  41. """Dummy no-op."""
  42. pass
  43. def _restore_term_title():
  44. pass
  45. _xterm_term_title_saved = False
  46. def _set_term_title_xterm(title):
  47. """ Change virtual terminal title in xterm-workalikes """
  48. global _xterm_term_title_saved
  49. # Only save the title the first time we set, otherwise restore will only
  50. # go back one title (probably undoing a %cd title change).
  51. if not _xterm_term_title_saved:
  52. # save the current title to the xterm "stack"
  53. sys.stdout.write("\033[22;0t")
  54. _xterm_term_title_saved = True
  55. sys.stdout.write('\033]0;%s\007' % title)
  56. def _restore_term_title_xterm():
  57. # Make sure the restore has at least one accompanying set.
  58. global _xterm_term_title_saved
  59. if not _xterm_term_title_saved:
  60. warnings.warn(
  61. "Expecting xterm_term_title_saved to be True, but is not; will not restore terminal title.",
  62. stacklevel=1,
  63. )
  64. return
  65. sys.stdout.write('\033[23;0t')
  66. _xterm_term_title_saved = False
  67. if os.name == 'posix':
  68. TERM = os.environ.get('TERM','')
  69. if TERM.startswith('xterm'):
  70. _set_term_title = _set_term_title_xterm
  71. _restore_term_title = _restore_term_title_xterm
  72. elif sys.platform == 'win32':
  73. import ctypes
  74. SetConsoleTitleW = ctypes.windll.kernel32.SetConsoleTitleW
  75. SetConsoleTitleW.argtypes = [ctypes.c_wchar_p]
  76. def _set_term_title(title):
  77. """Set terminal title using ctypes to access the Win32 APIs."""
  78. SetConsoleTitleW(title)
  79. def set_term_title(title):
  80. """Set terminal title using the necessary platform-dependent calls."""
  81. if ignore_termtitle:
  82. return
  83. _set_term_title(title)
  84. def restore_term_title():
  85. """Restore, if possible, terminal title to the original state"""
  86. if ignore_termtitle:
  87. return
  88. _restore_term_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))