##// END OF EJS Templates
Use term width to determine header length
Use term width to determine header length

File last commit:

r22150:7e110f3b
r22150:7e110f3b
Show More
terminal.py
125 lines | 3.5 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)
"""
#-----------------------------------------------------------------------------
Matthias BUSSONNIER
update copyright to 2011/20xx-2011...
r5390 # Copyright (C) 2008-2011 The IPython Development Team
Brian Granger
Work to address the review comments on Fernando's branch....
r2498 #
# Distributed under the terms of the BSD License. The full license is in
# the file COPYING, distributed as part of this software.
#-----------------------------------------------------------------------------
#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
import os
import struct
import sys
import warnings
Vincent Woo
Use term width to determine header length
r22150 import backports.shutil_get_terminal_size
Brian Granger
Work to address the review comments on Fernando's branch....
r2498
Thomas Kluyver
Python 3 compatibility for os.getcwdu()
r13447 from . import py3compat
Brian Granger
Work to address the review comments on Fernando's branch....
r2498 #-----------------------------------------------------------------------------
# Code
#-----------------------------------------------------------------------------
# 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
----------
val : bool
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
def _set_term_title_xterm(title):
""" Change virtual terminal title in xterm-workalikes """
sys.stdout.write('\033]0;%s\007' % title)
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
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 try:
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)
except ImportError:
def _set_term_title(title):
"""Set terminal title using the 'title' command."""
global ignore_termtitle
try:
# Cannot be on network share when issuing system commands
Thomas Kluyver
Python 3 compatibility for os.getcwdu()
r13447 curr = py3compat.getcwd()
Brian Granger
Work to address the review comments on Fernando's branch....
r2498 os.chdir("C:")
ret = os.system("title " + title)
finally:
os.chdir(curr)
if ret:
# non-zero return code signals error, don't try again
ignore_termtitle = True
def set_term_title(title):
"""Set terminal title using the necessary platform-dependent calls."""
if ignore_termtitle:
return
_set_term_title(title)
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):
return backports.shutil_get_terminal_size.get_terminal_size((defaultx, defaulty))