-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
41 lines (37 loc) · 1.47 KB
/
test.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
import sys
import tty
colorama.init()
def command_line():
tty.setraw(sys.stdin)
while True: # loop for each line
# Define data-model for an input-string with a cursor
input = ""
index = 0
while True: # loop for each character
char = ord(sys.stdin.read(1)) # read one char and get char code
# Manage internal data-model
if char == 3: # CTRL-C
return
elif 32 <= char <= 126:
input = input[:index] + chr(char) + input[index:]
index += 1
elif char in {10, 13}:
sys.stdout.write(u"\u001b[1000D")
print("\nechoing... ", input)
input = ""
index = 0
elif char == 27:
next1, next2 = ord(sys.stdin.read(1)), ord(sys.stdin.read(1))
if next1 == 91:
if next2 == 68: # Left
index = max(0, index - 1)
elif next2 == 67: # Right
index = min(len(input), index + 1)
# Print current input-string
sys.stdout.write(u"\u001b[1000D") # Move all the way left
sys.stdout.write(input)
sys.stdout.write(u"\u001b[1000D") # Move all the way left again
if index > 0:
sys.stdout.write(u"\u001b[" + str(index) + "C") # Move cursor too index
sys.stdout.flush()
command_line()