Repository navigation
Expand file tree
/
Copy pathparse.py
More file actions
120 lines (101 loc) · 3.11 KB
/
Copy pathparse.py
File metadata and controls
120 lines (101 loc) · 3.11 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
import re
from collections.abc import Callable
from functools import partial
from typing import TypedDict, cast
from curtsies.formatstring import FmtStr, fmtstr
from curtsies.termformatconstants import (
BG_COLORS,
FG_COLORS,
)
from curtsies.termformatconstants import (
colors as CURTSIES_COLORS,
)
from ..config import COLOR_LETTERS
from ..lazyre import LazyReCompile
class ColorMatch(TypedDict):
colormarker: str | None
fg: str | None
bg: str | None
bold: str | bool | None
string: str
COLORS = CURTSIES_COLORS + ("default",)
CNAMES = dict(zip(COLOR_LETTERS, COLORS))
# hack for finding the "inverse"
INVERSE_COLORS = {
CURTSIES_COLORS[idx]: CURTSIES_COLORS[
(idx + (len(CURTSIES_COLORS) // 2)) % len(CURTSIES_COLORS)
]
for idx in range(len(CURTSIES_COLORS))
}
INVERSE_COLORS["default"] = INVERSE_COLORS[CURTSIES_COLORS[0]]
def func_for_letter(
letter_color_code: str, default: str = "k"
) -> Callable[[str | FmtStr], FmtStr]:
"""Returns FmtStr constructor for a bpython-style color code"""
if letter_color_code == "d":
letter_color_code = default
elif letter_color_code == "D":
letter_color_code = default.upper()
return partial(
fmtstr,
fg=CNAMES[letter_color_code.lower()],
bold=letter_color_code.isupper(),
)
def color_for_letter(letter_color_code: str, default: str = "k") -> str:
if letter_color_code == "d":
letter_color_code = default
return CNAMES[letter_color_code.lower()]
def parse(s: str) -> FmtStr:
"""Returns a FmtStr object from a bpython-formatted colored string"""
rest = s
stuff = []
while rest:
start, rest = peel_off_string(rest)
stuff.append(start)
return (
sum((fs_from_match(d) for d in stuff[1:]), fs_from_match(stuff[0]))
if len(stuff) > 0
else FmtStr()
)
def fs_from_match(d: ColorMatch) -> FmtStr:
atts = {}
color = "default"
if d["fg"]:
# this isn't according to spec as I understand it
if d["fg"].isupper():
d["bold"] = True
# TODO figure out why boldness isn't based on presence of \x02
color = CNAMES[d["fg"].lower()]
if color != "default":
atts["fg"] = FG_COLORS[color]
if d["bg"]:
if d["bg"] == "I":
# hack for finding the "inverse"
color = INVERSE_COLORS[color]
else:
color = CNAMES[d["bg"].lower()]
if color != "default":
atts["bg"] = BG_COLORS[color]
if d["bold"]:
atts["bold"] = True
return fmtstr(d["string"], **atts)
peel_off_string_re = LazyReCompile(
r"""(?P<colormarker>\x01
(?P<fg>[krgybmcwdKRGYBMCWD]?)
(?P<bg>[krgybmcwdKRGYBMCWDI]?)?)
(?P<bold>\x02?)
\x03
(?P<string>[^\x04]*)
\x04
(?P<rest>.*)
""",
re.VERBOSE | re.DOTALL,
)
def peel_off_string(s: str) -> tuple[ColorMatch, str]:
m = peel_off_string_re.match(s)
assert m, repr(s)
groups = m.groupdict()
rest = m.group("rest")
del groups["rest"]
d = cast(ColorMatch, groups)
return d, rest