mirror of
https://github.com/Textualize/rich.git
synced 2025-08-04 18:18:22 +00:00
highlighter optimization
This commit is contained in:
parent
b40f624024
commit
20084da4f8
3 changed files with 38 additions and 21 deletions
|
@ -4,6 +4,15 @@ from typing import List, Union
|
|||
from .text import Text
|
||||
|
||||
|
||||
def _combine_regex(*regexes: str) -> str:
|
||||
"""Combine a number of regexes in to a single regex.
|
||||
|
||||
Returns:
|
||||
str: New regex with all regexes ORed together.
|
||||
"""
|
||||
return "|".join(regexes)
|
||||
|
||||
|
||||
class Highlighter(ABC):
|
||||
"""Abstract base class for highlighters."""
|
||||
|
||||
|
@ -61,6 +70,7 @@ class RegexHighlighter(Highlighter):
|
|||
text (~Text): Text to highlighted.
|
||||
|
||||
"""
|
||||
|
||||
highlight_regex = text.highlight_regex
|
||||
for re_highlight in self.highlights:
|
||||
highlight_regex(re_highlight, style_prefix=self.base_style)
|
||||
|
@ -71,24 +81,21 @@ class ReprHighlighter(RegexHighlighter):
|
|||
|
||||
base_style = "repr."
|
||||
highlights = [
|
||||
r"(?P<brace>[\{\[\(\)\]\}])",
|
||||
r"(?P<tag_start>\<)(?P<tag_name>[\w\-\.\:]*)(?P<tag_contents>.*?)(?P<tag_end>\>)",
|
||||
r"(?P<attrib_name>\w+?)=(?P<attrib_value>\"?[\w_]+\"?)?",
|
||||
r"(?P<bool_true>True)|(?P<bool_false>False)|(?P<none>None)",
|
||||
r"(?P<number>(?<!\w)\-?[0-9]+\.?[0-9]*(e[\-\+]?\d+?)?\b)",
|
||||
r"(?P<number>0x[0-9a-f]*)",
|
||||
r"(?P<path>\B(\/[\w\.\-\_\+]+)*\/)(?P<filename>[\w\.\-\_\+]*)?",
|
||||
r"(?P<ipv4>[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})",
|
||||
r"(?P<ipv6>([A-Fa-f0-9]{1,4}::?){1,7}[A-Fa-f0-9]{1,4})",
|
||||
r"(?P<eui48>([0-9A-Fa-f]{1,2}-){5}[0-9A-Fa-f]{1,2})", # EUI-48 6x2 hyphen
|
||||
r"(?P<eui64>([0-9A-Fa-f]{1,2}-){7}[0-9A-Fa-f]{1,2})", # EUI-64 8x2 hyphen
|
||||
r"(?P<eui48>([0-9A-Fa-f]{1,2}:){5}[0-9A-Fa-f]{1,2})", # EUI-48 6x2 colon
|
||||
r"(?P<eui64>([0-9A-Fa-f]{1,2}:){7}[0-9A-Fa-f]{1,2})", # EUI-64 8x2 colon
|
||||
r"(?P<eui48>([0-9A-Fa-f]{4}\.){2}[0-9A-Fa-f]{4})", # EUI-48 3x4 dot
|
||||
r"(?P<eui64>([0-9A-Fa-f]{4}\.){3}[0-9A-Fa-f]{4})", # EUI-64 4x4 dot
|
||||
r"(?<!\\)(?P<str>b?\'\'\'.*?(?<!\\)\'\'\'|b?\'.*?(?<!\\)\'|b?\"\"\".*?(?<!\\)\"\"\"|b?\".*?(?<!\\)\")",
|
||||
_combine_regex(
|
||||
r"(?P<ipv4>[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})",
|
||||
r"(?P<ipv6>([A-Fa-f0-9]{1,4}::?){1,7}[A-Fa-f0-9]{1,4})",
|
||||
r"(?P<eui64>([0-9A-Fa-f]{1,2}-){7}[0-9A-Fa-f]{1,2}|([0-9A-Fa-f]{1,2}:){7}[0-9A-Fa-f]{1,2}|([0-9A-Fa-f]{4}\.){3}[0-9A-Fa-f]{4})",
|
||||
r"(?P<eui48>([0-9A-Fa-f]{1,2}-){5}[0-9A-Fa-f]{1,2}|([0-9A-Fa-f]{1,2}:){5}[0-9A-Fa-f]{1,2}|([0-9A-Fa-f]{4}\.){2}[0-9A-Fa-f]{4})",
|
||||
r"(?P<brace>[\{\[\(\)\]\}])",
|
||||
r"(?P<attrib_name>\w+?)=(?P<attrib_value>\"?[\w_]+\"?)?",
|
||||
r"(?P<bool_true>True)|(?P<bool_false>False)|(?P<none>None)",
|
||||
r"(?P<number>(?<!\w)\-?[0-9]+\.?[0-9]*(e[\-\+]?\d+?)?\b|0x[0-9a-f]*)",
|
||||
r"(?P<path>\B(\/[\w\.\-\_\+]+)*\/)(?P<filename>[\w\.\-\_\+]*)?",
|
||||
r"(?<!\\)(?P<str>b?\'\'\'.*?(?<!\\)\'\'\'|b?\'.*?(?<!\\)\'|b?\"\"\".*?(?<!\\)\"\"\"|b?\".*?(?<!\\)\")",
|
||||
r"(?P<uuid>[a-fA-F0-9]{8}\-[a-fA-F0-9]{4}\-[a-fA-F0-9]{4}\-[a-fA-F0-9]{4}\-[a-fA-F0-9]{12})",
|
||||
),
|
||||
r"(?P<url>https?:\/\/[0-9a-zA-Z\$\-\_\+\!`\(\)\,\.\?\/\;\:\&\=\%\#]*)",
|
||||
r"(?P<uuid>[a-fA-F0-9]{8}\-[a-fA-F0-9]{4}\-[a-fA-F0-9]{4}\-[a-fA-F0-9]{4}\-[a-fA-F0-9]{12})",
|
||||
]
|
||||
|
||||
|
||||
|
|
|
@ -16,8 +16,8 @@ def test_wrong_type():
|
|||
[
|
||||
("repr.eui48", "01-23-45-67-89-AB"), # 6x2 hyphen
|
||||
("repr.eui64", "01-23-45-FF-FE-67-89-AB"), # 8x2 hyphen
|
||||
("repr.eui48", "01:23:45:67:89:AB"), # 6x2 colon
|
||||
("repr.eui64", "01:23:45:FF:FE:67:89:AB"), # 8x2 colon
|
||||
("repr.ipv6", "01:23:45:67:89:AB"), # 6x2 colon
|
||||
("repr.ipv6", "01:23:45:FF:FE:67:89:AB"), # 8x2 colon
|
||||
("repr.eui48", "0123.4567.89AB"), # 3x4 dot
|
||||
("repr.eui64", "0123.45FF.FE67.89AB"), # 4x4 dot
|
||||
("repr.eui48", "ed-ed-ed-ed-ed-ed"), # lowercase
|
||||
|
@ -31,4 +31,4 @@ def test_highlight_regex(style_name: str, test_str: str):
|
|||
text = Text(test_str)
|
||||
highlighter = ReprHighlighter()
|
||||
highlighter.highlight(text)
|
||||
assert text._spans[-1] == Span(0, len(test_str), style_name)
|
||||
assert text.spans == [Span(0, len(test_str), style_name)]
|
||||
|
|
|
@ -2,10 +2,20 @@
|
|||
|
||||
|
||||
import io
|
||||
import re
|
||||
|
||||
from rich.console import Console
|
||||
|
||||
from .render import replace_link_ids
|
||||
|
||||
re_link_ids = re.compile(r"id=[\d\.\-]*?;.*?\x1b")
|
||||
|
||||
|
||||
def replace_link_ids(render: str) -> str:
|
||||
"""Link IDs have a random ID and system path which is a problem for
|
||||
reproducible tests.
|
||||
|
||||
"""
|
||||
return re_link_ids.sub("id=0;foo\x1b", render)
|
||||
|
||||
|
||||
test_data = [1, 2, 3]
|
||||
|
@ -28,7 +38,7 @@ def render_log():
|
|||
|
||||
def test_log():
|
||||
expected = replace_link_ids(
|
||||
"\n\x1b[2;36m[TIME]\x1b[0m\x1b[2;36m \x1b[0mHello from \x1b[1m<\x1b[0m\x1b[1;95mconsole\x1b[0m\x1b[39m \x1b[0m\x1b[3;33mwidth\x1b[0m\x1b[39m=\x1b[0m\x1b[1;34m80\x1b[0m\x1b[39m ColorSystem.TRUECOLOR\x1b[0m\x1b[1m>\x1b[0m ! \x1b]8;id=0;foo\x1b\\\x1b[2mtest_log.py\x1b[0m\x1b]8;;\x1b\\\x1b[2m:24\x1b[0m\n\x1b[2;36m \x1b[0m\x1b[2;36m \x1b[0m\x1b[1m[\x1b[0m\x1b[1;34m1\x1b[0m, \x1b[1;34m2\x1b[0m, \x1b[1;34m3\x1b[0m\x1b[1m]\x1b[0m \x1b]8;id=0;foo\x1b\\\x1b[2mtest_log.py\x1b[0m\x1b]8;;\x1b\\\x1b[2m:25\x1b[0m\n \x1b[34m╭─\x1b[0m\x1b[34m───────────────────── \x1b[0m\x1b[3;34mlocals\x1b[0m\x1b[34m ─────────────────────\x1b[0m\x1b[34m─╮\x1b[0m \n \x1b[34m│\x1b[0m \x1b[3;33mconsole\x1b[0m\x1b[31m =\x1b[0m \x1b[1m<\x1b[0m\x1b[1;95mconsole\x1b[0m\x1b[39m \x1b[0m\x1b[3;33mwidth\x1b[0m\x1b[39m=\x1b[0m\x1b[1;34m80\x1b[0m\x1b[39m ColorSystem.TRUECOLOR\x1b[0m\x1b[1m>\x1b[0m \x1b[34m│\x1b[0m \n \x1b[34m╰────────────────────────────────────────────────────╯\x1b[0m \n"
|
||||
"\n\x1b[2;36m[TIME]\x1b[0m\x1b[2;36m \x1b[0mHello from \x1b[1m<\x1b[0m\x1b[1;95mconsole\x1b[0m\x1b[39m \x1b[0m\x1b[3;33mwidth\x1b[0m\x1b[39m=\x1b[0m\x1b[35m80\x1b[0m\x1b[39m ColorSystem.TRUECOLOR\x1b[0m\x1b[1m>\x1b[0m ! \x1b]8;id=0;foo\x1b\\\x1b[2mtest_log.py\x1b[0m\x1b]8;;\x1b\\\x1b[2m:34\x1b[0m\n\x1b[2;36m \x1b[0m\x1b[2;36m \x1b[0m\x1b[1m[\x1b[0m\x1b[1;34m1\x1b[0m, \x1b[1;34m2\x1b[0m, \x1b[1;34m3\x1b[0m\x1b[1m]\x1b[0m \x1b]8;id=0;foo\x1b\\\x1b[2mtest_log.py\x1b[0m\x1b]8;;\x1b\\\x1b[2m:35\x1b[0m\n \x1b[34m╭─\x1b[0m\x1b[34m───────────────────── \x1b[0m\x1b[3;34mlocals\x1b[0m\x1b[34m ─────────────────────\x1b[0m\x1b[34m─╮\x1b[0m \n \x1b[34m│\x1b[0m \x1b[3;33mconsole\x1b[0m\x1b[31m =\x1b[0m \x1b[1m<\x1b[0m\x1b[1;95mconsole\x1b[0m\x1b[39m \x1b[0m\x1b[3;33mwidth\x1b[0m\x1b[39m=\x1b[0m\x1b[35m80\x1b[0m\x1b[39m ColorSystem.TRUECOLOR\x1b[0m\x1b[1m>\x1b[0m \x1b[34m│\x1b[0m \n \x1b[34m╰────────────────────────────────────────────────────╯\x1b[0m \n"
|
||||
)
|
||||
rendered = render_log()
|
||||
print(repr(rendered))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue