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