-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnsi.py
More file actions
190 lines (140 loc) · 4.62 KB
/
Ansi.py
File metadata and controls
190 lines (140 loc) · 4.62 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
import ctypes
import time
from sys import platform, stdout
from KeyGetter import KeyGetter
keyGetter = None
class Ansi():
ESC = 27
ENTER = 13
ARROWDOWN = 57424
ARROWUP = 57416
ARROWLEFT = 57419
ARROWRIGHT = 57421
PAGEUP = 57417
PAGEDOWN = 57425
HOME = 57415
END = 57423
DEL = 57427
INSERT = 57426
Black = u"\u001b[30m"
Red = u"\u001b[31m"
Green = u"\u001b[32m"
Yellow = u"\u001b[33m"
Blue = u"\u001b[34m"
Magenta = u"\u001b[35m"
Cyan = u"\u001b[36m"
White = u"\u001b[37m"
BrightBlack = u"\u001b[30;1m"
BrightRed = u"\u001b[31;1m"
BrightGreen = u"\u001b[32;1m"
BrightYellow = u"\u001b[33;1m"
BrightBlue = u"\u001b[34;1m"
BrightMagenta = u"\u001b[35;1m"
BrightCyan = u"\u001b[36;1m"
BrightWhite = u"\u001b[37;1m"
BlackBackground = u"\u001b[40m"
RedBackground = u"\u001b[41m"
GreenBackground = u"\u001b[42m"
YellowBackground = u"\u001b[43m"
BlueBackground = u"\u001b[44m"
MagentaBackground = u"\u001b[45m"
CyanBackground = u"\u001b[46m"
WhiteBackground = u"\u001b[47m"
BrightBlackBackground = u"\u001b[100;1m"
BrightRedBackground = u"\u001b[101;1m"
BrightGreenBackground = u"\u001b[102;1m"
BrightYellowBackground = u"\u001b[103;1m"
BrightBlueBackground = u"\u001b[104;1m"
BrightMagentaBackground = u"\u001b[105;1m"
BrightCyanBackground = u"\u001b[106;1m"
BrightWhiteBackground = u"\u001b[107;1m"
Bold = u"\u001b[1m"
Dim = u"\u001b[2m"
Underline = u"\u001b[4m"
Reversed = u"\u001b[7m"
Up = u"\u001b[%dA"
Down = u"\u001b[%dB"
Right = u"\u001b[%dC"
Left = u"\u001b[%dD"
NextLine = u"\u001b[1E"
PreviousLine = u"\u001b[1F"
ToColumn = u"\u001b[%dG"
Move = u"\u001b[%d;%dH"
Clear = u"\u001b[2J"
ClearLine = u"\u001b[2K"
Reset = u"\u001b[0m"
HideCursor = u"\u001b[?25l"
ShowCursor = u"\u001b[?25h"
@staticmethod
def SetColor(color: int) -> str:
return u"\u001b[3%dm" % color
@staticmethod
def CursorToColumn(column: int) -> str:
return Ansi.ToColumn % column
@staticmethod
def MoveUp(count: int = 1) -> str:
return Ansi.Up % count
@staticmethod
def MoveDown(count: int = 1) -> str:
return Ansi.Down % count
@staticmethod
def MoveLeft(count: int = 1) -> str:
return Ansi.Left % count
@staticmethod
def MoveRight(count: int = 1) -> str:
return Ansi.Right % count
@staticmethod
def MoveCursor(column: int, line: int) -> str:
return Ansi.Move % (line, column)
@staticmethod
def Flush():
stdout.flush()
@staticmethod
def Init():
if platform== "linux" or platform == "linux2":
# linux
pass
elif platform == "darwin":
# OS X
pass
elif platform == "win32":
import msvcrt
kernel32 = ctypes.windll.kernel32
kernel32.SetConsoleMode(kernel32.GetStdHandle(-11), 7)
@staticmethod
def GetKey():
global keyGetter
if keyGetter == None:
keyGetter = KeyGetter()
try:
if keyGetter.kbhit():
ch = keyGetter.getch(False)
ch1 = ord(ch)
if ch1 == 224:
ch = keyGetter.getch(False)
ch2 = ord(ch)
return (ch1 << 8) | ch2
return ch1
return None
except KeyboardInterrupt:
pass
# https://www.lihaoyi.com/post/BuildyourownCommandLinewithANSIescapecodes.html
# https://en.wikipedia.org/wiki/ANSI_escape_code
####################################################################################
####################################################################################
####################################################################################
if __name__ == "__main__":
Ansi.Init()
print(Ansi.Clear + Ansi.HideCursor, end = "")
print(Ansi.Red + " Red " + Ansi.Reset, end = "")
print(Ansi.BrightRed + " Bright Red " + Ansi.Reset, end = "")
print(Ansi.RedBackground + " Red Background " + Ansi.Reset, end = "")
print(Ansi.MoveDown(2) + Ansi.BrightYellow + Ansi.CursorToColumn(1) + "Press ESC ", end = "")
Ansi.Flush()
while True:
ch = Ansi.GetKey()
if ch != None:
print(ch, repr(ch))
time.sleep(0.2)
print(Ansi.MoveDown(2) + Ansi.BrightGreen + Ansi.CursorToColumn(1) + "Done ", end = "")
print(Ansi.ShowCursor, end = "")