-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathterm.py
184 lines (124 loc) · 3.32 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
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Copyright (c) 2015-2016, René Tanczos <[email protected]> (Twitter @gravmatt)
The MIT License (MIT)
pyterm helps positioning the cursor and styling output inside the terminal.
Project on github https://github.com/gravmatt/py-term
"""
__author__ = 'Rene Tanczos'
__version__ = '0.7'
__license__ = 'MIT'
import sys
import re
import os
from subprocess import Popen, PIPE
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 pos(line, column):
send('\033[%s;%sf' % (line, column))
def homePos():
send('\033[H')
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 saveCursor():
send('\0337')
# send('\033[s')
def restoreCursor():
send('\0338')
# send('\033[u')
def clear():
send('\033[2J')
def clearLineFromPos():
send('\033[K')
def clearLineToPos():
send('\033[1K')
def clearLine():
send('\033[2K')
def write(text='', *style):
send(format(text, *style))
def writeLine(text='', *style):
write(str(text) + '\n', *style)
def setTitle(name):
send('\033]2;%s\007' % name)
def clearTitle():
setTitle('')
def setTab(name):
send('\033]1;%s\007' % name)
def clearTab():
setTab('')
def strip(text):
return re.sub('\x1b\[[0-9]{1,2}m', '', text)
def textCenter(text):
return ' ' * (int(getSize()[1] / 2) - int(len(strip(text)) / 2)) + text
def center(text):
'''
DEPRICATED: Use textCenter() instead!
Will be removed in later verions!
'''
textCenter(text)
def textRight(text):
return ' ' * (getSize()[1] - len(strip(text))) + text
def getSize():
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')
def format(text, *style):
if(style):
return '%s%s%s' % (''.join(style), text, off)
else:
return text
def highlight(pattern, text, func):
output = ''
idx = 0
matches = [(m.start(), m.end()) for m in re.finditer(pattern, text)]
for p in matches:
output += text[idx:p[0]]
output += func(text[p[0]:p[1]])
idx = p[1]
output += text[idx:]
return (output, len(matches), matches)