Giant blob of minor changes
[dotfiles/.git] / .local / bin / term.py
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 """
4 Copyright (c) 2015-2016, RenĂ© Tanczos <gravmatt@gmail.com> (Twitter @gravmatt)
5 The MIT License (MIT)
6
7 pyterm helps positioning the cursor and styling output inside the terminal.
8
9 Project on github https://github.com/gravmatt/py-term
10 """
11
12 __author__ = 'Rene Tanczos'
13 __version__ = '0.6'
14 __license__ = 'MIT'
15
16 import sys
17 import re
18 import os
19 from subprocess import Popen, PIPE
20
21
22 off = '\033[0m\033[27m'
23 bold = '\033[1m'
24 dim = '\033[2m'
25 underscore = '\033[4m'
26 blink = '\033[5m'
27 reverse = ' \033[7m'
28 hide = '\033[8m'
29
30 black = '\033[30m'
31 red = '\033[31m'
32 green = '\033[32m'
33 yellow = '\033[33m'
34 blue = '\033[34m'
35 magenta = '\033[35m'
36 cyan = '\033[36m'
37 white = '\033[37m'
38
39 bgblack = '\033[40m'
40 bgred = '\033[41m'
41 bggreen = '\033[42m'
42 bgyellow = '\033[43m'
43 bgblue = '\033[44m'
44 bgmagenta = '\033[45m'
45 bgcyan = '\033[46m'
46 bgwhite = '\033[47m'
47
48
49 def send(cmd):
50     sys.stdout.write(cmd)
51     sys.stdout.flush()
52
53
54 def pos(line, column):
55     send('\033[%s;%sf' % (line, column))
56
57
58 def homePos():
59     send('\033[H')
60
61
62 def up(value=1):
63     send('\033[%sA' % value)
64
65
66 def down(value=1):
67     send('\033[%sB' % value)
68
69
70 def right(value=1):
71     send('\033[%sC' % value)
72
73
74 def left(value=1):
75     send('\033[%sD' % value)
76
77
78 def saveCursor():
79     send('\0337')
80     # send('\033[s')
81
82
83 def restoreCursor():
84     send('\0338')
85     # send('\033[u')
86
87
88 def clear():
89     send('\033[2J')
90
91
92 def clearLineFromPos():
93     send('\033[K')
94
95
96 def clearLineToPos():
97     send('\033[1K')
98
99
100 def clearLine():
101     send('\033[2K')
102
103
104 def write(text='', *style):
105     send(format(text, *style))
106
107
108 def writeLine(text='', *style):
109     write(str(text) + '\n', *style)
110
111
112 def setTitle(name):
113     send('\033]2;%s\007' % name)
114
115
116 def clearTitle():
117     setTitle('')
118
119
120 def setTab(name):
121     send('\033]1;%s\007' % name)
122
123
124 def clearTab():
125     setTab('')
126
127
128 def strip(text):
129     return re.sub('\x1b\[[0-9]{1,2}m', '', text)
130
131
132 def center(text):
133     return ' ' * (int(getSize()[1] / 2) - int(len(strip(text)) / 2)) + text
134
135
136 def right(text):
137     return ' ' * (getSize()[1] - len(strip(text))) + text
138
139
140 def getSize():
141     import platform
142     os_sys = platform.system()
143     if(os_sys in ['Linux', 'Darwin'] or os_sys.startswith('CYGWIN')):
144         try:
145             def __get_unix_terminal_size(fd):
146                 import fcntl, termios, struct
147                 return struct.unpack('hh', fcntl.ioctl(fd, termios.TIOCGWINSZ, 'rene'))
148             cr = __get_unix_terminal_size(0) or __get_unix_terminal_size(1) or __get_unix_terminal_size(2)
149             if(not cr):
150                 fd = os.open(os.ctermid(), os.O_RDONLY)
151                 cr = __get_unix_terminal_size(fd)
152                 os.close(fd)
153             return cr
154         except:
155             pass
156     else:
157         raise Exception('operating system not supported')
158
159
160 def format(text, *style):
161     if(style):
162         return '%s%s%s' % (''.join(style), text, off)
163     else:
164         return text
165
166
167 def highlight(pattern, text, func):
168     output = ''
169     idx = 0
170     matches = [(m.start(), m.end()) for m in re.finditer(pattern, text)]
171     for p in matches:
172         output += text[idx:p[0]]
173         output += func(text[p[0]:p[1]])
174         idx = p[1]
175     output += text[idx:]
176     return (output, len(matches), matches)