Пример #1
0
def test_apply_style():
    segments = [Segment("foo"), Segment("bar", Style(bold=True))]
    assert Segment.apply_style(segments, None) is segments
    assert list(Segment.apply_style(segments, Style(italic=True))) == [
        Segment("foo", Style(italic=True)),
        Segment("bar", Style(italic=True, bold=True)),
    ]
Пример #2
0
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
Пример #3
0
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
Пример #4
0
def test_style_stack():
    stack = StyleStack(Style(color="red"))
    repr(stack)
    assert stack.current == Style(color="red")
    stack.push(Style(bold=True))
    assert stack.current == Style(color="red", bold=True)
    stack.pop()
    assert stack.current == Style(color="red")
Пример #5
0
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"),
    )
Пример #6
0
def test_without_color():
    style = Style(bold=True, color="red", bgcolor="blue")
    colorless_style = style.without_color
    assert colorless_style.color == None
    assert colorless_style.bgcolor == None
    assert colorless_style.bold == True
    null_style = Style.null()
    assert null_style.without_color == null_style
Пример #7
0
def test_remove_color():
    segments = [
        Segment("foo", Style(bold=True, color="red")),
        Segment("bar", None),
    ]
    assert list(Segment.remove_color(segments)) == [
        Segment("foo", Style(bold=True)),
        Segment("bar", None),
    ]
Пример #8
0
def test_str():
    assert str(Style(bold=False)) == "not bold"
    assert str(Style(color="red", bold=False)) == "not bold red"
    assert str(Style(color="red", bold=False,
                     italic=True)) == "not bold italic red"
    assert str(Style()) == "none"
    assert str(Style(bold=True)) == "bold"
    assert str(Style(color="red", bold=True)) == "bold red"
    assert str(Style(color="red", bgcolor="black",
                     bold=True)) == "bold red on black"
    all_styles = Style(
        color="red",
        bgcolor="black",
        bold=True,
        dim=True,
        italic=True,
        underline=True,
        blink=True,
        blink2=True,
        reverse=True,
        conceal=True,
        strike=True,
        underline2=True,
        frame=True,
        encircle=True,
        overline=True,
    )
    expected = "bold dim italic underline blink blink2 reverse conceal strike underline2 frame encircle overline red on black"
    assert str(all_styles) == expected
    assert str(Style(link="foo")) == "link foo"
Пример #9
0
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()
Пример #10
0
def test_rich() -> None:
    color = Color.parse("red")
    as_text = color.__rich__()
    print(repr(as_text))
    print(repr(as_text.spans))
    assert as_text == Text("<color 'red' (standard)⬤ >",
                           spans=[Span(23, 24, Style(color=color))])
Пример #11
0
def test_ansi_codes():
    all_styles = Style(
        color="red",
        bgcolor="black",
        bold=True,
        dim=True,
        italic=True,
        underline=True,
        blink=True,
        blink2=True,
        reverse=True,
        conceal=True,
        strike=True,
        underline2=True,
        frame=True,
        encircle=True,
        overline=True,
    )
    expected = "1;2;3;4;5;6;7;8;9;21;51;52;53;31;40"
    assert all_styles._make_ansi_codes(ColorSystem.TRUECOLOR) == expected
Пример #12
0
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")
Пример #13
0
 def __rich_console__(self, console: Console,
                      options: ConsoleOptions) -> RenderResult:
     for y in range(0, 5):
         for x in range(options.max_width):
             h = x / options.max_width
             l = 0.1 + ((y / 5) * 0.7)
             r1, g1, b1 = colorsys.hls_to_rgb(h, l, 1.0)
             r2, g2, b2 = colorsys.hls_to_rgb(h, l + 0.7 / 10, 1.0)
             bgcolor = Color.from_rgb(r1 * 255, g1 * 255, b1 * 255)
             color = Color.from_rgb(r2 * 255, g2 * 255, b2 * 255)
             yield Segment("▄", Style(color=color, bgcolor=bgcolor))
         yield Segment.line()
Пример #14
0
def test_get_line_color_none():
    style = PygmentsSyntaxTheme("default")
    style._background_style = Style(bgcolor=None)
    syntax = Syntax(
        CODE,
        lexer_name="python",
        line_numbers=True,
        line_range=(2, 10),
        theme=style,
        code_width=60,
        word_wrap=True,
        background_color="red",
    )
    assert syntax._get_line_numbers_color() == Color.default()
Пример #15
0
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)
Пример #16
0
def test_get_html_style():
    expected = "color: #7f7fbf; text-decoration-color: #7f7fbf; background-color: #800000; font-weight: bold; font-style: italic; text-decoration: underline; text-decoration: line-through; text-decoration: overline"
    html_style = Style(
        reverse=True,
        dim=True,
        color="red",
        bgcolor="blue",
        bold=True,
        italic=True,
        underline=True,
        strike=True,
        overline=True,
    ).get_html_style()
    print(repr(html_style))
    assert html_style == expected
Пример #17
0
def test_get_style_for_token():
    # from pygments.style import Style as PygmentsStyle
    # pygments_style = PygmentsStyle()
    from pygments.style import Token

    style = PygmentsSyntaxTheme("default")
    style_dict = {Token.Text: Style(color=None)}
    style._style_cache = style_dict
    syntax = Syntax(
        CODE,
        lexer_name="python",
        line_numbers=True,
        line_range=(2, 10),
        theme=style,
        code_width=60,
        word_wrap=True,
        background_color="red",
    )
    assert syntax._get_line_numbers_color() == Color.default()
Пример #18
0
def test_rich_console():
    renderable = "test renderable"
    style = Style(color="red")
    options = ConsoleOptions(
        ConsoleDimensions(80, 25),
        legacy_windows=False,
        min_width=10,
        max_width=20,
        is_terminal=False,
        encoding="utf-8",
    )

    expected_outputs = [
        Segment(renderable, style=style),
        Segment(" " * (20 - len(renderable)), style=style),
        Segment("\n", style=None),
    ]
    padding_generator = Padding(renderable, style=style).__rich_console__(
        Console(), options)
    for output, expected in zip(padding_generator, expected_outputs):
        assert output == expected
Пример #19
0
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"),
    ]
Пример #20
0
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
Пример #21
0
def test_inherit():
    theme = Theme({"warning": "red"})
    assert theme.styles["warning"] == Style(color="red")
    assert theme.styles["dim"] == Style(dim=True)
Пример #22
0
def test_get_style_at_offset():
    console = Console()
    text = Text.from_markup("Hello [b]World[/b]")
    assert text.get_style_at_offset(console, 0) == Style()
    assert text.get_style_at_offset(console, 6) == Style(bold=True)
Пример #23
0
 def get_style(text: str) -> Style:
     return Style.parse(
         f"bold yellow link https://cve.mitre.org/cgi-bin/cvekey.cgi?keyword={text}"
     )
Пример #24
0
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
Пример #25
0
 def convert(self) -> Style:
     return Style(**self.export())
Пример #26
0
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()
Пример #27
0
def test_empty():
    assert Style.null() == Style()
Пример #28
0
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")
Пример #29
0
def test_bgcolor_property():
    assert Style(bgcolor="black").bgcolor == Color("black", ColorType.STANDARD,
                                                   0, None)
Пример #30
0
def test_color_property():
    assert Style(color="red").color == Color("red", ColorType.STANDARD, 1,
                                             None)