terminal.py 3.1 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. # 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. assert _xterm_term_title_saved
  60. sys.stdout.write('\033[23;0t')
  61. _xterm_term_title_saved = False
  62. if os.name == 'posix':
  63. TERM = os.environ.get('TERM','')
  64. if TERM.startswith('xterm'):
  65. _set_term_title = _set_term_title_xterm
  66. _restore_term_title = _restore_term_title_xterm
  67. elif sys.platform == 'win32':
  68. import ctypes
  69. SetConsoleTitleW = ctypes.windll.kernel32.SetConsoleTitleW
  70. SetConsoleTitleW.argtypes = [ctypes.c_wchar_p]
  71. def _set_term_title(title):
  72. """Set terminal title using ctypes to access the Win32 APIs."""
  73. SetConsoleTitleW(title)
  74. def set_term_title(title):
  75. """Set terminal title using the necessary platform-dependent calls."""
  76. if ignore_termtitle:
  77. return
  78. _set_term_title(title)
  79. def restore_term_title():
  80. """Restore, if possible, terminal title to the original state"""
  81. if ignore_termtitle:
  82. return
  83. _restore_term_title()
  84. def freeze_term_title():
  85. warnings.warn("This function is deprecated, use toggle_set_term_title()")
  86. global ignore_termtitle
  87. ignore_termtitle = True
  88. def get_terminal_size(defaultx=80, defaulty=25):
  89. return _get_terminal_size((defaultx, defaulty))