Repository navigation
Expand file tree
/
Copy pathconv4bit.py
More file actions
executable file
·365 lines (302 loc) · 10.7 KB
/
Copy pathconv4bit.py
File metadata and controls
executable file
·365 lines (302 loc) · 10.7 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
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#!/usr/bin/env python3
""" Convert 4-bit terminal colorthemes between formats.
The format support is best-effort and generally expects a snippet of only the
color config, when the color config is part of a bigger config file.
By writing the OSC format to stdout, the color palette of a running terminal
can be changed, for those that support xterm-style OSC 4/10/11/12.
Copyright 2024 Jordan Torbiak
License: [MIT](https://opensource.org/license/mit/)
"""
from dataclasses import dataclass, field
from pathlib import Path
from typing import IO, cast, Callable
import argparse
import contextlib
import re
import sys
COLORS_3BIT = ['black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white']
NAME_FOR: dict[int, str] = {
0: 'black',
1: 'red',
2: 'green',
3: 'yellow',
4: 'blue',
5: 'magenta',
6: 'cyan',
7: 'white',
8: 'black_bright',
9: 'red_bright',
10: 'green_bright',
11: 'yellow_bright',
12: 'blue_bright',
13: 'magenta_bright',
14: 'cyan_bright',
15: 'white_bright',
}
@dataclass
class Color:
r: int
g: int
b: int
@staticmethod
def parse(s: str) -> 'Color':
if (m := re.match(r'(?:#|0x)([0-9a-fA-F]{6})', s)):
digits = m.group(1)
r = int(digits[0:2], 16)
g = int(digits[2:4], 16)
b = int(digits[4:6], 16)
return Color(r, g, b)
else:
raise Exception(f'unexpected color format: {s}')
def hex(self) -> str:
return f'#{self.r:02x}{self.g:02x}{self.b:02x}'
@dataclass
class Theme:
foreground: Color
background: Color
black: Color
red: Color
green: Color
yellow: Color
blue: Color
magenta: Color
cyan: Color
white: Color
black_bright: Color
red_bright: Color
green_bright: Color
yellow_bright: Color
blue_bright: Color
magenta_bright: Color
cyan_bright: Color
white_bright: Color
cursor: Color = field(default_factory=lambda: Color.parse('#cccccc'))
cursor_reverse: Color = field(default_factory=lambda: Color.parse('#555555'))
def open_infile(filename: Path) -> IO:
if filename == Path('-'):
return cast(IO, contextlib.nullcontext(sys.stdin))
return open(filename)
def open_outfile(filename: Path) -> IO:
if filename == Path('-'):
return cast(IO, contextlib.nullcontext(sys.stdout))
return open(filename, 'w')
def read_yaml(r: IO) -> Theme:
import yaml # type: ignore[import-untyped]
doc = cast(dict, yaml.safe_load(r))
if 'color_01' in doc:
return read_yaml_gogh(doc)
elif 'colors' in doc and 'primary' in doc['colors']:
return read_yaml_alacritty(doc)
else:
raise ValueError('unexpected yaml format')
def read_yaml_gogh(doc: dict) -> Theme:
color_dict = {}
for i, name in enumerate(COLORS_3BIT):
color_dict[name] = Color.parse(doc[f'color_{i+1:02d}'])
color_dict[f'{name}_bright'] = Color.parse(doc[f'color_{i+1+8:02d}'])
for k in ['foreground', 'background', 'cursor']:
color_dict[k] = Color.parse(doc[k])
return Theme(**color_dict)
def read_yaml_alacritty(doc: dict) -> Theme:
""" Read Alacritty YAML config. """
color_dict = {}
for name in ['foreground', 'background']:
color_dict[name] = Color.parse(doc['colors']['primary'][name])
for name in COLORS_3BIT:
color_dict[name] = Color.parse(doc['colors']['normal'][name])
color_dict[f'{name}_bright'] = Color.parse(doc['colors']['bright'][name])
return Theme(**color_dict)
def read_nidx(r: IO) -> Theme:
""" Read whitespace-separated name-value pairs.
One pair per line. Unix toolkit style.
"""
color_dict = {}
for i, line in enumerate(r):
if line.startswith('#'):
continue
try:
name, hex = line.split()
except ValueError:
raise Exception(f'unexpected format on line {i}')
color_dict[name] = Color.parse(hex)
return Theme(**color_dict)
def write_nidx(w: IO, theme: Theme) -> None:
""" Write whitespace-separated name-value pairs. """
keys = ['foreground', 'background']
for key in COLORS_3BIT:
keys.append(key)
keys.append(f'{key}_bright')
keys.extend(['cursor', 'cursor_reverse'])
for k in keys:
c = getattr(theme, k)
print(f'{k} {c.hex()}', file=w)
def read_csv(r: IO) -> Theme:
""" Read CSV-lite headerless (color_name,hex) """
color_dict = {}
for i, line in enumerate(r):
if line.startswith('#'):
continue
try:
name, hex = line.split(',')
except ValueError:
raise Exception(f'unexpected format on line {i}')
color_dict[name] = Color.parse(hex)
return Theme(**color_dict)
def write_csv(w: IO, theme: Theme) -> None:
""" Write CSV-lite headerless (color_name,hex). """
keys = ['foreground', 'background']
for key in COLORS_3BIT:
keys.append(key)
keys.append(f'{key}_bright')
keys.extend(['cursor', 'cursor_reverse'])
for k in keys:
c = getattr(theme, k)
print(f'{k},{c.hex()}', file=w)
def read_stconf(r: IO) -> Theme:
""" Read color config from suckless st config.h.
Expects just the color part of the config, not the whole file.
"""
color_for = NAME_FOR.copy()
color_for[256] = 'background'
color_for[257] = 'foreground'
color_dict: dict[str, Color] = {}
i = 0
for line in r:
if line.startswith('//'):
continue
if m := re.search(r'(?:\[(\d+)\] += +)?"(#[0-9a-fA-F]{6}")', line):
num, hex = m.groups()
if num is not None:
i = int(num)
color = Color.parse(hex)
color_name = color_for[i]
color_dict[color_name] = color
i += 1
if i > 257:
break
return Theme(**color_dict)
def write_stconf(w: IO, theme: Theme) -> None:
""" Write the color config for suckless st config.h. """
i = 0
print('/* 8 normal colors */', file=w)
for name in COLORS_3BIT:
color = getattr(theme, name)
print(f'[{i}] = "{color.hex()}", /* {name} */', file=w)
i += 1
print('', file=w)
print('/* 8 bright colors */', file=w)
for name in [f'{x}_bright' for x in COLORS_3BIT]:
color = getattr(theme, name)
print(f'[{i}] = "{color.hex()}", /* {name} */', file=w)
i += 1
print('', file=w)
print('/* special colors */', file=w)
print(f'[256] = "{theme.background.hex()}", /* background */', file=w)
print(f'[257] = "{theme.foreground.hex()}", /* foreground */', file=w)
def read_xres(r: IO) -> Theme:
""" Read colors from Xresources. """
color_dict: dict[str, Color] = {}
for line in r:
if line.startswith('!') or re.search(r'^\s*$', line):
continue
if m := re.search(r'([^:]+): *(#[a-fA-F0-9]{6})', line):
label, hex = m.groups()
label = re.sub(r'.*[.*]', '', label) # remove up to last '*' or '.'
color = Color.parse(hex)
if label in {'foreground', 'background'}:
color_dict[label] = color
elif label == 'cursorColor':
color_dict['cursor'] = color
elif label.startswith('color'):
num = int(label.removeprefix('color'))
name = NAME_FOR[num]
color_dict[name] = color
else:
raise Exception(f'unexpected line format: {line}')
return Theme(**color_dict)
def write_xres(w: IO, theme: Theme) -> None:
""" Write colortheme in X resources format.
Use the same format as https://terminal.sexy.
"""
def print_color(name: str, color: Color) -> None:
label = f'*.{name}:'
print(f'{label:16s}{color.hex()}', file=w)
print('! special', file=w)
print_color('foreground', theme.foreground)
print_color('background', theme.background)
print_color('cursorColor', theme.cursor)
for i, name in enumerate(COLORS_3BIT):
print('', file=w)
print(f'! {name}', file=w)
print_color(f'color{i}', getattr(theme, name))
print_color(f'color{i+8}', getattr(theme, name + '_bright'))
def write_osc(w:IO, theme: Theme) -> None:
""" Write OSC escapes to set the color palette of a running terminal.
Tested with: st, tmux, xterm
VTE-based terminals supposedly support them, too.
"""
for i, name in enumerate(COLORS_3BIT):
c = getattr(theme, name)
print(f'\033]4;{i};{c.hex()}\007', file=w, end='')
c = getattr(theme, f'{name}_bright')
print(f'\033]4;{i+8};{c.hex()}\007', file=w, end='')
print(f'\033]10;{theme.foreground.hex()}\007', file=w, end='')
print(f'\033]11;{theme.background.hex()}\007', file=w, end='')
print(f'\033]12;{theme.cursor.hex()}\007', file=w, end='')
IFORMATS: dict[str, Callable[[IO], Theme]] = {
'yaml': read_yaml,
'yml': read_yaml,
'nidx': read_nidx,
'stconf': read_stconf,
'xres': read_xres,
'csv': read_csv,
}
OFORMATS: dict[str, Callable[[IO, Theme], None]] = {
'stconf': write_stconf,
'nidx': write_nidx,
'xres': write_xres,
'csv': write_csv,
'osc': write_osc,
}
def main() -> None:
ap = argparse.ArgumentParser(
formatter_class=argparse.RawDescriptionHelpFormatter,
description='''\
convert 4-bit terminal color schemes between formats.
Formats are guessed from filenames but can also be given with --ifmt/--ofmt.
''')
ap.add_argument('infile', default='-', type=Path, help='use "-" to read from stdin')
ap.add_argument('outfile', default='-', type=Path, help='use "-" to write to stdout')
ap.add_argument('--ifmt', '-i', choices=IFORMATS.keys())
ap.add_argument('--ofmt', '-o', choices=OFORMATS.keys())
args = ap.parse_args()
if args.infile == Path('-') and args.ifmt is None:
print('--ifmt must be given when reading stdin', file=sys.stderr)
sys.exit(1)
if args.outfile == Path('-') and args.ofmt is None:
print('--ofmt must be given when writing to stdout', file=sys.stderr)
sys.exit(1)
ifmt = args.ifmt
if ifmt is None:
iext = args.infile.suffix.lstrip('.')
if iext in IFORMATS:
ifmt = iext
if ifmt is None:
print('unsupported input format', file=sys.stderr)
sys.exit(1)
ofmt = args.ofmt
if ofmt is None:
oext = args.outfile.suffix.lstrip('.')
if oext in OFORMATS:
ofmt = oext
if ofmt is None:
print('unsupported output format', file=sys.stderr)
sys.exit(1)
with (
open_infile(args.infile) as infile,
open_outfile(args.outfile) as outfile
):
theme = IFORMATS[ifmt](infile)
OFORMATS[ofmt](outfile, theme)
if __name__ == '__main__':
main()