3
Utilities for working with terminals.
9
* Alexander Belchenko (e-mail: bialix AT ukr.net)
12
#-----------------------------------------------------------------------------
13
# Copyright (C) 2008-2009 The IPython Development Team
15
# Distributed under the terms of the BSD License. The full license is in
16
# the file COPYING, distributed as part of this software.
17
#-----------------------------------------------------------------------------
19
#-----------------------------------------------------------------------------
21
#-----------------------------------------------------------------------------
28
#-----------------------------------------------------------------------------
30
#-----------------------------------------------------------------------------
32
# This variable is part of the expected API of the module:
33
ignore_termtitle = True
40
if os.name == 'posix':
45
if sys.platform == 'win32':
54
def toggle_set_term_title(val):
55
"""Control whether set_term_title is active or not.
57
set_term_title() allows writing to the console titlebar. In embedded
58
widgets this can cause problems, so this call can be used to toggle it on
61
The default state of the module is for the function to be disabled.
66
If True, set_term_title() actually writes to the terminal (using the
67
appropriate platform-specific module). If False, it is a no-op.
69
global ignore_termtitle
70
ignore_termtitle = not(val)
73
def _set_term_title(*args,**kw):
78
def _set_term_title_xterm(title):
79
""" Change virtual terminal title in xterm-workalikes """
80
sys.stdout.write('\033]0;%s\007' % title)
82
if os.name == 'posix':
83
TERM = os.environ.get('TERM','')
84
if (TERM == 'xterm') or (TERM == 'xterm-color'):
85
_set_term_title = _set_term_title_xterm
88
if sys.platform == 'win32':
92
SetConsoleTitleW = ctypes.windll.kernel32.SetConsoleTitleW
93
SetConsoleTitleW.argtypes = [ctypes.c_wchar_p]
95
def _set_term_title(title):
96
"""Set terminal title using ctypes to access the Win32 APIs."""
97
SetConsoleTitleW(title)
99
def _set_term_title(title):
100
"""Set terminal title using the 'title' command."""
101
global ignore_termtitle
104
# Cannot be on network share when issuing system commands
107
ret = os.system("title " + title)
111
# non-zero return code signals error, don't try again
112
ignore_termtitle = True
115
def set_term_title(title):
116
"""Set terminal title using the necessary platform-dependent calls."""
119
_set_term_title(title)
122
def freeze_term_title():
123
warnings.warn("This function is deprecated, use toggle_set_term_title()")
124
global ignore_termtitle
125
ignore_termtitle = True
128
def get_terminal_size(defaultx=80, defaulty=25):
129
return defaultx, defaulty
132
if sys.platform == 'win32':
133
def get_terminal_size(defaultx=80, defaulty=25):
134
"""Return size of current terminal console.
136
This function try to determine actual size of current working
137
console window and return tuple (sizex, sizey) if success,
138
or default size (defaultx, defaulty) otherwise.
140
Dependencies: ctypes should be installed.
142
Author: Alexander Belchenko (e-mail: bialix AT ukr.net)
147
return defaultx, defaulty
149
h = ctypes.windll.kernel32.GetStdHandle(-11)
150
csbi = ctypes.create_string_buffer(22)
151
res = ctypes.windll.kernel32.GetConsoleScreenBufferInfo(h, csbi)
154
(bufx, bufy, curx, cury, wattr,
155
left, top, right, bottom, maxx, maxy) = struct.unpack(
156
"hhhhHhhhhhh", csbi.raw)
157
sizex = right - left + 1
158
sizey = bottom - top + 1
159
return (sizex, sizey)
161
return (defaultx, defaulty)