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