def test_link_id(): assert Style().link_id == "" assert Style.parse("").link_id == "" assert Style.parse("red").link_id == "" style = Style.parse("red link https://example.org") assert isinstance(style.link_id, str) assert len(style.link_id) > 1
def test_decode(): console = Console(force_terminal=True, legacy_windows=False, color_system="truecolor") console.begin_capture() console.print("Hello") console.print("[b]foo[/b]") console.print("[link http://example.org]bar") console.print("[#ff0000 on color(200)]red") console.print("[color(200) on #ff0000]red") terminal_codes = console.end_capture() decoder = AnsiDecoder() lines = list(decoder.decode(terminal_codes)) expected = [ Text("Hello"), Text("foo", spans=[Span(0, 3, Style.parse("bold"))]), Text("bar", spans=[Span(0, 3, Style.parse("link http://example.org"))]), Text("red", spans=[Span(0, 3, Style.parse("#ff0000 on color(200)"))]), Text("red", spans=[Span(0, 3, Style.parse("color(200) on #ff0000"))]), ] assert lines == expected
def test_get_number_styles(): syntax = Syntax(CODE, "python", theme="monokai", line_numbers=True) console = Console(color_system="windows") assert syntax._get_number_styles(console=console) == ( Style.parse("on #272822"), Style.parse("dim on #272822"), Style.parse("not dim on #272822"), )
def test_theme_stack(): theme = Theme({"warning": "red"}) stack = ThemeStack(theme) assert stack.get("warning") == Style.parse("red") new_theme = Theme({"warning": "bold yellow"}) stack.push_theme(new_theme) assert stack.get("warning") == Style.parse("bold yellow") stack.pop_theme() assert stack.get("warning") == Style.parse("red") with pytest.raises(ThemeStackError): stack.pop_theme()
def test_highlight_background_color(): syntax = Syntax( CODE, lexer_name="python", line_numbers=True, line_range=(2, 10), theme="foo", code_width=60, word_wrap=True, background_color="red", ) assert syntax.highlight(CODE).style == Style.parse("on red")
def test_rich_console(live_render): options = ConsoleOptions( ConsoleDimensions(80, 25), legacy_windows=False, min_width=10, max_width=20, is_terminal=False, encoding="utf-8", ) rich_console = live_render.__rich_console__(Console(), options) assert [Segment("my string", None)] == list(rich_console) live_render.style = "red" rich_console = live_render.__rich_console__(Console(), options) assert [Segment("my string", Style.parse("red"))] == list(rich_console)
def test_lines_justify(): console = Console() lines1 = Lines([Text("foo", style="b"), Text("test", style="b")]) lines1.justify(console, 10, justify="left") assert lines1._lines == [Text("foo "), Text("test ")] lines1.justify(console, 10, justify="center") assert lines1._lines == [Text(" foo "), Text(" test ")] lines1.justify(console, 10, justify="right") assert lines1._lines == [Text(" foo"), Text(" test")] lines2 = Lines([Text("foo bar", style="b"), Text("test", style="b")]) lines2.justify(console, 7, justify="full") print(repr(lines2._lines[0].spans)) assert lines2._lines == [ Text( "foo bar", spans=[ Span(0, 3, "b"), Span(3, 4, Style.parse("bold")), Span(4, 7, "b") ], ), Text("test"), ]
def get_style(text: str) -> Style: return Style.parse( f"bold yellow link https://cve.mitre.org/cgi-bin/cvekey.cgi?keyword={text}" )
def test_get_pulse_segments(): bar = ProgressBar() segments = bar._get_pulse_segments( Style.parse("red"), Style.parse("yellow"), None, False, False ) print(repr(segments)) expected = [ Segment("━", Style.parse("red")), Segment("━", Style.parse("red")), Segment("━", Style.parse("red")), Segment("━", Style.parse("red")), Segment("━", Style.parse("red")), Segment("━", Style.parse("red")), Segment("━", Style.parse("red")), Segment("━", Style.parse("red")), Segment("━", Style.parse("red")), Segment("━", Style.parse("red")), Segment("━", Style.parse("yellow")), Segment("━", Style.parse("yellow")), Segment("━", Style.parse("yellow")), Segment("━", Style.parse("yellow")), Segment("━", Style.parse("yellow")), Segment("━", Style.parse("yellow")), Segment("━", Style.parse("yellow")), Segment("━", Style.parse("yellow")), Segment("━", Style.parse("yellow")), Segment("━", Style.parse("yellow")), ] assert segments == expected
from mudrich.style import Style from mudrich.theme import Theme from mudrich.highlighter import RegexHighlighter class RequestHighlighter(RegexHighlighter): base_style = "req." highlights = [ r"^(?P<protocol>\w+) (?P<method>\w+) (?P<path>\S+) (?P<result>\w+) (?P<stats>\[.+\])$", r"\/(?P<filename>\w+\..{3,4})", ] theme = Theme( { "req.protocol": Style.parse("dim bold green"), "req.method": Style.parse("bold cyan"), "req.path": Style.parse("magenta"), "req.filename": Style.parse("bright_magenta"), "req.result": Style.parse("yellow"), "req.stats": Style.parse("dim"), } ) console = Console(theme=theme) console.log("Server starting...") console.log("Serving on http://127.0.0.1:8000") time.sleep(1) request_highlighter = RequestHighlighter()
def test_parse(): assert Style.parse("") == Style() assert Style.parse("red") == Style(color="red") assert Style.parse("not bold") == Style(bold=False) assert Style.parse("bold red on black") == Style(color="red", bgcolor="black", bold=True) assert Style.parse("bold link https://example.org") == Style( bold=True, link="https://example.org") with pytest.raises(errors.StyleSyntaxError): Style.parse("on") with pytest.raises(errors.StyleSyntaxError): Style.parse("on nothing") with pytest.raises(errors.StyleSyntaxError): Style.parse("rgb(999,999,999)") with pytest.raises(errors.StyleSyntaxError): Style.parse("not monkey") with pytest.raises(errors.StyleSyntaxError): Style.parse("link")
def test_bool(): assert bool(Style()) is False assert bool(Style(bold=True)) is True assert bool(Style(color="red")) is True assert bool(Style.parse("")) is False
def test_pygments_syntax_theme(): style = PygmentsSyntaxTheme("default") assert style.get_style_for_token("abc") == Style.parse("none")