-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconsole.py
More file actions
342 lines (291 loc) · 11.7 KB
/
Copy pathconsole.py
File metadata and controls
342 lines (291 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#
# Functions for manipulating the console using Microsoft's Console API
#
import ctypes, sys, locale, time
from ctypes import Structure, Union, c_int, c_long, c_char, c_wchar, c_short, pointer, byref
from ctypes.wintypes import BOOL, WORD, DWORD
from win32console import GetStdHandle, STD_INPUT_HANDLE, PyINPUT_RECORDType, KEY_EVENT
from win32con import LEFT_CTRL_PRESSED, RIGHT_CTRL_PRESSED
from win32con import LEFT_ALT_PRESSED, RIGHT_ALT_PRESSED
from win32con import SHIFT_PRESSED
global FOREGROUND_RED
global FOREGROUND_GREEN
global FOREGROUND_BLUE
global FOREGROUND_WHITE
global FOREGROUND_BRIGHT
global BACKGROUND_RED
global BACKGROUND_GREEN
global BACKGROUND_BLUE
global BACKGROUND_BRIGHT
global stdout_handle
global stdin_handle
class COORD(Structure):
_fields_ = [('X', c_short),
('Y', c_short)]
class SMALL_RECT(Structure):
_fields_ = [('Left', c_short),
('Top', c_short),
('Right', c_short),
('Bottom', c_short)]
class CONSOLE_CURSOR_INFO(Structure):
_fields_ = [('size', c_int),
('visible', c_int)]
class CONSOLE_SCREEN_BUFFER_INFO(Structure):
_fields_ = [('size', COORD),
('cursorPosition', COORD),
('attributes', WORD),
('window', SMALL_RECT),
('maxWindowSize', COORD)]
class KEY_EVENT_RECORD(Structure):
_fields_ = [('keyDown', BOOL),
('repeatCount', WORD),
('virtualKeyCode', WORD),
('virtualScanCode', WORD),
('char', c_char),
('controlKeyState', DWORD)]
def get_text_attributes():
"""Get the current foreground/background RGB components"""
buffer_info = CONSOLE_SCREEN_BUFFER_INFO()
ctypes.windll.kernel32.GetConsoleScreenBufferInfo(stdout_handle, pointer(buffer_info))
return buffer_info.attributes
def set_text_attributes(color):
"""Set foreground/background RGB components for the text to write"""
ctypes.windll.kernel32.SetConsoleTextAttribute(stdout_handle, color)
def get_buffer_attributes(x, y, n):
"""Get the fg/bg/attributes for the n chars in the buffer starting at (x, y)"""
colors = (n * WORD)()
coord = COORD(x, y)
read = DWORD(0)
ctypes.windll.kernel32.ReadConsoleOutputAttribute(stdout_handle, colors, n, coord, pointer(read))
return colors
def set_buffer_attributes(x, y, colors):
"""Set the fg/bg attributes for the n chars in the the buffer starting at (x, y)"""
coord = COORD(x, y)
written = DWORD(0)
ctypes.windll.kernel32.WriteConsoleOutputAttribute(stdout_handle, colors, len(colors), coord, pointer(written))
def visual_bell():
"""Flash the screen for brief moment to notify the user"""
l, t, r, b = get_viewport()
count = (r - l + 1) * (b - t + 1)
colors = get_buffer_attributes(l, t, count)
reverted_colors = (count * WORD)(*tuple([c ^ BACKGROUND_BRIGHT for c in colors]))
set_buffer_attributes(l, t, reverted_colors)
time.sleep(0.15)
set_buffer_attributes(l, t, colors)
def set_console_title(title):
"""Set the title of the current console"""
ctypes.windll.kernel32.SetConsoleTitleA(title)
def move_cursor(x, y):
"""Move the cursor to the specified location"""
location = COORD(x, y)
ctypes.windll.kernel32.SetConsoleCursorPosition(stdout_handle, location)
def get_cursor():
"""Get the current cursor position"""
buffer_info = CONSOLE_SCREEN_BUFFER_INFO()
ctypes.windll.kernel32.GetConsoleScreenBufferInfo(stdout_handle, pointer(buffer_info))
return (buffer_info.cursorPosition.X, buffer_info.cursorPosition.Y)
def get_buffer_size():
"""Get the size of the text buffer"""
buffer_info = CONSOLE_SCREEN_BUFFER_INFO()
ctypes.windll.kernel32.GetConsoleScreenBufferInfo(stdout_handle, pointer(buffer_info))
return (buffer_info.size.X, buffer_info.size.Y)
def get_viewport():
"""Get the current viewport position"""
buffer_info = CONSOLE_SCREEN_BUFFER_INFO()
ctypes.windll.kernel32.GetConsoleScreenBufferInfo(stdout_handle, pointer(buffer_info))
return (buffer_info.window.Left, buffer_info.window.Top, buffer_info.window.Right, buffer_info.window.Bottom)
def set_cursor_attributes(size, visibility):
"""Set the cursor size and visibility"""
cursor_info = CONSOLE_CURSOR_INFO(size, visibility)
ctypes.windll.kernel32.SetConsoleCursorInfo(stdout_handle, pointer(cursor_info))
def cursor_backward(count):
"""Move cursor backward with the given number of positions"""
(x, y) = get_cursor()
while count > 0:
x -= 1
if x < 0:
y -= 1
(x, _) = get_buffer_size()
x -= 1
count -= 1
move_cursor(x, y)
def scroll_buffer(lines):
"""Scroll vertically with the given (positive or negative) number of lines"""
global scroll_mark
(l, t, r, b) = get_viewport()
(w, h) = get_buffer_size()
if t + lines < 0:
lines = -t # Scroll up to beginning
elif b + lines > h:
lines = h - b - 1 # Scroll down to end
if (lines < 0 and t >= lines or lines > 0 and b + lines <= h):
info = SMALL_RECT(l, t + lines, r, b + lines)
ctypes.windll.kernel32.SetConsoleWindowInfo(stdout_handle, True, byref(info))
def read_input():
"""Read one input event from the console input buffer"""
while True:
record = stdin_handle.ReadConsoleInput(1)[0]
if record.EventType == KEY_EVENT and record.KeyDown:
return record
def write_input(key_code, control_state):
"""Emulate a key press with the given key code and control key mask"""
record = PyINPUT_RECORDType(KEY_EVENT)
record.KeyDown = True
record.VirtualKeyCode = key_code
record.ControlKeyState = control_state
stdin_handle.WriteConsoleInput([record])
def write_str(s):
"""
Output s to stdout (after encoding it with stdout encoding to
avoid conversion errors with non ASCII characters)
"""
def write_with_sane_cursor(s):
"""
Under Win10, write() no longer advances the cursor to the next line after writing in the last column; so we
use a custom function to restore that behavior when needed
"""
buffer_width = get_buffer_size()[0]
cursor_before = get_cursor()[0]
sys.__stdout__.write(s)
cursor_after = get_cursor()[0]
if (buffer_width > 0
and (cursor_before + len(s)) % buffer_width == 0
and cursor_after > 0):
# We have written over until the last column, but the cursor is NOT pushed to the next line; so we push it
# ourselves
sys.__stdout__.write(' \r')
if sys.__stdout__.encoding:
encoded_str = s.encode(sys.__stdout__.encoding, 'replace')
else:
encoded_str = s
i = 0
buf = ''
attr = get_text_attributes()
while i < len(encoded_str):
c = encoded_str[i]
if c == chr(27):
if buf:
# We have some characters, apply attributes and write them out
set_text_attributes(attr)
write_with_sane_cursor(buf)
buf = ''
# Process color commands to compute and set new attributes
target = encoded_str[i + 1]
command = encoded_str[i + 2]
component = encoded_str[i + 3]
i += 3
# Escape sequence format is [ESC][TGT][OP][COMP], where:
# * ESC is the Escape character: chr(27)
# * TGT is the target: 'F' for foreground, 'B' for background
# * OP is the operation: 'S' (set), 'C' (clear), 'T' (toggle) a component
# * COMP is the color component: 'R', 'G', 'B' or 'X' (bright)
if target == 'F':
name_prefix = 'FOREGROUND'
else:
name_prefix = 'BACKGROUND'
if component == 'R':
name_suffix = 'RED'
elif component == 'G':
name_suffix = 'GREEN'
elif component == 'B':
name_suffix = 'BLUE'
else:
name_suffix = 'BRIGHT'
if command == 'S':
operator = lambda x, y: x | y
elif command == 'C':
operator = lambda x, y: x & ~y
else:
operator = lambda x, y: x ^ y
import console
# We use the bit masks defined at the end of console.py by computing
# the name and accessing the module's dictionary (FOREGROUND_RED,
# BACKGROUND_BRIGHT etc)
bit_mask = console.__dict__[name_prefix + '_' + name_suffix]
attr = operator(attr, bit_mask)
else:
# Regular character, just append to the buffer
buf += c
i += 1
# Apply the last attributes and write the remaining chars (if any)
set_text_attributes(attr)
if buf:
write_with_sane_cursor(buf)
def remove_escape_sequences(s):
"""
Remove color escape sequences from the given string
"""
from pycmd_public import color
escape_sequences_fore = [v for (k, v) in color.Fore.__dict__.items() + color.Back.__dict__.items()
if not k in ['__dict__', '__doc__', '__weakref__', '__module__']]
return reduce(lambda x, y: x.replace(y, ''),
escape_sequences_fore,
s)
def get_current_foreground():
"""Get the current foreground setting as a color string"""
color = ''
attr = get_text_attributes()
letters = ['B', 'G', 'R', 'X']
for i in range(4):
if attr & 1 << i:
color += chr(27) + 'FS' + letters[i]
else:
color += chr(27) + 'FC' + letters[i]
return color
def get_current_background():
"""Get the current background setting as a color string"""
color = ''
attr = get_text_attributes()
letters = ['B', 'G', 'R', 'X']
for i in range(4):
if attr & 1 << (i + 4):
color += chr(27) + 'BS' + letters[i]
else:
color += chr(27) + 'BC' + letters[i]
return color
def is_ctrl_pressed(record):
"""Check whether the Ctrl key is pressed"""
return record.ControlKeyState & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED) != 0
def is_alt_pressed(record):
"""Check whether the Alt key is pressed"""
return record.ControlKeyState & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED) != 0
def is_shift_pressed(record):
"""Check whether the Shift key is pressed"""
return record.ControlKeyState & SHIFT_PRESSED != 0
def is_control_only(record):
"""
Check whether this is a control-key-only press, i.e. just a modifier
key w/out an "actual" key
"""
return record.VirtualKeyCode in [16, 17, 18]
# Initialization
FOREGROUND_BLUE = 0x01
FOREGROUND_GREEN = 0x02
FOREGROUND_RED = 0x04
FOREGROUND_WHITE = FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED
FOREGROUND_BRIGHT = 0x08
BACKGROUND_BLUE = 0x10
BACKGROUND_GREEN = 0x20
BACKGROUND_RED = 0x40
BACKGROUND_BRIGHT = 0x80
BACKGROUND_WHITE = BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED
stdin_handle = GetStdHandle(STD_INPUT_HANDLE)
stdout_handle = ctypes.windll.kernel32.GetStdHandle(-11)
class ColorOutputStream:
"""
We install a custom sys.stdout that handles:
* our color sequences
* string encoding
Note that this requires sys.stdout be only imported _after_ console;
not doing so will bring the original stdout in the current scope!
"""
encoding = sys.__stdout__.encoding
def write(self, str):
"""Dispatch printing to our enhanced write function"""
write_str(str)
# Set default encoding to the system's locale; this needs to be done here,
# before we install the custom output stream (since we reload(sys))
reload(sys)
sys.setdefaultencoding(locale.getdefaultlocale()[1])
# Install our custom output stream
sys.stdout = ColorOutputStream()