Show More
@@ -1,121 +1,120 | |||||
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 struct |
|
|||
17 | import sys |
|
16 | import sys | |
18 | import warnings |
|
17 | import warnings | |
19 | try: |
|
18 | try: | |
20 | from shutil import get_terminal_size as _get_terminal_size |
|
19 | from shutil import get_terminal_size as _get_terminal_size | |
21 | except ImportError: |
|
20 | except ImportError: | |
22 | # use backport on Python 2 |
|
21 | # use backport on Python 2 | |
23 | from backports.shutil_get_terminal_size import get_terminal_size as _get_terminal_size |
|
22 | from backports.shutil_get_terminal_size import get_terminal_size as _get_terminal_size | |
24 |
|
23 | |||
25 | from . import py3compat |
|
24 | from . import py3compat | |
26 |
|
25 | |||
27 | #----------------------------------------------------------------------------- |
|
26 | #----------------------------------------------------------------------------- | |
28 | # Code |
|
27 | # Code | |
29 | #----------------------------------------------------------------------------- |
|
28 | #----------------------------------------------------------------------------- | |
30 |
|
29 | |||
31 | # This variable is part of the expected API of the module: |
|
30 | # This variable is part of the expected API of the module: | |
32 | ignore_termtitle = True |
|
31 | ignore_termtitle = True | |
33 |
|
32 | |||
34 |
|
33 | |||
35 |
|
34 | |||
36 | if os.name == 'posix': |
|
35 | if os.name == 'posix': | |
37 | def _term_clear(): |
|
36 | def _term_clear(): | |
38 | os.system('clear') |
|
37 | os.system('clear') | |
39 | elif sys.platform == 'win32': |
|
38 | elif sys.platform == 'win32': | |
40 | def _term_clear(): |
|
39 | def _term_clear(): | |
41 | os.system('cls') |
|
40 | os.system('cls') | |
42 | else: |
|
41 | else: | |
43 | def _term_clear(): |
|
42 | def _term_clear(): | |
44 | pass |
|
43 | pass | |
45 |
|
44 | |||
46 |
|
45 | |||
47 |
|
46 | |||
48 | def toggle_set_term_title(val): |
|
47 | def toggle_set_term_title(val): | |
49 | """Control whether set_term_title is active or not. |
|
48 | """Control whether set_term_title is active or not. | |
50 |
|
49 | |||
51 | set_term_title() allows writing to the console titlebar. In embedded |
|
50 | set_term_title() allows writing to the console titlebar. In embedded | |
52 | widgets this can cause problems, so this call can be used to toggle it on |
|
51 | widgets this can cause problems, so this call can be used to toggle it on | |
53 | or off as needed. |
|
52 | or off as needed. | |
54 |
|
53 | |||
55 | The default state of the module is for the function to be disabled. |
|
54 | The default state of the module is for the function to be disabled. | |
56 |
|
55 | |||
57 | Parameters |
|
56 | Parameters | |
58 | ---------- |
|
57 | ---------- | |
59 | val : bool |
|
58 | val : bool | |
60 | If True, set_term_title() actually writes to the terminal (using the |
|
59 | If True, set_term_title() actually writes to the terminal (using the | |
61 | appropriate platform-specific module). If False, it is a no-op. |
|
60 | appropriate platform-specific module). If False, it is a no-op. | |
62 | """ |
|
61 | """ | |
63 | global ignore_termtitle |
|
62 | global ignore_termtitle | |
64 | ignore_termtitle = not(val) |
|
63 | ignore_termtitle = not(val) | |
65 |
|
64 | |||
66 |
|
65 | |||
67 | def _set_term_title(*args,**kw): |
|
66 | def _set_term_title(*args,**kw): | |
68 | """Dummy no-op.""" |
|
67 | """Dummy no-op.""" | |
69 | pass |
|
68 | pass | |
70 |
|
69 | |||
71 |
|
70 | |||
72 | def _set_term_title_xterm(title): |
|
71 | def _set_term_title_xterm(title): | |
73 | """ Change virtual terminal title in xterm-workalikes """ |
|
72 | """ Change virtual terminal title in xterm-workalikes """ | |
74 | sys.stdout.write('\033]0;%s\007' % title) |
|
73 | sys.stdout.write('\033]0;%s\007' % title) | |
75 |
|
74 | |||
76 | if os.name == 'posix': |
|
75 | if os.name == 'posix': | |
77 | TERM = os.environ.get('TERM','') |
|
76 | TERM = os.environ.get('TERM','') | |
78 | if TERM.startswith('xterm'): |
|
77 | if TERM.startswith('xterm'): | |
79 | _set_term_title = _set_term_title_xterm |
|
78 | _set_term_title = _set_term_title_xterm | |
80 | elif sys.platform == 'win32': |
|
79 | elif sys.platform == 'win32': | |
81 | try: |
|
80 | try: | |
82 | import ctypes |
|
81 | import ctypes | |
83 |
|
82 | |||
84 | SetConsoleTitleW = ctypes.windll.kernel32.SetConsoleTitleW |
|
83 | SetConsoleTitleW = ctypes.windll.kernel32.SetConsoleTitleW | |
85 | SetConsoleTitleW.argtypes = [ctypes.c_wchar_p] |
|
84 | SetConsoleTitleW.argtypes = [ctypes.c_wchar_p] | |
86 |
|
85 | |||
87 | def _set_term_title(title): |
|
86 | def _set_term_title(title): | |
88 | """Set terminal title using ctypes to access the Win32 APIs.""" |
|
87 | """Set terminal title using ctypes to access the Win32 APIs.""" | |
89 | SetConsoleTitleW(title) |
|
88 | SetConsoleTitleW(title) | |
90 | except ImportError: |
|
89 | except ImportError: | |
91 | def _set_term_title(title): |
|
90 | def _set_term_title(title): | |
92 | """Set terminal title using the 'title' command.""" |
|
91 | """Set terminal title using the 'title' command.""" | |
93 | global ignore_termtitle |
|
92 | global ignore_termtitle | |
94 |
|
93 | |||
95 | try: |
|
94 | try: | |
96 | # Cannot be on network share when issuing system commands |
|
95 | # Cannot be on network share when issuing system commands | |
97 | curr = py3compat.getcwd() |
|
96 | curr = py3compat.getcwd() | |
98 | os.chdir("C:") |
|
97 | os.chdir("C:") | |
99 | ret = os.system("title " + title) |
|
98 | ret = os.system("title " + title) | |
100 | finally: |
|
99 | finally: | |
101 | os.chdir(curr) |
|
100 | os.chdir(curr) | |
102 | if ret: |
|
101 | if ret: | |
103 | # non-zero return code signals error, don't try again |
|
102 | # non-zero return code signals error, don't try again | |
104 | ignore_termtitle = True |
|
103 | ignore_termtitle = True | |
105 |
|
104 | |||
106 |
|
105 | |||
107 | def set_term_title(title): |
|
106 | def set_term_title(title): | |
108 | """Set terminal title using the necessary platform-dependent calls.""" |
|
107 | """Set terminal title using the necessary platform-dependent calls.""" | |
109 | if ignore_termtitle: |
|
108 | if ignore_termtitle: | |
110 | return |
|
109 | return | |
111 | _set_term_title(title) |
|
110 | _set_term_title(title) | |
112 |
|
111 | |||
113 |
|
112 | |||
114 | def freeze_term_title(): |
|
113 | def freeze_term_title(): | |
115 | warnings.warn("This function is deprecated, use toggle_set_term_title()") |
|
114 | warnings.warn("This function is deprecated, use toggle_set_term_title()") | |
116 | global ignore_termtitle |
|
115 | global ignore_termtitle | |
117 | ignore_termtitle = True |
|
116 | ignore_termtitle = True | |
118 |
|
117 | |||
119 |
|
118 | |||
120 | def get_terminal_size(defaultx=80, defaulty=25): |
|
119 | def get_terminal_size(defaultx=80, defaulty=25): | |
121 | return _get_terminal_size((defaultx, defaulty)) |
|
120 | return _get_terminal_size((defaultx, defaulty)) |
General Comments 0
You need to be logged in to leave comments.
Login now