-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase.py
More file actions
354 lines (288 loc) · 10.5 KB
/
base.py
File metadata and controls
354 lines (288 loc) · 10.5 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
from __future__ import annotations
import abc
import collections
import colorsys
import enum
import threading
from collections import defaultdict
from typing import ClassVar
from python_utils import converters, types
from .. import base as pbase, env
from .os_specific import getch
ESC = '\x1b'
class CSI:
_code: str
_template = ESC + '[{args}{code}'
def __init__(self, code, *default_args):
self._code = code
self._default_args = default_args
def __call__(self, *args):
return self._template.format(args=';'.join(map(str, args or self._default_args)), code=self._code)
def __str__(self):
return self()
class CSINoArg(CSI):
def __call__(self):
return super().__call__()
CUP = CSI('H', 1, 1)
UP = CSI('A', 1)
DOWN = CSI('B', 1)
RIGHT = CSI('C', 1)
LEFT = CSI('D', 1)
NEXT_LINE = CSI('E', 1)
PREVIOUS_LINE = CSI('F', 1)
COLUMN = CSI('G', 1)
CLEAR_SCREEN = CSI('J', 0)
CLEAR_SCREEN_TILL_END = CSINoArg('0J')
CLEAR_SCREEN_TILL_START = CSINoArg('1J')
CLEAR_SCREEN_ALL = CSINoArg('2J')
CLEAR_SCREEN_ALL_AND_HISTORY = CSINoArg('3J')
CLEAR_LINE_ALL = CSI('K')
CLEAR_LINE_RIGHT = CSINoArg('0K')
CLEAR_LINE_LEFT = CSINoArg('1K')
CLEAR_LINE = CSINoArg('2K')
SCROLL_UP = CSI('S')
SCROLL_DOWN = CSI('T')
SAVE_CURSOR = CSINoArg('s')
RESTORE_CURSOR = CSINoArg('u')
HIDE_CURSOR = CSINoArg('?25l')
SHOW_CURSOR = CSINoArg('?25h')
class _CPR(str):
_response_lock = threading.Lock()
def __call__(self, stream) -> tuple[int, int]:
res: str = ''
with self._response_lock:
stream.write(str(self))
stream.flush()
while not res.endswith('R'):
char = getch()
if char is not None:
res += char
res_list = res[2:-1].split(';')
res_list = tuple((int(item) if item.isdigit() else item for item in res_list))
if len(res_list) == 1:
return types.cast(types.Tuple[int, int], res_list[0])
return types.cast(types.Tuple[int, int], tuple(res_list))
class WindowsColors(enum.Enum):
BLACK = (0, 0, 0)
BLUE = (0, 0, 128)
GREEN = (0, 128, 0)
CYAN = (0, 128, 128)
RED = (128, 0, 0)
MAGENTA = (128, 0, 128)
YELLOW = (128, 128, 0)
GREY = (192, 192, 192)
INTENSE_BLACK = (128, 128, 128)
INTENSE_BLUE = (0, 0, 255)
INTENSE_GREEN = (0, 255, 0)
INTENSE_CYAN = (0, 255, 255)
INTENSE_RED = (255, 0, 0)
INTENSE_MAGENTA = (255, 0, 255)
INTENSE_YELLOW = (255, 255, 0)
INTENSE_WHITE = (255, 255, 255)
@staticmethod
def from_rgb(rgb: types.Tuple[int, int, int]):
"""
Find the closest WindowsColors to the given RGB color.
>>> WindowsColors.from_rgb((0, 0, 0))
<WindowsColors.BLACK: (0, 0, 0)>
>>> WindowsColors.from_rgb((255, 255, 255))
<WindowsColors.INTENSE_WHITE: (255, 255, 255)>
>>> WindowsColors.from_rgb((0, 255, 0))
<WindowsColors.INTENSE_GREEN: (0, 255, 0)>
>>> WindowsColors.from_rgb((45, 45, 45))
<WindowsColors.BLACK: (0, 0, 0)>
>>> WindowsColors.from_rgb((128, 0, 128))
<WindowsColors.MAGENTA: (128, 0, 128)>
"""
min_distance = float('inf')
closest_color = None
for color in WindowsColors:
# Calculate Euclidean distance between colors
r1, g1, b1 = rgb
r2, g2, b2 = color.value
distance = ((r1 - r2) ** 2 + (g1 - g2) ** 2 + (b1 - b2) ** 2) ** 0.5
if distance < min_distance:
min_distance = distance
closest_color = color
return closest_color
class WindowsColor:
"""
Windows compatible color class for when ANSI is not supported.
Currently a no-op because it is not possible to buffer these colors.
>>> WindowsColor(WindowsColors.RED)('test')
'test'
"""
__slots__ = ('color',)
def __init__(self, color: Color):
self.color = color
def __call__(self, text):
return text
class RGB(collections.namedtuple('RGB', ['red', 'green', 'blue'])):
__slots__ = ()
def __str__(self):
return self.rgb
@property
def to_windows(self):
"""
Convert an RGB color (0-255 per channel) to the closest color in the
Windows 16 color scheme.
"""
return WindowsColors.from_rgb((self.red, self.green, self.blue))
class HSL(collections.namedtuple('HSL', ['hue', 'saturation', 'lightness'])):
"""
Hue, Saturation, Lightness color.
Hue is a value between 0 and 360, saturation and lightness are between 0(%)
and 100(%).
"""
__slots__ = ()
@classmethod
def from_rgb(cls, rgb: RGB) -> HSL:
"""
Convert a 0-255 RGB color to a 0-255 HLS color.
"""
# Convert RGB values to 0-1 range for colorsys
r = rgb.red / 255.0
g = rgb.green / 255.0
b = rgb.blue / 255.0
# Convert to HSL
h, l, s = colorsys.rgb_to_hls(r, g, b)
# Convert hue to 0-360 range and saturation/lightness to 0-100 range
h = h * 360
s = s * 100
l = l * 100
return cls(h, s, l)
class ColorBase(abc.ABC):
pass
class Color(collections.namedtuple('Color', ['rgb', 'hls', 'name', 'xterm']), ColorBase):
"""
Color base class.
This class contains the colors in RGB (Red, Green, Blue), HSL (Hue,
Lightness, Saturation) and Xterm (8-bit) formats. It also contains the
color name.
To make a custom color the only required arguments are the RGB values.
The other values will be automatically interpolated from that if needed,
but you can be more explicitly if you wish.
"""
__slots__ = ()
def __call__(self, value: str) -> str:
return self.fg(value)
def __str__(self):
return self.name
def __repr__(self):
return f'{self.__class__.__name__}({self.name!r})'
def __hash__(self):
return hash(self.rgb)
class Colors:
by_name: ClassVar[defaultdict[str, types.List[Color]]] = collections.defaultdict(list)
by_lowername: ClassVar[defaultdict[str, types.List[Color]]] = collections.defaultdict(list)
by_hex: ClassVar[defaultdict[str, types.List[Color]]] = collections.defaultdict(list)
by_rgb: ClassVar[defaultdict[RGB, types.List[Color]]] = collections.defaultdict(list)
by_hls: ClassVar[defaultdict[HSL, types.List[Color]]] = collections.defaultdict(list)
by_xterm: ClassVar[dict[int, Color]] = dict()
@classmethod
def register(cls, rgb: RGB, hls: HSL, name: str, xterm: int) -> Color:
"""Register a new color with the given RGB, HSL, name and xterm code."""
color = Color(rgb, hls, name, xterm)
cls.by_name[name].append(color)
cls.by_lowername[name.lower()].append(color)
cls.by_rgb[rgb].append(color)
cls.by_hls[hls].append(color)
cls.by_xterm[xterm] = color
return color
@staticmethod
def interpolate(color1: Color, color2: Color, value: float) -> Color:
"""Interpolate between two colors based on a value between 0 and 1."""
if value <= 0:
return color1
elif value >= 1:
return color2
# Interpolate RGB values
r1, g1, b1 = color1.rgb
r2, g2, b2 = color2.rgb
r = int(r1 + (r2 - r1) * value)
g = int(g1 + (g2 - g1) * value)
b = int(b1 + (b2 - b1) * value)
rgb = RGB(r, g, b)
# Interpolate HSL values
h1, s1, l1 = color1.hls
h2, s2, l2 = color2.hls
h = h1 + (h2 - h1) * value
s = s1 + (s2 - s1) * value
l = l1 + (l2 - l1) * value
hsl = HSL(h, s, l)
# Use the name of the color we're closer to
name = color1.name if value < 0.5 else color2.name
xterm = color1.xterm if value < 0.5 else color2.xterm
return Color(rgb, hsl, name, xterm)
class ColorGradient(ColorBase):
def __init__(self, *colors: Color, interpolate=Colors.interpolate):
assert colors
self.colors = colors
self.interpolate = interpolate
def __call__(self, value: float) -> Color:
return self.get_color(value)
def get_color(self, value: float) -> Color:
"""Map a value from 0 to 1 to a color."""
if value <= 0:
return self.colors[0]
elif value >= 1:
return self.colors[-1]
# Calculate which segment of the gradient we're in
segment_size = 1.0 / (len(self.colors) - 1)
segment = int(value / segment_size)
segment_value = (value - segment * segment_size) / segment_size
return self.interpolate(self.colors[segment], self.colors[segment + 1], segment_value)
OptionalColor = types.Union[Color, ColorGradient, None]
def apply_colors(text: str, percentage: float | None=None, *, fg: OptionalColor=None, bg: OptionalColor=None, fg_none: Color | None=None, bg_none: Color | None=None, **kwargs: types.Any) -> str:
"""Apply colors/gradients to a string depending on the given percentage.
When percentage is `None`, the `fg_none` and `bg_none` colors will be used.
Otherwise, the `fg` and `bg` colors will be used. If the colors are
gradients, the color will be interpolated depending on the percentage.
"""
if percentage is None:
fg_color = fg_none
bg_color = bg_none
else:
fg_color = fg(percentage) if isinstance(fg, ColorGradient) else fg
bg_color = bg(percentage) if isinstance(bg, ColorGradient) else bg
if fg_color is None and bg_color is None:
return text
# Apply colors
if fg_color:
text = fg_color(text)
if bg_color:
text = bg_color(text)
return text
class DummyColor:
def __call__(self, text):
return text
def __repr__(self):
return 'DummyColor()'
class SGR(CSI):
_start_code: int
_end_code: int
_code = 'm'
__slots__ = ('_start_code', '_end_code')
def __init__(self, start_code: int, end_code: int):
self._start_code = start_code
self._end_code = end_code
def __call__(self, text, *args):
return self._start_template + text + self._end_template
class SGRColor(SGR):
__slots__ = ('_color', '_start_code', '_end_code')
def __init__(self, color: Color, start_code: int, end_code: int):
self._color = color
super().__init__(start_code, end_code)
encircled = SGR(52, 54)
framed = SGR(51, 54)
overline = SGR(53, 55)
bold = SGR(1, 22)
gothic = SGR(20, 10)
italic = SGR(3, 23)
strike_through = SGR(9, 29)
fast_blink = SGR(6, 25)
slow_blink = SGR(5, 25)
underline = SGR(4, 24)
double_underline = SGR(21, 24)
faint = SGR(2, 22)
inverse = SGR(7, 27)