-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
413 lines (379 loc) · 15.1 KB
/
__init__.py
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
from thefuzz import process, utils, fuzz
from colorama import Fore, init
import msvcrt as m
import ctypes
import ctypes.wintypes as w
from time import time
init(autoreset=True)
commands = None
autoCompleteOnEnter = False
CF_UNICODETEXT = 13
u32 = ctypes.WinDLL('user32')
k32 = ctypes.WinDLL('kernel32')
OpenClipboard = u32.OpenClipboard
OpenClipboard.argtypes = w.HWND,
OpenClipboard.restype = w.BOOL
GetClipboardData = u32.GetClipboardData
GetClipboardData.argtypes = w.UINT,
GetClipboardData.restype = w.HANDLE
GlobalLock = k32.GlobalLock
GlobalLock.argtypes = w.HGLOBAL,
GlobalLock.restype = w.LPVOID
GlobalUnlock = k32.GlobalUnlock
GlobalUnlock.argtypes = w.HGLOBAL,
GlobalUnlock.restype = w.BOOL
CloseClipboard = u32.CloseClipboard
CloseClipboard.argtypes = None
CloseClipboard.restype = w.BOOL
def predict(text, lst=None):
text = text.lower()
test_lst = []
for l in lst:
if l.lower().startswith(text):
test_lst.append(l)
if test_lst:
lst = test_lst
if lst is None:
if commands is None:
return None, None
else:
list = commands
if utils.full_process(text):
pred, score = process.extractOne(text, lst, scorer=fuzz.token_sort_ratio)
return pred, score
else:
return None, None
def isCommand(text, command=None):
if type(command) == str:
command = [command]
if command is None:
if commands is None:
return False
else:
command = commands
for com in command:
if com.lower() == text.lower():
return True
else:
return False
def get_clip():
try:
OpenClipboard(None)
h_clip_mem = GetClipboardData(CF_UNICODETEXT)
text = ctypes.wstring_at(GlobalLock(h_clip_mem))
GlobalUnlock(h_clip_mem)
CloseClipboard()
return text
except:
GlobalUnlock(h_clip_mem)
CloseClipboard()
return ""
# def MoveYcur(y):
# if y > 0:
# print(f"\033[{y}A", end='\r')
# elif y < 0:
# print(f"\033[{abs(y)}B", end='\r')
def clear_console(pred=None, inp=None, lineDel=1):
if pred is None and inp is None:
print("\x1b[2K\r" + "\033[%d;A" % (lineDel), end="\r")
return
if len(pred.split("\n")) > len(inp.split("\n")):
text = pred
else:
text = inp
if (text is None) or (len(text.split("\n")) == 0):
print("\x1b[2K\r" + "\033[%d;A" % (1), end="\r")
else:
for i in range(len(text.split("\n"))-1):
print("\x1b[2K\r" + "\033[%d;A" % (1), end="\r")
def curVisible(isVisible=True):
if isVisible:
print('\033[?25h', end="")
else:
print('\033[?25l', end="")
def input(prefix=">> ", command=None, free=True, cursor=True, timer=True, timeInfo=None, secret=False, inp='', minLength=0, maxLength=0, iHelp=3, cursorVisibleTime=0.9, cursorNotVisibleTime=0.6):
if prefix is None:
prefix = ""
lastpred = ""
print(prefix, end=f' \b')
pred = ""
postfix = ""
curposx = 0
#curposy = 0
isprediction = True
isCleared = False
isSelected = False
helptabi = 0
s_time = time() - 0.1
scur_time = time()
if not cursor:
curisVis = False
else:
curisVis = True
curVisible(curisVis)
if timeInfo is None:
timeInfo = 2 * 10 #1 sec
else:
timeInfo = timeInfo * 10
ipostfix = timeInfo
if command is False:
isprediction = False
elif not command:
if commands is None:
isprediction = False
else:
command = commands
if minLength >= maxLength:
if maxLength != 0:
minLength = 0
maxLength = 0
while True:
kbh = m.kbhit()
if kbh or s_time + 0.1 < time():
s_time = time()
lentext = 0
if kbh:
if not cursor:
curisVis = False
else:
curisVis = True
curVisible(curisVis)
scur_time = time()
key = m.getwch()
skey = key.encode('utf-8')
postfix = ""
ipostfix = timeInfo
s_time = time() - 0.1
#MoveYcur(curposy)
elif cursor:
if curisVis and scur_time + cursorVisibleTime < time():
curisVis = False
curVisible(curisVis)
scur_time = time()
elif not curisVis and scur_time + cursorNotVisibleTime < time():
curisVis = True
curVisible(curisVis)
scur_time = time()
if not kbh:
if ipostfix > 0 and len(postfix) > 5:
if timer:
if postfix.endswith(f" ({(ipostfix+1) / 10}c)"):
postfix = postfix.replace(f" ({(ipostfix+1) / 10}c)", f" ({ipostfix / 10}c)")
else:
postfix += f" ({ipostfix / 10}c)"
ipostfix -= 1
else:
postfix = ""
key, skey = "", ""
elif skey == b'\x01':
if isSelected:
isSelected = False
postfix = "<W>Disable text select."
elif len(inp) == 0:
postfix = "<F>No text to select."
else:
isSelected = True
postfix = "All text selected."
elif skey == b'\x16':
cliptext = get_clip()
if cliptext == "":
postfix = "<F>Clipboard is empty."
elif len(cliptext.split("\n")) > 1:
inp += cliptext.split("\n")[0].replace("\r", "")
postfix = "<W>Only the first line was pasted."
else:
inp += cliptext
postfix = "Text pasted!"
elif skey == b'\xc3\xa0' and key == "à":
key = m.getwch()
if key == "K":
if not (curposx >= len(inp)):
curposx += 1
else:
postfix = "<F>The cursor is already left."
elif key == "M":
if not (curposx <= 0):
curposx -= 1
else:
postfix = "<F>The cursor is already right."
elif key == "H":
#curposy += 1
#MoveYcur(1)
postfix = "<F>Can't move the cursor up."
elif key == "P":
#curposy -= 1
#MoveYcur(-1)
postfix = "<F>Can't move the cursor down."
elif key == "R":
postfix = "<F>Use Ctrl + V to paste"
elif key == "G":
if curposx < len(inp):
curposx = len(inp)
postfix = "Cursor moved to the home."
else:
postfix = "<F>The cursor is already left."
elif key == "O":
if curposx > 0:
curposx = 0
postfix = "Cursor moved to the end."
else:
postfix = "<F>The cursor is already right."
elif key == "I":
if curposx > 0:
curposx = 0
postfix = "Cursor moved to the end."
else:
postfix = "<F>The cursor is already right."
elif key == "Q":
if curposx < len(inp):
curposx = len(inp)
postfix = "Cursor moved to the home."
else:
postfix = "<F>The cursor is already left."
elif key == "S":
postfix = "<F>Use the backspace to delete."
else:
postfix = "<F>Unknown key."
elif skey == b'\x1b':
postfix = "<F>You can't esc."
elif skey == b"\x08":
helptabi = 0
if isSelected:
if len(inp) != 0:
inp = ""
postfix = "All text deleted!"
else:
postfix = "<F>You can't delete."
else:
if curposx == 0:
if len(inp) != 0:
inp = inp[:-1]
else:
postfix = "<F>You can't delete."
else:
if len(inp[:-curposx]) != 0:
inp = inp[:-1-curposx] + inp[-curposx:]
else:
postfix = "<F>You can't delete."
elif skey == b'\t':
if pred != None:
inp = pred
postfix = "Autocompletion successful."
else:
postfix = "<F>No suggestion for autocompletion."
elif skey == b'\r':
if minLength == 0 or len(inp) >= minLength:
if maxLength == 0 or len(inp) <= maxLength:
if free:
curVisible(True)
if isCleared:
clear_console(lastpred, inp)
print("\x1b[2K\r", end='\r')
else:
isCleared = True
return inp.lower()
else:
if isCommand(inp, command=command):
curVisible(True)
if isCleared:
clear_console(lastpred, inp)
print("\x1b[2K\r", end='\r')
else:
isCleared = True
if autoCompleteOnEnter:
return pred.lower()
else:
return inp.lower()
else:
postfix = "<F>Doesn't match commands."
else:
postfix = f"<F>Max length is {maxLength} characters."
else:
postfix = f"<F>Min length is {minLength} characters."
elif len(key) == 1:
if isSelected:
inp = ""
if curposx == 0:
inp += key
if inp.lower() == lastpred[:len(inp)].lower():
if inp.lower() != lastpred.lower():
helptabi += 1
if helptabi >= iHelp:
postfix = "<W>Press Tab for Autocomplete!"
else:
helptabi = 0
else:
if helptabi >= iHelp:
postfix = "<W>Was I wrong?!"
helptabi = 0
else:
inp = inp[:-curposx] + key + inp[-curposx:]
else:
postfix = "<F>Unknown key."
if kbh and skey != b'\x01':
isSelected = False
print("\x1b[2K\r" + "\033[1;A")
if isprediction and kbh:
pred, score = predict(inp, command)
elif not kbh and pred:
pass
else:
pred = None
postfixlen = len(postfix.replace("<F>", "").replace("<W>", ""))
lentext += postfixlen
if postfix.startswith("<F>"):
colored_postfix = Fore.RED + postfix.replace("<F>", "")
elif postfix.startswith("<W>"):
colored_postfix = Fore.LIGHTYELLOW_EX + postfix.replace("<W>", "")
else:
colored_postfix = Fore.LIGHTGREEN_EX + postfix
if pred != None:
if isCleared:
clear_console(lastpred, inp)
else:
isCleared = True
isSimilar = pred[:len(inp)].lower() == inp.lower()
if secret:
output = prefix + "*" * len(inp) + Fore.LIGHTBLACK_EX + "*" * len(pred[len(inp):])
else:
output = prefix + inp + Fore.LIGHTBLACK_EX + pred[len(inp):]
if len(pred) - len(inp) > 0:
lentext += len(pred) - len(inp)
if score > 0:
if isSimilar:
print(output + f" ({score}%) " + colored_postfix, end='\b' * (lentext + len(f" ({score}%) ") + curposx))
else:
if len(pred.split("\n")) > 1:
if secret:
pred_ = "*" * len(pred.split("\n")[len(inp.split("\n"))-1].replace("\n", ""))
else:
pred_ = pred.split("\n")[len(inp.split("\n"))-1].replace("\n", "")
print(output + f" [{pred_}] - ({score}%) " + colored_postfix, end='\b' * (lentext + len(f" [{pred_}] - ({score}%) ") + curposx))
else:
if secret:
pred_ = "*" * len(pred)
else:
pred_ = pred
print(output + f" [{pred_}] - ({score}%) " + colored_postfix, end='\b' * (lentext + len(f" [{pred_}] - ({score}%) ") + curposx))
else:
if isSimilar:
print(output + colored_postfix, end='\b' * lentext)
else:
if secret:
pred_ = "*" * len(pred)
else:
pred_ = pred
print(output + f" [{pred_}] " + colored_postfix, end='\b' * (lentext + len(f" [{pred_}] ") + curposx))
lastpred = pred
else:
if isCleared:
clear_console(lastpred, inp)
else:
isCleared = True
lastpred = ""
curposx = 0
print("\x1b[2K\r", end='\r')
if secret:
print(prefix + "*" * len(inp) + " " + colored_postfix, end='\b' * (postfixlen + len(" ")))
else:
print(prefix + inp + " " + colored_postfix, end='\b' * (postfixlen + len(" ")))