-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathESC.py
More file actions
514 lines (472 loc) · 13.9 KB
/
ESC.py
File metadata and controls
514 lines (472 loc) · 13.9 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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
import os
import sys, termios
import time
class ForeGroundColor:
Black = 30
Red = 31
Green = 32
Yellow = 33
Blue = 34
Magenta = 35
Cyan = 36
LightGray = 37
DarkGray = 90
LightRed = 91
LightGreen = 92
LightYellow = 93
LightBlue = 94
LightMagenta = 95
LightCyan = 96
White = 97
class BackGroundColor:
Black = 40
Red = 41
Green = 42
Yellow = 43
Blue = 44
Magenta = 45
Cyan = 46
LightGray = 47
DarkGray = 100
LightRed = 101
LightGreen = 102
LightYellow = 103
LightBlue = 104
LightMagenta = 105
LightCyan = 106
White = 107
# Classes for solarized color scheme
class Solarized16:
Base02 = 30 # Schwarz
Red = 31 # Rot
Green = 32 # Grün
Yellow = 33 # Gelb
Blue = 34 # Blau
Magenta = 35 # Magenta
Cyan = 36 # Cyan
Base2 = 37 # Weiß
Base03 = 90 # brSchwarz
Orange = 91 # Grün
Base01 = 92 # Gelb
Base00 = 93 # Cyan
Base0 = 94 # Hellgrau
Violet = 95 # Gelb
Base1 = 96 # Rot
Base3 = 97 # Magenta
def KeyToFunction(key):
escKeys ={
'\x1bOP': 'F1',
'\x1bOQ': 'F2',
'\x1bOR': 'F3',
'\x1bOS': 'F4',
'\x1b[15~': 'F5',
'\x1b[17~': 'F6',
'\x1b[18~': 'F7',
'\x1b[19~': 'F8',
'\x1b[20~': 'F9',
'\x1b[21~': 'F10',
'\x1b[23~': 'F11',
'\x1b[24~': 'F12',
'\x1b[1;2P': 'Shift-F1',
'\x1b[1;2Q': 'Shift-F2',
'\x1b[1;2R': 'Shift-F3',
'\x1b[1;2S': 'Shift-F4',
'\x1b[15;2~': 'Shift-F5',
'\x1b[17;2~': 'Shift-F6',
'\x1b[18;2~': 'Shift-F7',
'\x1b[19;2~': 'Shift-F8',
'\x1b[20;2~': 'Shift-F9',
'\x1b[21;2~': 'Shift-F10',
'\x1b[23;2~': 'Shift-F11',
'\x1b[24;2~': 'Shift-F12',
'\x1b[1;5P': 'Ctrl-F1',
'\x1b[1;5Q': 'Ctrl-F2',
'\x1b[1;5R': 'Ctrl-F3',
'\x1b[1;5S': 'Ctrl-F4',
'\x1b[15;5~': 'Ctrl-F5',
'\x1b[17;5~': 'Ctrl-F6',
'\x1b[18;5~': 'Ctrl-F7',
'\x1b[19;5~': 'Ctrl-F8',
'\x1b[20;5~': 'Ctrl-F9',
'\x1b[21;5~': 'Ctrl-F10',
'\x1b[23;5~': 'Ctrl-F11',
'\x1b[24;5~': 'Ctrl-F12',
'\x1b[1;6P': 'Ctrl-Shift-F1',
'\x1b[1;6Q': 'Ctrl-Shift-F2',
'\x1b[1;6R': 'Ctrl-Shift-F3',
'\x1b[1;6S': 'Ctrl-Shift-F4',
'\x1b[15;6~': 'Ctrl-Shift-F5',
'\x1b[17;6~': 'Ctrl-Shift-F6',
'\x1b[18;6~': 'Ctrl-Shift-F7',
'\x1b[19;6~': 'Ctrl-Shift-F8',
'\x1b[20;6~': 'Ctrl-Shift-F9',
'\x1b[21;6~': 'Ctrl-Shift-F10',
'\x1b[23;6~': 'Ctrl-Shift-F11',
'\x1b[24;6~': 'Ctrl-Shift-F12',
'\x1b[1;3P': 'Alt-F1',
'\x1b[1;3Q': 'Alt-F2',
'\x1b[1;3R': 'Alt-F3',
'\x1b[1;3S': 'Alt-F4',
'\x1b[15;3~': 'Alt-F5',
'\x1b[17;3~': 'Alt-F6',
'\x1b[18;3~': 'Alt-F7',
'\x1b[19;3~': 'Alt-F8',
'\x1b[20;3~': 'Alt-F9',
'\x1b[21;3~': 'Alt-F10',
'\x1b[23;3~': 'Alt-F11',
'\x1b[24;3~': 'Alt-F12',
'\x1b[1;4P': 'Alt-Shift-F1',
'\x1b[1;4Q': 'Alt-Shift-F2',
'\x1b[1;4R': 'Alt-Shift-F3',
'\x1b[1;4S': 'Alt-Shift-F4',
'\x1b[15;4~': 'Alt-Shift-F5',
'\x1b[17;4~': 'Alt-Shift-F6',
'\x1b[18;4~': 'Alt-Shift-F7',
'\x1b[19;4~': 'Alt-Shift-F8',
'\x1b[20;4~': 'Alt-Shift-F9',
'\x1b[21;4~': 'Alt-Shift-F10',
'\x1b[23;4~': 'Alt-Shift-F11',
'\x1b[24;4~': 'Alt-Shift-F12',
'\x1b[1;7P': 'Alt-Ctrl-F1',
'\x1b[1;7Q': 'Alt-Ctrl-F2',
'\x1b[1;7R': 'Alt-Ctrl-F3',
'\x1b[1;7S': 'Alt-Ctrl-F4',
'\x1b[15;7~': 'Alt-Ctrl-F5',
'\x1b[17;7~': 'Alt-Ctrl-F6',
'\x1b[18;7~': 'Alt-Ctrl-F7',
'\x1b[19;7~': 'Alt-Ctrl-F8',
'\x1b[20;7~': 'Alt-Ctrl-F9',
'\x1b[21;7~': 'Alt-Ctrl-F10',
'\x1b[23;7~': 'Alt-Ctrl-F11',
'\x1b[24;7~': 'Alt-Ctrl-F12',
'\x1b[A': 'Up',
'\x1b[B': 'Down',
'\x1b[C': 'Right',
'\x1b[D': 'Left',
'\x1b[E': 'Center',
'\x1b[F': 'End',
'\x1b[H': 'Home',
'\x1b[Z': 'Shift-Tab',
'\x1b[1;2A':'Shift-Up',
'\x1b[1;2B':'Shift-Down',
'\x1b[1;2C':'Shift-Right',
'\x1b[1;2D':'Shift-Left',
'\x1b[1;2E':'Shift-Center',
'\x1b[1;2F':'Shift-End',
'\x1b[1;2H':'Shift-Home',
'\x1b[1;3A':'Alt-Up',
'\x1b[1;3B':'Alt-Down',
'\x1b[1;3C':'Alt-Right',
'\x1b[1;3D':'Alt-Left',
'\x1b[1;3E':'Alt-Center',
'\x1b[1;3F':'Alt-End',
'\x1b[1;3H':'Alt-Home',
'\x1b[1;5A':'Ctrl-Up',
'\x1b[1;5B':'Ctrl-Down',
'\x1b[1;5C':'Ctrl-Right',
'\x1b[1;5D':'Ctrl-Left',
'\x1b[1;5E':'Ctrl-Center',
'\x1b[1;5F':'Ctrl-End',
'\x1b[1;5H':'Ctrl-Home',
'\x7F': 'Back',
'\x08': 'Back',
'\x1b\x08': 'Alt-Back',
'\x1b[3~': 'Del',
'\x1b[2~': 'Ins',
'\x1b[5~': 'PgUp',
'\x1b[6~': 'PgDn',
'\x09': 'Tab',
'\x1b': 'Esc',
'\x0A': 'Enter',
'\x0D': 'Enter',
'\x0D\x0A': 'Enter',
'\x1b[2;2~': 'Shift-Ins',
'\x1b[3;2~': 'Shift-Del',
'\x1b[5;2~': 'Shift-PgUp',
'\x1b[6;2~': 'Shift-PgDn',
'\x1b[2;5~': 'Ctrl-Ins',
'\x1b[3;5~': 'Ctrl-Del',
'\x1b[5;5~': 'Ctrl-PgUp',
'\x1b[6;5~': 'Ctrl-PgDn',
'\x1b[2;3~': 'Alt-Ins',
'\x1b[3;3~': 'Alt-Del',
'\x1b[5;3~': 'Alt-PgUp',
'\x1b[6;3~': 'Alt-PgDn',
'\x1ba': 'Alt-a',
'\x1bb': 'Alt-b',
'\x1bc': 'Alt-c',
'\x1bd': 'Alt-d',
'\x1be': 'Alt-e',
'\x1bf': 'Alt-f',
'\x1bg': 'Alt-g',
'\x1bh': 'Alt-h',
'\x1bi': 'Alt-i',
'\x1bj': 'Alt-j',
'\x1bk': 'Alt-k',
'\x1bl': 'Alt-l',
'\x1bm': 'Alt-m',
'\x1bn': 'Alt-n',
'\x1bo': 'Alt-o',
'\x1bp': 'Alt-p',
'\x1bq': 'Alt-q',
'\x1br': 'Alt-r',
'\x1bs': 'Alt-s',
'\x1bt': 'Alt-t',
'\x1bu': 'Alt-u',
'\x1bv': 'Alt-v',
'\x1bw': 'Alt-w',
'\x1bx': 'Alt-x',
'\x1by': 'Alt-y',
'\x1bz': 'Alt-z',
'\x1bA': 'Shift-Alt-A',
'\x1bB': 'Shift-Alt-B',
'\x1bC': 'Shift-Alt-C',
'\x1bD': 'Shift-Alt-D',
'\x1bE': 'Shift-Alt-E',
'\x1bF': 'Shift-Alt-F',
'\x1bG': 'Shift-Alt-G',
'\x1bH': 'Shift-Alt-H',
'\x1bI': 'Shift-Alt-I',
'\x1bJ': 'Shift-Alt-J',
'\x1bK': 'Shift-Alt-K',
'\x1bL': 'Shift-Alt-L',
'\x1bM': 'Shift-Alt-M',
'\x1bN': 'Shift-Alt-N',
'\x1bO': 'Shift-Alt-O',
'\x1bP': 'Shift-Alt-P',
'\x1bQ': 'Shift-Alt-Q',
'\x1bR': 'Shift-Alt-R',
'\x1bS': 'Shift-Alt-S',
'\x1bT': 'Shift-Alt-T',
'\x1bU': 'Shift-Alt-U',
'\x1bV': 'Shift-Alt-V',
'\x1bW': 'Shift-Alt-W',
'\x1bX': 'Shift-Alt-X',
'\x1bY': 'Shift-Alt-Y',
'\x1bZ': 'Shift-Alt-Z',
'\x1b0': 'Alt-0',
'\x1b1': 'Alt-1',
'\x1b2': 'Alt-2',
'\x1b3': 'Alt-3',
'\x1b4': 'Alt-4',
'\x1b5': 'Alt-5',
'\x1b6': 'Alt-6',
'\x1b7': 'Alt-7',
'\x1b8': 'Alt-8',
'\x1b9': 'Alt-9',
}
if key in escKeys:
return escKeys[key]
else:
if key.startswith('\x1b'):
return 'ESC+(' + key[1:] + ' : ' + str(ord(key[1])) + ')'
elif len(key):
return key
else:
return ''
def GetKey():
# idea from: https://stackoverflow.com/questions/71801157/detect-key-press-in-python-without-running-as-root-and-without-blockingioerror
fd = sys.stdin
oldterm = termios.tcgetattr(fd)
newattr = termios.tcgetattr(fd)
newattr[3] = newattr[3] & ~termios.ICANON
newattr[3] = newattr[3] & ~termios.ECHO
newattr[6][termios.VMIN] = 0
newattr[6][termios.VTIME] = 0
termios.tcsetattr(fd, termios.TCSANOW, newattr)
key = ''
try:
while True:
try:
c = sys.stdin.read(1)
if c:
key += c
else:
return KeyToFunction(key)
except:
return ''
finally:
termios.tcsetattr(fd, termios.TCSANOW, oldterm)
def edlin(strIN):
# edlin is a simple text editor for a single line
strSave = strIN
CursorSave() # save cursor position
pos = len(strIN) # pos of cursor
selStart = 0 # start of selection
selEnd = pos # end of selection
removeSelection = 0 # flag to remove selection
TxtInverse(1)
print(strIN, end="", flush=True)
TxtInverse(0)
while True:
removeSelection = 0
key = GetKey()
if key == "Left":
if pos > 0:
pos -= 1
selStart = pos
selEnd = pos
elif key == "Right":
if pos < len(strIN):
pos += 1
selStart = pos
selEnd = pos
elif key == "Home":
pos = 0
selStart = 0
selEnd = 0
elif key == "End":
pos = len(strIN)
selStart = pos
selEnd = pos
elif key == "Shift-Left":
if pos > 0:
if pos == selStart:
selStart -= 1
elif pos == selEnd:
selEnd -= 1
pos -= 1
elif key == "Shift-Right":
if pos == selEnd:
selEnd += 1
elif pos == selStart:
selStart += 1
pos += 1
elif key == "Shift-Home":
selEnd = pos
selStart = 0
pos = 0
elif key == "Shift-End":
selStart = pos
selEnd = len(strIN)
pos = len(strIN)
elif key == "Back":
if selStart == selEnd:
if pos > 0:
pos -= 1
selStart = pos
removeSelection = 1
elif key == "Del":
if selStart == selEnd:
if pos < len(strIN):
selEnd += 1
removeSelection = 1
elif key == "Enter":
return strIN
elif key == "Esc":
return strSave
elif len(key) == 1:
if selStart != selEnd:
removeSelection = 1
else:
time.sleep(0.05)
if key:
oldLen = len(strIN)
if removeSelection:
strIN = strIN[:selStart] + strIN[selEnd:]
pos = selStart
selEnd = pos
if len(key) == 1:
strIN = strIN[:selStart] + key + strIN[selEnd:]
pos += 1
selStart = pos
selEnd = pos
oldLen -= len(strIN)
CursorRestore()
print(strIN[:selStart], end="", flush=True)
TxtInverse(1)
print(strIN[selStart:selEnd], end="", flush=True)
TxtInverse(0)
print(strIN[selEnd:], end="", flush=True)
if oldLen > 0:
print(" " * oldLen, end="", flush=True)
CursorRestore()
CursorMoveX(pos)
def BreakLines(text, pos):
# Split a string into multiple lines, each with a maximum length of 'pos'
lines = []
doubleLF = 0
for line in text.splitlines():
while len(line) > pos:
space_index = line.rfind(' ', 0, pos)
if space_index == -1: # If no space found before the Xth character
space_index = pos # Break at the 60th character anyway
lines.append(line[:space_index].rstrip())
line = line[space_index:].lstrip()
if line: # Add any remaining part of the line
lines.append(line)
doubleLF = 0
else: # simulate original LineFeed
if not doubleLF: # prevent multiple LF
lines.append(" ")
doubleLF = 1
return '\n'.join(lines)
def PrintLines(text, offset = 0, cnt = 0):
# Print a string, line for line, with a given offset
cnt2 = 0
for line in text.splitlines():
cnt2 += 1
CursorRight(offset)
print(line)
if cnt == cnt2:
break
def CursorSave():
print("\0337", end="", flush=True)
def CursorRestore():
print("\0338", end="", flush=True)
def CursorDown(y):
print("\x1B[" + str(y) + "B", end="", flush=True)
def CursorRight(x):
print("\x1B[" + str(x) + "C", end="", flush=True)
def CursorLeft(x):
print("\x1B[" + str(x) + "D", end="", flush=True)
def CursorUp(y):
print("\x1B[" + str(y) + "A", end="", flush=True)
def CursorMoveX(x):
if x > 0:
CursorRight(x)
elif x < 0:
CursorLeft(x * -1)
def CursorMoveY(y):
if y > 0:
CursorUp(y)
elif y < 0:
CursorDown(y * -1)
def CursorMoveXY(x, y):
CursorMoveX(x)
CursorMoveY(y)
def CursorAbs(x, y):
print("\x1B[" + str(y) + ";" + str(x) + "H", end="", flush=True)
def CLS():
os.system('cls' if os.name == 'nt' else 'clear')
# print("\x1B[2J", end="", flush=True)
def TxtBold(set):
if set:
print("\x1B[1m", end="", flush=True)
else:
print("\x1B[22m", end="", flush=True)
def TxtInverse(set):
if set:
print("\x1B[7m", end="", flush=True)
else:
print("\x1B[27m", end="", flush=True)
def ResetForeGround():
print("\x1B[39m", end="", flush=True)
def ResetBackGround():
print("\x1B[49m", end="", flush=True)
def ResetForeBack():
ResetForeGround()
ResetBackGround()
def SetForeGround(c):
if not ((c > 29 and c < 38) or (c > 89 and c < 98)):
ResetForeGround()
else:
print("\x1B[" + str(c) + "m", end="", flush=True)
def SetBackGround(c):
if not ((c > 39 and c < 48) or (c > 99 and c < 108)):
ResetBackGround()
else:
print("\x1B[" + str(c) + "m", end="", flush=True)
def SetForeBack(fc, bc):
SetForeGround(fc)
SetBackGround(bc)