Show More
@@ -1,125 +1,131 b'' | |||
|
1 | 1 | # encoding: utf-8 |
|
2 | 2 | """ |
|
3 | 3 | Utilities for working with terminals. |
|
4 | 4 | |
|
5 | 5 | Authors: |
|
6 | 6 | |
|
7 | 7 | * Brian E. Granger |
|
8 | 8 | * Fernando Perez |
|
9 | 9 | * Alexander Belchenko (e-mail: bialix AT ukr.net) |
|
10 | 10 | """ |
|
11 | 11 | |
|
12 | 12 | # Copyright (c) IPython Development Team. |
|
13 | 13 | # Distributed under the terms of the Modified BSD License. |
|
14 | 14 | |
|
15 | 15 | import os |
|
16 | 16 | import sys |
|
17 | 17 | import warnings |
|
18 | 18 | from shutil import get_terminal_size as _get_terminal_size |
|
19 | 19 | |
|
20 | 20 | # This variable is part of the expected API of the module: |
|
21 | 21 | ignore_termtitle = True |
|
22 | 22 | |
|
23 | 23 | |
|
24 | 24 | |
|
25 | 25 | if os.name == 'posix': |
|
26 | 26 | def _term_clear(): |
|
27 | 27 | os.system('clear') |
|
28 | 28 | elif sys.platform == 'win32': |
|
29 | 29 | def _term_clear(): |
|
30 | 30 | os.system('cls') |
|
31 | 31 | else: |
|
32 | 32 | def _term_clear(): |
|
33 | 33 | pass |
|
34 | 34 | |
|
35 | 35 | |
|
36 | 36 | |
|
37 | 37 | def toggle_set_term_title(val): |
|
38 | 38 | """Control whether set_term_title is active or not. |
|
39 | 39 | |
|
40 | 40 | set_term_title() allows writing to the console titlebar. In embedded |
|
41 | 41 | widgets this can cause problems, so this call can be used to toggle it on |
|
42 | 42 | or off as needed. |
|
43 | 43 | |
|
44 | 44 | The default state of the module is for the function to be disabled. |
|
45 | 45 | |
|
46 | 46 | Parameters |
|
47 | 47 | ---------- |
|
48 | 48 | val : bool |
|
49 | 49 | If True, set_term_title() actually writes to the terminal (using the |
|
50 | 50 | appropriate platform-specific module). If False, it is a no-op. |
|
51 | 51 | """ |
|
52 | 52 | global ignore_termtitle |
|
53 | 53 | ignore_termtitle = not(val) |
|
54 | 54 | |
|
55 | 55 | |
|
56 | 56 | def _set_term_title(*args,**kw): |
|
57 | 57 | """Dummy no-op.""" |
|
58 | 58 | pass |
|
59 | 59 | |
|
60 | 60 | |
|
61 | 61 | def _restore_term_title(): |
|
62 | 62 | pass |
|
63 | 63 | |
|
64 | 64 | |
|
65 | 65 | _xterm_term_title_saved = False |
|
66 | 66 | |
|
67 | 67 | |
|
68 | 68 | def _set_term_title_xterm(title): |
|
69 | 69 | """ Change virtual terminal title in xterm-workalikes """ |
|
70 | 70 | global _xterm_term_title_saved |
|
71 | 71 | # Only save the title the first time we set, otherwise restore will only |
|
72 | 72 | # go back one title (probably undoing a %cd title change). |
|
73 | 73 | if not _xterm_term_title_saved: |
|
74 | 74 | # save the current title to the xterm "stack" |
|
75 | 75 | sys.stdout.write("\033[22;0t") |
|
76 | 76 | _xterm_term_title_saved = True |
|
77 | 77 | sys.stdout.write('\033]0;%s\007' % title) |
|
78 | 78 | |
|
79 | 79 | |
|
80 | 80 | def _restore_term_title_xterm(): |
|
81 | 81 | # Make sure the restore has at least one accompanying set. |
|
82 | 82 | global _xterm_term_title_saved |
|
83 |
|
|
|
83 | if not _xterm_term_title_saved: | |
|
84 | warnings.warn( | |
|
85 | "Expecting xterm_term_title_saved to be True, but is not; will not restore terminal title.", | |
|
86 | stacklevel=1, | |
|
87 | ) | |
|
88 | return | |
|
89 | ||
|
84 | 90 | sys.stdout.write('\033[23;0t') |
|
85 | 91 | _xterm_term_title_saved = False |
|
86 | 92 | |
|
87 | 93 | |
|
88 | 94 | if os.name == 'posix': |
|
89 | 95 | TERM = os.environ.get('TERM','') |
|
90 | 96 | if TERM.startswith('xterm'): |
|
91 | 97 | _set_term_title = _set_term_title_xterm |
|
92 | 98 | _restore_term_title = _restore_term_title_xterm |
|
93 | 99 | elif sys.platform == 'win32': |
|
94 | 100 | import ctypes |
|
95 | 101 | |
|
96 | 102 | SetConsoleTitleW = ctypes.windll.kernel32.SetConsoleTitleW |
|
97 | 103 | SetConsoleTitleW.argtypes = [ctypes.c_wchar_p] |
|
98 | 104 | |
|
99 | 105 | def _set_term_title(title): |
|
100 | 106 | """Set terminal title using ctypes to access the Win32 APIs.""" |
|
101 | 107 | SetConsoleTitleW(title) |
|
102 | 108 | |
|
103 | 109 | |
|
104 | 110 | def set_term_title(title): |
|
105 | 111 | """Set terminal title using the necessary platform-dependent calls.""" |
|
106 | 112 | if ignore_termtitle: |
|
107 | 113 | return |
|
108 | 114 | _set_term_title(title) |
|
109 | 115 | |
|
110 | 116 | |
|
111 | 117 | def restore_term_title(): |
|
112 | 118 | """Restore, if possible, terminal title to the original state""" |
|
113 | 119 | if ignore_termtitle: |
|
114 | 120 | return |
|
115 | 121 | _restore_term_title() |
|
116 | 122 | |
|
117 | 123 | |
|
118 | 124 | def freeze_term_title(): |
|
119 | 125 | warnings.warn("This function is deprecated, use toggle_set_term_title()") |
|
120 | 126 | global ignore_termtitle |
|
121 | 127 | ignore_termtitle = True |
|
122 | 128 | |
|
123 | 129 | |
|
124 | 130 | def get_terminal_size(defaultx=80, defaulty=25): |
|
125 | 131 | return _get_terminal_size((defaultx, defaulty)) |
General Comments 0
You need to be logged in to leave comments.
Login now