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)), ]
def test_filter_control(): control_code = (ControlType.HOME, 0) segments = [Segment("foo"), Segment("bar", None, (control_code, ))] assert list(Segment.filter_control(segments)) == [Segment("foo")] assert list(Segment.filter_control(segments, is_control=True)) == [ Segment("bar", None, (control_code, )) ]
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), ]
def test_segments_renderable(): segments = Segments([Segment("foo")]) assert list(segments.__rich_console__(None, None)) == [Segment("foo")] segments = Segments([Segment("foo")], new_lines=True) assert list(segments.__rich_console__(None, None)) == [ Segment("foo"), Segment.line(), ]
def test_control_move(): assert Control.move(0, 0).segment == Segment("", None, []) control = Control.move(3, 4) print(repr(control.segment)) assert control.segment == Segment( "\x1b[3C\x1b[4B", None, [(ControlType.CURSOR_FORWARD, 3), (ControlType.CURSOR_DOWN, 4)], )
def test_repr(): assert repr(Segment("foo")) == "Segment('foo', None)" home = (ControlType.HOME, 0) if sys.version_info >= (3, 10): assert (repr(Segment( "foo", None, [home])) == "Segment('foo', None, [(ControlType.HOME, 0)])") else: assert (repr(Segment( "foo", None, [home])) == "Segment('foo', None, [(<ControlType.HOME: 3>, 0)])")
def test_screen_update_class(): screen_update = ScreenUpdate([[Segment("foo")], [Segment("bar")]], 5, 10) assert screen_update.x == 5 assert screen_update.y == 10 console = Console(force_terminal=True) console.begin_capture() console.print(screen_update) result = console.end_capture() print(repr(result)) expected = "\x1b[11;6Hfoo\x1b[12;6Hbar" assert result == expected
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_move_to_row(): print(repr(Control.move_to_row(10, 20).segment)) assert Control.move_to_row(10, 20).segment == Segment( "\x1b[12G\x1b[20B", None, [(ControlType.CURSOR_MOVE_TO_ROW, 11), (ControlType.CURSOR_DOWN, 20)], )
def test_split_and_crop_lines(): assert list( Segment.split_and_crop_lines( [Segment("Hello\nWorld!\n"), Segment("foo")], 4)) == [ [Segment("Hell"), Segment("\n", None)], [Segment("Worl"), Segment("\n", None)], [Segment("foo"), Segment(" ")], ]
def test_simplify(): assert list( Segment.simplify([Segment("Hello"), Segment(" "), Segment("World!")])) == [Segment("Hello World!")] assert list( Segment.simplify([ Segment("Hello", "red"), Segment(" ", "red"), Segment("World!", "blue") ])) == [Segment("Hello ", "red"), Segment("World!", "blue")] assert list(Segment.simplify([])) == []
def test_set_shape(): assert Segment.set_shape([[Segment("Hello")]], 10) == [[Segment("Hello"), Segment(" ")]] assert Segment.set_shape([[Segment("Hello")]], 10, 2) == [ [Segment("Hello"), Segment(" ")], [Segment(" " * 10)], ]
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
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()
def test_adjust_line_length(): line = [Segment("Hello", "foo")] assert Segment.adjust_line_length(line, 10, style="bar") == [ Segment("Hello", "foo"), Segment(" ", "bar"), ] line = [Segment("H"), Segment("ello, World!")] assert Segment.adjust_line_length(line, 5) == [Segment("H"), Segment("ello")] line = [Segment("Hello")] assert Segment.adjust_line_length(line, 5) == line
def test_strip_links(): segments = [ Segment("foo", Style(bold=True, link="https://www.example.org")) ] assert list( Segment.strip_links(segments)) == [Segment("foo", Style(bold=True))]
def test_strip_styles(): segments = [Segment("foo", Style(bold=True))] assert list(Segment.strip_styles(segments)) == [Segment("foo", None)]
def test_control_move_to(): control = Control.move_to(5, 10) print(control.segment) assert control.segment == Segment("\x1b[11;6H", None, [(ControlType.CURSOR_MOVE_TO, 5, 10)])
def test_is_control(): assert Segment("foo", Style(bold=True)).is_control == False assert Segment("foo", Style(bold=True), []).is_control == True assert Segment("foo", Style(bold=True), [(ControlType.HOME, 0)]).is_control == True
def test_line(): assert Segment.line() == Segment("\n")
def test_split_lines(): lines = [Segment("Hello\nWorld")] assert list(Segment.split_lines(lines)) == [[Segment("Hello")], [Segment("World")]]
def test_get_shape(): assert Segment.get_shape([[Segment("Hello")]]) == (5, 1) assert Segment.get_shape([[Segment("Hello")], [Segment("World!")]]) == (6, 2)
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
def test_get_line_length(): assert Segment.get_line_length([Segment("foo"), Segment("bar")]) == 6