##// END OF EJS Templates
forgotten import (#13961)...
forgotten import (#13961) Fixes #13967

File last commit:

r28089:991849c2
r28161:a80ee46c merge
Show More
terminal.py
125 lines | 3.1 KiB | text/x-python | PythonLexer
Brian Granger
Work to address the review comments on Fernando's branch....
r2498 # encoding: utf-8
"""
Utilities for working with terminals.
Authors:
* Brian E. Granger
* Fernando Perez
* Alexander Belchenko (e-mail: bialix AT ukr.net)
"""
Min RK
only use backports.shutil_get_terminal_size on Python 2...
r22266 # Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.
Brian Granger
Work to address the review comments on Fernando's branch....
r2498
import os
import sys
import warnings
Srinivas Reddy Thatiparthy
remove usage of backports.shutil_get_terminal_size
r23251 from shutil import get_terminal_size as _get_terminal_size
Brian Granger
Work to address the review comments on Fernando's branch....
r2498
# This variable is part of the expected API of the module:
ignore_termtitle = True
if os.name == 'posix':
def _term_clear():
os.system('clear')
Matthias Bussonnier
Some code cleanup in javascript and python...
r19739 elif sys.platform == 'win32':
Brian Granger
Work to address the review comments on Fernando's branch....
r2498 def _term_clear():
os.system('cls')
Matthias Bussonnier
Some code cleanup in javascript and python...
r19739 else:
def _term_clear():
pass
Brian Granger
Work to address the review comments on Fernando's branch....
r2498
def toggle_set_term_title(val):
"""Control whether set_term_title is active or not.
set_term_title() allows writing to the console titlebar. In embedded
widgets this can cause problems, so this call can be used to toggle it on
or off as needed.
The default state of the module is for the function to be disabled.
Parameters
----------
Matthias Bussonnier
reformat docstring in IPython utils
r26419 val : bool
Brian Granger
Work to address the review comments on Fernando's branch....
r2498 If True, set_term_title() actually writes to the terminal (using the
appropriate platform-specific module). If False, it is a no-op.
"""
global ignore_termtitle
ignore_termtitle = not(val)
def _set_term_title(*args,**kw):
"""Dummy no-op."""
pass
Георгий Фролов
restore terminal title on exit (xterm)
r25194 def _restore_term_title():
pass
Maciej Goszczycki
Only save the initial terminal title
r27832 _xterm_term_title_saved = False
Brian Granger
Work to address the review comments on Fernando's branch....
r2498 def _set_term_title_xterm(title):
""" Change virtual terminal title in xterm-workalikes """
Maciej Goszczycki
Only save the initial terminal title
r27832 global _xterm_term_title_saved
# Only save the title the first time we set, otherwise restore will only
# go back one title (probably undoing a %cd title change).
if not _xterm_term_title_saved:
# save the current title to the xterm "stack"
Matthias Bussonnier
Fix formatting
r27833 sys.stdout.write("\033[22;0t")
Maciej Goszczycki
Only save the initial terminal title
r27832 _xterm_term_title_saved = True
Brian Granger
Work to address the review comments on Fernando's branch....
r2498 sys.stdout.write('\033]0;%s\007' % title)
Георгий Фролов
restore terminal title on exit (xterm)
r25194
def _restore_term_title_xterm():
Maciej Goszczycki
Only save the initial terminal title
r27832 # Make sure the restore has at least one accompanying set.
global _xterm_term_title_saved
assert _xterm_term_title_saved
Георгий Фролов
restore terminal title on exit (xterm)
r25194 sys.stdout.write('\033[23;0t')
Maciej Goszczycki
Only save the initial terminal title
r27832 _xterm_term_title_saved = False
Георгий Фролов
restore terminal title on exit (xterm)
r25194
Brian Granger
Work to address the review comments on Fernando's branch....
r2498 if os.name == 'posix':
TERM = os.environ.get('TERM','')
Lars Solberg
Fix set_term_title when xterm represent itself as xterm*, not just xterm and xterm-color.
r8358 if TERM.startswith('xterm'):
Brian Granger
Work to address the review comments on Fernando's branch....
r2498 _set_term_title = _set_term_title_xterm
Георгий Фролов
restore terminal title on exit (xterm)
r25194 _restore_term_title = _restore_term_title_xterm
Matthias Bussonnier
Some code cleanup in javascript and python...
r19739 elif sys.platform == 'win32':
Konstantin Weddige
Fix CVE-2023-24816 by removing legacy code....
r28089 import ctypes
SetConsoleTitleW = ctypes.windll.kernel32.SetConsoleTitleW
SetConsoleTitleW.argtypes = [ctypes.c_wchar_p]
def _set_term_title(title):
"""Set terminal title using ctypes to access the Win32 APIs."""
SetConsoleTitleW(title)
Brian Granger
Work to address the review comments on Fernando's branch....
r2498
def set_term_title(title):
"""Set terminal title using the necessary platform-dependent calls."""
if ignore_termtitle:
return
_set_term_title(title)
Георгий Фролов
restore terminal title on exit (xterm)
r25194 def restore_term_title():
"""Restore, if possible, terminal title to the original state"""
if ignore_termtitle:
return
_restore_term_title()
Brian Granger
Work to address the review comments on Fernando's branch....
r2498 def freeze_term_title():
warnings.warn("This function is deprecated, use toggle_set_term_title()")
global ignore_termtitle
ignore_termtitle = True
Vincent Woo
Use term width to determine header length
r22150 def get_terminal_size(defaultx=80, defaulty=25):
Min RK
only use backports.shutil_get_terminal_size on Python 2...
r22266 return _get_terminal_size((defaultx, defaulty))