SIGN IN SIGN UP
Textualize / rich UNCLAIMED

Rich is a Python library for rich text and beautiful formatting in the terminal.

55902 0 0 Python
2021-12-14 17:57:11 +00:00
from io import StringIO
2021-05-19 16:58:33 +01:00
2021-07-04 19:59:06 +01:00
import pytest
2024-09-28 20:19:10 +01:00
from rich.cells import cell_len
2022-01-07 11:49:21 +00:00
from rich.segment import ControlType, Segment, SegmentLines, Segments
2019-12-11 18:53:51 +00:00
from rich.style import Style
2019-12-11 18:35:25 +00:00
def test_repr():
2021-06-27 07:54:02 +01:00
assert repr(Segment("foo")) == "Segment('foo')"
home = (ControlType.HOME, 0)
assert (
repr(Segment("foo", None, [home]))
== "Segment('foo', None, [(<ControlType.HOME: 3>, 0)])"
)
2019-12-11 18:35:25 +00:00
def test_line():
assert Segment.line() == Segment("\n")
2019-12-11 18:53:51 +00:00
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_split_lines():
lines = [Segment("Hello\nWorld")]
assert list(Segment.split_lines(lines)) == [[Segment("Hello")], [Segment("World")]]
2026-01-23 15:27:15 +00:00
def test_split_lines_terminator():
lines = [Segment("Hello\nWorld")]
assert list(Segment.split_lines_terminator(lines)) == [
([Segment("Hello")], True),
([Segment("World")], False),
]
2026-01-23 15:28:10 +00:00
def test_split_lines_terminator_single_line():
lines = [Segment("Hello")]
assert list(Segment.split_lines_terminator(lines)) == [
([Segment("Hello")], False),
]
2019-12-11 18:53:51 +00:00
def test_split_and_crop_lines():
assert list(
Segment.split_and_crop_lines([Segment("Hello\nWorld!\n"), Segment("foo")], 4)
2020-03-01 22:44:00 +00:00
) == [
[Segment("Hell"), Segment("\n", None)],
[Segment("Worl"), Segment("\n", None)],
[Segment("foo"), Segment(" ")],
]
2019-12-11 18:53:51 +00:00
2019-12-24 12:48:57 +00:00
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
2019-12-11 18:53:51 +00:00
def test_get_line_length():
assert Segment.get_line_length([Segment("foo"), Segment("bar")]) == 6
2019-12-24 12:48:57 +00:00
def test_get_shape():
assert Segment.get_shape([[Segment("Hello")]]) == (5, 1)
assert Segment.get_shape([[Segment("Hello")], [Segment("World!")]]) == (6, 2)
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_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([])) == []
2020-05-14 20:15:31 +01:00
def test_filter_control():
control_code = (ControlType.HOME, 0)
segments = [Segment("foo"), Segment("bar", None, (control_code,))]
2020-05-14 20:15:31 +01:00
assert list(Segment.filter_control(segments)) == [Segment("foo")]
assert list(Segment.filter_control(segments, is_control=True)) == [
Segment("bar", None, (control_code,))
2020-05-14 20:15:31 +01:00
]
2020-10-10 18:12:44 +01:00
def test_strip_styles():
segments = [Segment("foo", Style(bold=True))]
assert list(Segment.strip_styles(segments)) == [Segment("foo", None)]
def test_strip_links():
segments = [Segment("foo", Style(bold=True, link="https://www.example.org"))]
2020-10-10 18:39:50 +01:00
assert list(Segment.strip_links(segments)) == [Segment("foo", Style(bold=True))]
2021-01-09 16:08:36 +00:00
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),
2021-01-09 16:15:15 +00:00
]
2021-03-19 21:37:47 +00:00
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
2021-06-09 15:39:58 +01:00
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(),
]
2021-06-27 16:01:12 +01:00
def test_divide():
bold = Style(bold=True)
italic = Style(italic=True)
segments = [
Segment("Hello", bold),
Segment(" World!", italic),
]
assert list(Segment.divide(segments, [])) == []
2021-07-05 14:56:42 +01:00
assert list(Segment.divide([], [1])) == [[]]
2021-06-27 16:01:12 +01:00
assert list(Segment.divide(segments, [1])) == [[Segment("H", bold)]]
assert list(Segment.divide(segments, [1, 2])) == [
[Segment("H", bold)],
[Segment("e", bold)],
]
assert list(Segment.divide(segments, [1, 2, 12])) == [
[Segment("H", bold)],
[Segment("e", bold)],
[Segment("llo", bold), Segment(" World!", italic)],
]
2021-07-12 19:09:08 +01:00
assert list(Segment.divide(segments, [4, 20])) == [
[Segment("Hell", bold)],
[Segment("o", bold), Segment(" World!", italic)],
]
2021-06-27 16:01:12 +01:00
2022-09-19 11:27:11 +01:00
# https://github.com/textualize/rich/issues/1755
2021-12-14 17:57:11 +00:00
def test_divide_complex():
MAP = (
"[on orange4] [on green]XX[on orange4] \n"
" \n"
" \n"
" \n"
" [bright_red on black]Y[on orange4] \n"
"[on green]X[on orange4] [on green]X[on orange4] \n"
" [on green]X[on orange4] [on green]X\n"
"[on orange4] \n"
" [on green]XX[on orange4] \n"
)
from rich.console import Console
2022-01-07 11:49:21 +00:00
from rich.text import Text
2021-12-14 17:57:11 +00:00
text = Text.from_markup(MAP)
console = Console(
color_system="truecolor", width=30, force_terminal=True, file=StringIO()
)
console.print(text)
result = console.file.getvalue()
print(repr(result))
expected = "\x1b[48;5;94m \x1b[0m\x1b[42mXX\x1b[0m\x1b[48;5;94m \x1b[0m\n\x1b[48;5;94m \x1b[0m\n\x1b[48;5;94m \x1b[0m\n\x1b[48;5;94m \x1b[0m\n\x1b[48;5;94m \x1b[0m\x1b[91;40mY\x1b[0m\x1b[91;48;5;94m \x1b[0m\n\x1b[91;42mX\x1b[0m\x1b[91;48;5;94m \x1b[0m\x1b[91;42mX\x1b[0m\x1b[91;48;5;94m \x1b[0m\n\x1b[91;48;5;94m \x1b[0m\x1b[91;42mX\x1b[0m\x1b[91;48;5;94m \x1b[0m\x1b[91;42mX\x1b[0m\n\x1b[91;48;5;94m \x1b[0m\n\x1b[91;48;5;94m \x1b[0m\x1b[91;42mXX\x1b[0m\x1b[91;48;5;94m \x1b[0m\n\n"
assert result == expected
2021-06-27 16:01:12 +01:00
def test_divide_emoji():
bold = Style(bold=True)
italic = Style(italic=True)
segments = [
Segment("Hello", bold),
Segment("💩💩💩", italic),
]
assert list(Segment.divide(segments, [7])) == [
[Segment("Hello", bold), Segment("💩", italic)],
]
assert list(Segment.divide(segments, [8])) == [
[Segment("Hello", bold), Segment("💩 ", italic)],
]
assert list(Segment.divide(segments, [9])) == [
[Segment("Hello", bold), Segment("💩💩", italic)],
]
assert list(Segment.divide(segments, [8, 11])) == [
[Segment("Hello", bold), Segment("💩 ", italic)],
[Segment(" 💩", italic)],
]
assert list(Segment.divide(segments, [9, 11])) == [
[Segment("Hello", bold), Segment("💩💩", italic)],
[Segment("💩", italic)],
]
2021-07-04 20:52:35 +01:00
def test_divide_edge():
segments = [Segment("foo"), Segment("bar"), Segment("baz")]
result = list(Segment.divide(segments, [1, 3, 9]))
print(result)
assert result == [
[Segment("f")],
[Segment("oo")],
[Segment("bar"), Segment("baz")],
]
2021-07-04 21:11:00 +01:00
def test_divide_edge_2():
segments = [
Segment("╭─"),
Segment(
"────── Placeholder ───────",
),
Segment(
"─╮",
),
]
result = list(Segment.divide(segments, [30, 60]))
expected = [segments, []]
print(repr(result))
assert result == expected
2021-07-04 19:59:06 +01:00
@pytest.mark.parametrize(
"text,split,result",
[
2021-11-25 20:47:09 +00:00
("XX", 4, (Segment("XX"), Segment(""))),
2021-07-04 19:59:06 +01:00
("X", 1, (Segment("X"), Segment(""))),
("💩", 1, (Segment(" "), Segment(" "))),
("XY", 1, (Segment("X"), Segment("Y"))),
("💩X", 1, (Segment(" "), Segment(" X"))),
("💩💩", 1, (Segment(" "), Segment(" 💩"))),
("X💩Y", 2, (Segment("X "), Segment(" Y"))),
("X💩YZ", 2, (Segment("X "), Segment(" YZ"))),
("X💩💩Z", 2, (Segment("X "), Segment(" 💩Z"))),
("X💩💩Z", 3, (Segment("X💩"), Segment("💩Z"))),
("X💩💩Z", 4, (Segment("X💩 "), Segment(" Z"))),
("X💩💩Z", 5, (Segment("X💩💩"), Segment("Z"))),
("X💩💩Z", 6, (Segment("X💩💩Z"), Segment(""))),
("XYZABC💩💩", 6, (Segment("XYZABC"), Segment("💩💩"))),
("XYZABC💩💩", 7, (Segment("XYZABC "), Segment(" 💩"))),
("XYZABC💩💩", 8, (Segment("XYZABC💩"), Segment("💩"))),
("XYZABC💩💩", 9, (Segment("XYZABC💩 "), Segment(" "))),
("XYZABC💩💩", 10, (Segment("XYZABC💩💩"), Segment(""))),
("💩💩💩💩💩", 3, (Segment("💩 "), Segment(" 💩💩💩"))),
("💩💩💩💩💩", 4, (Segment("💩💩"), Segment("💩💩💩"))),
2021-07-04 20:09:30 +01:00
("💩X💩Y💩Z💩A💩", 4, (Segment("💩X "), Segment(" Y💩Z💩A💩"))),
("XYZABC", 4, (Segment("XYZA"), Segment("BC"))),
("XYZABC", 5, (Segment("XYZAB"), Segment("C"))),
2023-01-06 14:14:48 +00:00
(
"a1あbcdaef",
9,
(Segment("a1あb"), Segment("cdaef")),
),
2021-07-04 19:59:06 +01:00
],
)
def test_split_cells_emoji(text, split, result):
assert Segment(text).split_cells(split) == result
2024-10-04 12:38:55 +01:00
@pytest.mark.parametrize(
"segment",
[
2024-10-04 11:55:26 +01:00
Segment("早乙女リリエル (CV: 徳井青)"),
2024-10-04 12:38:55 +01:00
Segment("メイド・イン・きゅんクチュアリ☆ "),
2024-10-04 11:55:26 +01:00
Segment("TVアニメ「メルクストーリア -無気力少年と瓶の中の少女-」 主題歌CD"),
2024-10-04 12:38:55 +01:00
Segment("南無阿弥JKうらめしや? "),
Segment("メルク (CV: 水瀬いのり) "),
2024-10-04 12:41:32 +01:00
Segment(" メルク (CV: 水瀬いのり) "),
Segment(" メルク (CV: 水瀬いのり) "),
Segment(" メルク (CV: 水瀬いのり) "),
2024-10-04 12:38:55 +01:00
],
)
def test_split_cells_mixed(segment: Segment) -> None:
"""Check that split cells splits on cell positions."""
# Caused https://github.com/Textualize/textual/issues/4996 in Textual
for position in range(0, segment.cell_length + 1):
left, right = Segment.split_cells(segment, position)
assert all(
cell_len(c) > 0 for c in segment.text
) # Sanity check there aren't any sneaky control codes
assert cell_len(left.text) == position
assert cell_len(right.text) == segment.cell_length - position
2024-09-28 20:34:29 +01:00
def test_split_cells_doubles() -> None:
"""Check that split cells splits on cell positions with all double width characters."""
test = Segment("" * 20)
for position in range(1, test.cell_length):
left, right = Segment.split_cells(test, position)
assert cell_len(left.text) == position
assert cell_len(right.text) == test.cell_length - position
def test_split_cells_single() -> None:
"""Check that split cells splits on cell positions with all single width characters."""
test = Segment("A" * 20)
for position in range(1, test.cell_length):
left, right = Segment.split_cells(test, position)
assert cell_len(left.text) == position
assert cell_len(right.text) == test.cell_length - position
2024-09-28 20:19:10 +01:00
2021-06-27 16:01:12 +01:00
def test_segment_lines_renderable():
lines = [[Segment("hello"), Segment(" "), Segment("world")], [Segment("foo")]]
segment_lines = SegmentLines(lines)
assert list(segment_lines.__rich_console__(None, None)) == [
Segment("hello"),
Segment(" "),
Segment("world"),
Segment("foo"),
]
segment_lines = SegmentLines(lines, new_lines=True)
assert list(segment_lines.__rich_console__(None, None)) == [
Segment("hello"),
Segment(" "),
Segment("world"),
Segment("\n"),
Segment("foo"),
Segment("\n"),
2021-07-04 12:14:38 +01:00
]
2022-01-07 11:49:21 +00:00
def test_align_top():
lines = [[Segment("X")]]
assert Segment.align_top(lines, 3, 1, Style()) == lines
assert Segment.align_top(lines, 3, 3, Style()) == [
[Segment("X")],
[Segment(" ", Style())],
[Segment(" ", Style())],
]
def test_align_middle():
lines = [[Segment("X")]]
assert Segment.align_middle(lines, 3, 1, Style()) == lines
assert Segment.align_middle(lines, 3, 3, Style()) == [
[Segment(" ", Style())],
[Segment("X")],
[Segment(" ", Style())],
]
def test_align_bottom():
lines = [[Segment("X")]]
assert Segment.align_bottom(lines, 3, 1, Style()) == lines
assert Segment.align_bottom(lines, 3, 3, Style()) == [
[Segment(" ", Style())],
[Segment(" ", Style())],
[Segment("X")],
]