rich/tests/test_ansi.py

33 lines
998 B
Python
Raw Permalink Normal View History

2020-11-05 18:41:49 +00:00
import io
from rich.ansi import AnsiDecoder
from rich.console import Console
from rich.style import Style
from rich.text import Span, Text
def test_decode():
2020-11-07 15:27:37 +00:00
console = Console(
force_terminal=True, legacy_windows=False, color_system="truecolor"
)
2020-11-05 18:41:49 +00:00
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")
2020-11-07 15:19:22 +00:00
console.print("[color(200) on #ff0000]red")
2020-11-05 18:41:49 +00:00
terminal_codes = console.end_capture()
decoder = AnsiDecoder()
lines = list(decoder.decode(terminal_codes))
expected = [
Text("Hello"),
2020-11-07 15:19:22 +00:00
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"))]),
2020-11-05 18:41:49 +00:00
]
2020-11-07 15:23:18 +00:00
assert lines == expected