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