-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathterm.py
142 lines (92 loc) · 2.55 KB
/
term.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Fonctions permettant de styliser l'affichage dans le terminal
Basé sur py-term : https://github.com/gravmatt/py-term"""
import sys
OFF = '\033[0m\033[27m'
BOLD = '\033[1m'
DIM = '\033[2m'
UNDERSCORE = '\033[4m'
BLINK = '\033[5m'
REVERSE = ' \033[7m'
HIDE = '\033[8m'
BLACK = '\033[30m'
RED = '\033[31m'
GREEN = '\033[32m'
YELLOW = '\033[33m'
BLUE = '\033[34m'
MAGENTA = '\033[35m'
CYAN = '\033[36m'
WHITE = '\033[37m'
BGBLACK = '\033[40m'
BGRED = '\033[41m'
BGGREEN = '\033[42m'
BGYELLOW = '\033[43m'
BGBLUE = '\033[44m'
BGMAGENTA = '\033[45m'
BGCYAN = '\033[46m'
BGWHITE = '\033[47m'
def send(cmd):
sys.stdout.write(cmd)
sys.stdout.flush()
def up(value=1):
send('\033[%sA' % value)
def down(value=1):
send('\033[%sB' % value)
def right(value=1):
send('\033[%sC' % value)
def left(value=1):
send('\033[%sD' % value)
def clear():
send('\033[2J')
def clear_line_from_pos():
send('\033[K')
def clear_line_to_pos():
send('\033[1K')
def clear_line():
send('\033[2K')
def clear_previous_line():
up(1)
clear_line()
def bold(txt):
return str(BOLD) + str(txt) + str(OFF)
def underscore(txt):
return str(UNDERSCORE) + str(txt) + str(OFF)
def black(txt):
return str(BLACK) + str(txt) + str(OFF)
def red(txt):
return str(RED) + str(txt) + str(OFF)
def green(txt):
return str(GREEN) + str(txt) + str(OFF)
def yellow(txt):
return str(YELLOW) + str(txt) + str(OFF)
def blue(txt):
return str(BLUE) + str(txt) + str(OFF)
def magenta(txt):
return str(MAGENTA) + str(txt) + str(OFF)
def cyan(txt):
return str(CYAN) + str(txt) + str(OFF)
def white(txt):
return str(WHITE) + str(txt) + str(OFF)
def print_separator(c):
send(c * get_size()[1]+'\n')
def get_size():
import platform
os_sys = platform.system()
if (os_sys in ['Linux', 'Darwin'] or os_sys.startswith('CYGWIN')):
try:
def __get_unix_terminal_size(fd):
import fcntl, termios, struct
return struct.unpack('hh', fcntl.ioctl(fd, termios.TIOCGWINSZ,
'rene'))
cr = __get_unix_terminal_size(0) or __get_unix_terminal_size(
1) or __get_unix_terminal_size(2)
if (not cr):
fd = os.open(os.ctermid(), os.O_RDONLY)
cr = __get_unix_terminal_size(fd)
os.close(fd)
return cr
except:
pass
else:
raise Exception('operating system not supported')