mirror of
https://github.com/kovidgoyal/kitty
synced 2026-07-24 01:08:10 +02:00
Use only a single marker function
Multipe colors/expressions can instead be combined at definition time
This commit is contained in:
@@ -21,10 +21,10 @@ def get_output_variables(left_address, right_address, color_address):
|
||||
)
|
||||
|
||||
|
||||
def marker_from_regex(expression, color):
|
||||
def marker_from_regex(expression, color, flags=re.UNICODE):
|
||||
color = max(1, min(color, 3))
|
||||
if isinstance(expression, str):
|
||||
pat = re.compile(expression)
|
||||
pat = re.compile(expression, flags=flags)
|
||||
else:
|
||||
pat = expression
|
||||
|
||||
@@ -39,6 +39,28 @@ def marker_from_regex(expression, color):
|
||||
return marker
|
||||
|
||||
|
||||
def marker_from_multiple_regex(regexes, flags=re.UNICODE):
|
||||
expr = ''
|
||||
color_map = {}
|
||||
for i, (color, spec) in enumerate(regexes):
|
||||
grp = 'mcg{}'.format(i)
|
||||
expr += '|(?P<{}>{})'.format(grp, spec)
|
||||
color_map[grp] = color
|
||||
expr = expr[1:]
|
||||
pat = re.compile(expr, flags=flags)
|
||||
|
||||
def marker(text, left_address, right_address, color_address):
|
||||
left, right, color = get_output_variables(left_address, right_address, color_address)
|
||||
for match in pat.finditer(text):
|
||||
left.value = match.start()
|
||||
right.value = match.end() - 1
|
||||
grp = next(k for k, v in match.groupdict().items() if v is not None)
|
||||
color.value = color_map[grp]
|
||||
yield
|
||||
|
||||
return marker
|
||||
|
||||
|
||||
def marker_from_text(expression, color):
|
||||
return marker_from_regex(re.escape(expression), color)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user