Show More
@@ -1,162 +1,162 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 | #----------------------------------------------------------------------------- |
|
12 | #----------------------------------------------------------------------------- | |
13 | # Copyright (C) 2008-2011 The IPython Development Team |
|
13 | # Copyright (C) 2008-2011 The IPython Development Team | |
14 | # |
|
14 | # | |
15 | # Distributed under the terms of the BSD License. The full license is in |
|
15 | # Distributed under the terms of the BSD License. The full license is in | |
16 | # the file COPYING, distributed as part of this software. |
|
16 | # the file COPYING, distributed as part of this software. | |
17 | #----------------------------------------------------------------------------- |
|
17 | #----------------------------------------------------------------------------- | |
18 |
|
18 | |||
19 | #----------------------------------------------------------------------------- |
|
19 | #----------------------------------------------------------------------------- | |
20 | # Imports |
|
20 | # Imports | |
21 | #----------------------------------------------------------------------------- |
|
21 | #----------------------------------------------------------------------------- | |
22 |
|
22 | |||
23 | import os |
|
23 | import os | |
24 | import struct |
|
24 | import struct | |
25 | import sys |
|
25 | import sys | |
26 | import warnings |
|
26 | import warnings | |
27 |
|
27 | |||
28 | #----------------------------------------------------------------------------- |
|
28 | #----------------------------------------------------------------------------- | |
29 | # Code |
|
29 | # Code | |
30 | #----------------------------------------------------------------------------- |
|
30 | #----------------------------------------------------------------------------- | |
31 |
|
31 | |||
32 | # This variable is part of the expected API of the module: |
|
32 | # This variable is part of the expected API of the module: | |
33 | ignore_termtitle = True |
|
33 | ignore_termtitle = True | |
34 |
|
34 | |||
35 |
|
35 | |||
36 | def _term_clear(): |
|
36 | def _term_clear(): | |
37 | pass |
|
37 | pass | |
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 |
|
43 | |||
44 |
|
44 | |||
45 | if sys.platform == 'win32': |
|
45 | if sys.platform == 'win32': | |
46 | def _term_clear(): |
|
46 | def _term_clear(): | |
47 | os.system('cls') |
|
47 | os.system('cls') | |
48 |
|
48 | |||
49 |
|
49 | |||
50 | def term_clear(): |
|
50 | def term_clear(): | |
51 | _term_clear() |
|
51 | _term_clear() | |
52 |
|
52 | |||
53 |
|
53 | |||
54 | def toggle_set_term_title(val): |
|
54 | def toggle_set_term_title(val): | |
55 | """Control whether set_term_title is active or not. |
|
55 | """Control whether set_term_title is active or not. | |
56 |
|
56 | |||
57 | set_term_title() allows writing to the console titlebar. In embedded |
|
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 |
|
58 | widgets this can cause problems, so this call can be used to toggle it on | |
59 | or off as needed. |
|
59 | or off as needed. | |
60 |
|
60 | |||
61 | The default state of the module is for the function to be disabled. |
|
61 | The default state of the module is for the function to be disabled. | |
62 |
|
62 | |||
63 | Parameters |
|
63 | Parameters | |
64 | ---------- |
|
64 | ---------- | |
65 | val : bool |
|
65 | val : bool | |
66 | If True, set_term_title() actually writes to the terminal (using the |
|
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. |
|
67 | appropriate platform-specific module). If False, it is a no-op. | |
68 | """ |
|
68 | """ | |
69 | global ignore_termtitle |
|
69 | global ignore_termtitle | |
70 | ignore_termtitle = not(val) |
|
70 | ignore_termtitle = not(val) | |
71 |
|
71 | |||
72 |
|
72 | |||
73 | def _set_term_title(*args,**kw): |
|
73 | def _set_term_title(*args,**kw): | |
74 | """Dummy no-op.""" |
|
74 | """Dummy no-op.""" | |
75 | pass |
|
75 | pass | |
76 |
|
76 | |||
77 |
|
77 | |||
78 | def _set_term_title_xterm(title): |
|
78 | def _set_term_title_xterm(title): | |
79 | """ Change virtual terminal title in xterm-workalikes """ |
|
79 | """ Change virtual terminal title in xterm-workalikes """ | |
80 | sys.stdout.write('\033]0;%s\007' % title) |
|
80 | sys.stdout.write('\033]0;%s\007' % title) | |
81 |
|
81 | |||
82 | if os.name == 'posix': |
|
82 | if os.name == 'posix': | |
83 | TERM = os.environ.get('TERM','') |
|
83 | TERM = os.environ.get('TERM','') | |
84 | if (TERM == 'xterm') or (TERM == 'xterm-color'): |
|
84 | if TERM.startswith('xterm'): | |
85 | _set_term_title = _set_term_title_xterm |
|
85 | _set_term_title = _set_term_title_xterm | |
86 |
|
86 | |||
87 |
|
87 | |||
88 | if sys.platform == 'win32': |
|
88 | if sys.platform == 'win32': | |
89 | try: |
|
89 | try: | |
90 | import ctypes |
|
90 | import ctypes | |
91 |
|
91 | |||
92 | SetConsoleTitleW = ctypes.windll.kernel32.SetConsoleTitleW |
|
92 | SetConsoleTitleW = ctypes.windll.kernel32.SetConsoleTitleW | |
93 | SetConsoleTitleW.argtypes = [ctypes.c_wchar_p] |
|
93 | SetConsoleTitleW.argtypes = [ctypes.c_wchar_p] | |
94 |
|
94 | |||
95 | def _set_term_title(title): |
|
95 | def _set_term_title(title): | |
96 | """Set terminal title using ctypes to access the Win32 APIs.""" |
|
96 | """Set terminal title using ctypes to access the Win32 APIs.""" | |
97 | SetConsoleTitleW(title) |
|
97 | SetConsoleTitleW(title) | |
98 | except ImportError: |
|
98 | except ImportError: | |
99 | def _set_term_title(title): |
|
99 | def _set_term_title(title): | |
100 | """Set terminal title using the 'title' command.""" |
|
100 | """Set terminal title using the 'title' command.""" | |
101 | global ignore_termtitle |
|
101 | global ignore_termtitle | |
102 |
|
102 | |||
103 | try: |
|
103 | try: | |
104 | # Cannot be on network share when issuing system commands |
|
104 | # Cannot be on network share when issuing system commands | |
105 | curr = os.getcwdu() |
|
105 | curr = os.getcwdu() | |
106 | os.chdir("C:") |
|
106 | os.chdir("C:") | |
107 | ret = os.system("title " + title) |
|
107 | ret = os.system("title " + title) | |
108 | finally: |
|
108 | finally: | |
109 | os.chdir(curr) |
|
109 | os.chdir(curr) | |
110 | if ret: |
|
110 | if ret: | |
111 | # non-zero return code signals error, don't try again |
|
111 | # non-zero return code signals error, don't try again | |
112 | ignore_termtitle = True |
|
112 | ignore_termtitle = True | |
113 |
|
113 | |||
114 |
|
114 | |||
115 | def set_term_title(title): |
|
115 | def set_term_title(title): | |
116 | """Set terminal title using the necessary platform-dependent calls.""" |
|
116 | """Set terminal title using the necessary platform-dependent calls.""" | |
117 | if ignore_termtitle: |
|
117 | if ignore_termtitle: | |
118 | return |
|
118 | return | |
119 | _set_term_title(title) |
|
119 | _set_term_title(title) | |
120 |
|
120 | |||
121 |
|
121 | |||
122 | def freeze_term_title(): |
|
122 | def freeze_term_title(): | |
123 | warnings.warn("This function is deprecated, use toggle_set_term_title()") |
|
123 | warnings.warn("This function is deprecated, use toggle_set_term_title()") | |
124 | global ignore_termtitle |
|
124 | global ignore_termtitle | |
125 | ignore_termtitle = True |
|
125 | ignore_termtitle = True | |
126 |
|
126 | |||
127 |
|
127 | |||
128 | def get_terminal_size(defaultx=80, defaulty=25): |
|
128 | def get_terminal_size(defaultx=80, defaulty=25): | |
129 | return defaultx, defaulty |
|
129 | return defaultx, defaulty | |
130 |
|
130 | |||
131 |
|
131 | |||
132 | if sys.platform == 'win32': |
|
132 | if sys.platform == 'win32': | |
133 | def get_terminal_size(defaultx=80, defaulty=25): |
|
133 | def get_terminal_size(defaultx=80, defaulty=25): | |
134 | """Return size of current terminal console. |
|
134 | """Return size of current terminal console. | |
135 |
|
135 | |||
136 | This function try to determine actual size of current working |
|
136 | This function try to determine actual size of current working | |
137 | console window and return tuple (sizex, sizey) if success, |
|
137 | console window and return tuple (sizex, sizey) if success, | |
138 | or default size (defaultx, defaulty) otherwise. |
|
138 | or default size (defaultx, defaulty) otherwise. | |
139 |
|
139 | |||
140 | Dependencies: ctypes should be installed. |
|
140 | Dependencies: ctypes should be installed. | |
141 |
|
141 | |||
142 | Author: Alexander Belchenko (e-mail: bialix AT ukr.net) |
|
142 | Author: Alexander Belchenko (e-mail: bialix AT ukr.net) | |
143 | """ |
|
143 | """ | |
144 | try: |
|
144 | try: | |
145 | import ctypes |
|
145 | import ctypes | |
146 | except ImportError: |
|
146 | except ImportError: | |
147 | return defaultx, defaulty |
|
147 | return defaultx, defaulty | |
148 |
|
148 | |||
149 | h = ctypes.windll.kernel32.GetStdHandle(-11) |
|
149 | h = ctypes.windll.kernel32.GetStdHandle(-11) | |
150 | csbi = ctypes.create_string_buffer(22) |
|
150 | csbi = ctypes.create_string_buffer(22) | |
151 | res = ctypes.windll.kernel32.GetConsoleScreenBufferInfo(h, csbi) |
|
151 | res = ctypes.windll.kernel32.GetConsoleScreenBufferInfo(h, csbi) | |
152 |
|
152 | |||
153 | if res: |
|
153 | if res: | |
154 | (bufx, bufy, curx, cury, wattr, |
|
154 | (bufx, bufy, curx, cury, wattr, | |
155 | left, top, right, bottom, maxx, maxy) = struct.unpack( |
|
155 | left, top, right, bottom, maxx, maxy) = struct.unpack( | |
156 | "hhhhHhhhhhh", csbi.raw) |
|
156 | "hhhhHhhhhhh", csbi.raw) | |
157 | sizex = right - left + 1 |
|
157 | sizex = right - left + 1 | |
158 | sizey = bottom - top + 1 |
|
158 | sizey = bottom - top + 1 | |
159 | return (sizex, sizey) |
|
159 | return (sizex, sizey) | |
160 | else: |
|
160 | else: | |
161 | return (defaultx, defaulty) |
|
161 | return (defaultx, defaulty) | |
162 |
|
162 |
General Comments 0
You need to be logged in to leave comments.
Login now