-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheol.py
65 lines (61 loc) · 1.79 KB
/
eol.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
import os
import sys
def get_key(osp = None):
if osp == None:
osp = check_os()
if osp == "windows":
import msvcrt
else:
import tty
import termios
if osp == "windows":
#todo linux support
if msvcrt.kbhit():
key = msvcrt.getch()
if key == b'\xe0': # Special key prefix
key = msvcrt.getch()
if key == b'H':
return 'UP'
elif key == b'P':
return 'DOWN'
elif key == b'K':
return 'LEFT'
elif key == b'M':
return 'RIGHT'
else:
try:
return key.decode('utf-8', errors='ignore')
except UnicodeDecodeError:
return None
else:
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
key = sys.stdin.read(1)
if key == '\x1b': # Escape sequence (for arrow keys)
extra = sys.stdin.read(2)
if extra == '[A':
return 'UP'
elif extra == '[B':
return 'DOWN'
elif extra == '[C':
return 'RIGHT'
elif extra == '[D':
return 'LEFT'
else:
return key
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
def check_os():
if os.name == 'nt':
return "windows"
else:
return "linux/mac"
def clear_screen(osp = None):
if osp == None:
osp = check_os()
if osp == "windows":
os.system('cls')
else:
os.system('clear')