-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtcolors.py
277 lines (235 loc) · 8.02 KB
/
tcolors.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
tcolors.py
~~~~~~~~~~
Get/Set terminal ANSI colors.
Code for retrieving terminal colors was adopted from
http://xyne.archlinux.ca/projects/python3-colorsysplus/.
:Compatibility: Python 2.7 / 3.2
:Copyright: (c) 2014 Miroslav Koškár <http://mkoskar.com/>
:License: BSD 2-Clause, see LICENSE for details
"""
from __future__ import print_function
from contextlib import contextmanager
from signal import signal, SIG_DFL, SIGINT
from sys import exit, stdin, stdout, stderr
import os
import re
import select
import subprocess
import termios
_poll = None
_TERM = os.environ.get('TERM')
if os.environ.get('TMUX'):
_seqfmt = '\033Ptmux;\033{}\a\033\\'
elif _TERM and (_TERM == 'screen' or _TERM.startswith('screen-')):
_seqfmt = '\033P{}\a\033\\'
else:
_seqfmt = '{}\033\\'
def _writeseq(seq, flush=False):
stdout.write(_seqfmt.format(seq))
if flush:
stdout.flush()
def set_colorp(n, c, flush=False):
_writeseq('\033]4;{};{}'.format(n, c), flush)
def get_colorp(n):
return get_term_color([4, n])
def set_colorfg(c, flush=False):
_writeseq('\033]10;{}'.format(c), flush)
def get_colorfg():
return get_term_color([10])
def set_colorbg(c, flush=False):
_writeseq('\033]11;{}'.format(c), flush)
def get_colorbg():
return get_term_color([11])
def set_colorcur(c, flush=False):
_writeseq('\033]12;{}'.format(c), flush)
def get_colorcur():
return get_term_color([12])
def get_xcolors(file=None, prefix=None):
proc = None
if file:
proc = subprocess.Popen(['cpp', '-P', file], stdout=subprocess.PIPE)
else:
proc = subprocess.Popen(['cpp', '-P'], stdin=stdin, stdout=subprocess.PIPE)
xcolors = []
xcolor_pattern = re.compile(re.escape(prefix) +
'(color(\d+)|(foreground)|(background)|(cursorColor))\s*:(.*)$')
while True:
line = proc.stdout.readline()
if not line:
break
line = line.decode().strip()
match = xcolor_pattern.match(line)
if not match:
continue
name, p, fg, bg, cur, value = match.groups()
value = value.strip()
xcolors.append((name, value, p, fg, bg, cur))
if proc.wait() != 0:
raise RuntimeError('Preprocessing failed')
return xcolors
def set_from_xcolors(file=None, prefix=None):
xcolors = get_xcolors(file, prefix)
for c in xcolors:
name, value, p, fg, bg, cur = c
if fg:
set_colorfg(value)
elif bg:
set_colorbg(value)
elif cur:
set_colorcur(value)
else:
set_colorp(p, value)
return xcolors
def get_term_color(ansi, timeout=1000, retries=5):
global _poll
if not _poll:
_poll = select.poll()
_poll.register(stdin.fileno(), select.POLLIN)
while _poll.poll(0):
stdin.read()
query = '\033]' + ';'.join([str(a) for a in ansi]) + ';?' + '\007'
os.write(0, _seqfmt.format(query).encode())
regex = re.compile(
'\033\\](\d+;)+rgba?:(([0-9a-f]+)/)?([0-9a-f]+)/([0-9a-f]+)/([0-9a-f]+)\007',
re.IGNORECASE
)
match = None
output = ''
while not match:
if retries < 1 or not _poll.poll(timeout):
return None
retries -= 1
output += stdin.read()
match = regex.search(output)
return '#' + ''.join(match.group(i)[:2] for i in [4, 5, 6])
@contextmanager
def get_term_colors():
if not stdin.isatty():
raise RuntimeError('<stdin> is not connected to a terminal')
tc_save = None
try:
tc_save = termios.tcgetattr(stdin.fileno())
tc = termios.tcgetattr(stdin.fileno())
tc[3] &= ~termios.ECHO
tc[3] &= ~termios.ICANON
tc[6][termios.VMIN] = 0
tc[6][termios.VTIME] = 0
termios.tcsetattr(stdin.fileno(), termios.TCSANOW, tc)
yield
finally:
if tc_save:
termios.tcsetattr(
stdin.fileno(),
termios.TCSANOW,
tc_save
)
if __name__ == '__main__':
import argparse
import textwrap
parser = argparse.ArgumentParser(
formatter_class=argparse.RawDescriptionHelpFormatter,
description=textwrap.dedent("""
Get/Set terminal ANSI colors.
Color can be given as name or RGB specification (e.g., #rrggbb).
"""
)
)
subparsers = parser.add_subparsers(dest='mode')
p_parser = subparsers.add_parser(
'p',
help='get/set palette color',
description='Get/Set palette color.'
)
f_parser = subparsers.add_parser(
'f',
help='get/set foreground color',
description='Get/Set foreground color.'
)
b_parser = subparsers.add_parser(
'b',
help='get/set background color',
description='Get/Set background color.'
)
c_parser = subparsers.add_parser(
'c',
help='get/set cursor color',
description='Get/Set cursor color.'
)
x_parser = subparsers.add_parser(
'x',
help='get/set as/from X resources',
description='Get/Set colors as/from X resources.'
)
p_parser.add_argument('index', help='palette color index')
p_parser.add_argument('color', nargs='?', help='palette color')
f_parser.add_argument('color', nargs='?', help='foreground color')
b_parser.add_argument('color', nargs='?', help='background color')
c_parser.add_argument('color', nargs='?', help='cursor color')
x_parser.add_argument('-p', '--print', action='store_true',
help="don't apply, print-out only")
x_parser.add_argument('--prefix', default='*',
help='X resources prefix (default: *)')
x_parser.add_argument('file', nargs='?',
help="X resources source file; '-' for stdin")
args = parser.parse_args()
try:
if args.mode == 'p':
if args.color:
set_colorp(args.index, args.color)
else:
output = []
with get_term_colors():
output.append(get_colorp(args.index))
print('\n'.join(output))
elif args.mode == 'f':
if args.color:
set_colorfg(args.color)
else:
output = []
with get_term_colors():
output.append(get_colorfg())
print('\n'.join(output))
elif args.mode == 'b':
if args.color:
set_colorbg(args.color)
else:
output = []
with get_term_colors():
output.append(get_colorbg())
print('\n'.join(output))
elif args.mode == 'c':
if args.color:
set_colorcur(args.color)
else:
output = []
with get_term_colors():
output.append(get_colorcur())
print('\n'.join(output))
elif args.mode == 'x':
if args.file:
if args.print:
for c in get_xcolors(args.file, args.prefix):
print(c[0], c[1])
else:
set_from_xcolors(args.file, args.prefix)
else:
output = []
with get_term_colors():
output.append('{}foreground: {}'.format(args.prefix, get_colorfg()))
output.append('{}background: {}'.format(args.prefix, get_colorbg()))
output.append('{}cursorColor: {}'.format(args.prefix, get_colorcur()))
for n in range(0, 16):
output.append('{}color{}: {}'.format(args.prefix, n, get_colorp(n)))
print('\n'.join(output))
else:
parser.print_usage()
exit(2)
except KeyboardInterrupt:
signal(SIGINT, SIG_DFL)
os.kill(os.getpid(), SIG_DFL)
except RuntimeError as e:
print('{}: error: {}'.format(parser.prog, e), file=stderr)
exit(1)