| 1 |
import pytest |
| 2 |
|
| 3 |
from rich.ansi import AnsiDecoder |
| 4 |
from rich.console import Console |
| 5 |
from rich.style import Style |
| 6 |
from rich.text import Span, Text |
| 7 |
|
| 8 |
|
| 9 |
def test_decode(): |
| 10 |
console = Console( |
| 11 |
force_terminal=True, legacy_windows=False, color_system="truecolor" |
| 12 |
) |
| 13 |
console.begin_capture() |
| 14 |
console.print("Hello") |
| 15 |
console.print("[b]foo[/b]") |
| 16 |
console.print("[link http://example.org]bar") |
| 17 |
console.print("[#ff0000 on color(200)]red") |
| 18 |
console.print("[color(200) on #ff0000]red") |
| 19 |
terminal_codes = console.end_capture() |
| 20 |
|
| 21 |
decoder = AnsiDecoder() |
| 22 |
lines = list(decoder.decode(terminal_codes)) |
| 23 |
print(repr(lines)) |
| 24 |
expected = [ |
| 25 |
Text("Hello"), |
| 26 |
Text("foo", spans=[Span(0, 3, Style.parse("bold"))]), |
| 27 |
Text("bar", spans=[Span(0, 3, Style.parse("link http://example.org"))]), |
| 28 |
Text("red", spans=[Span(0, 3, Style.parse("#ff0000 on color(200)"))]), |
| 29 |
Text("red", spans=[Span(0, 3, Style.parse("color(200) on #ff0000"))]), |
| 30 |
Text(""), |
| 31 |
] |
| 32 |
|
| 33 |
assert lines == expected |
| 34 |
|
| 35 |
|
| 36 |
def test_decode_example(): |
| 37 |
ansi_bytes = b"\x1b[01m\x1b[KC:\\Users\\stefa\\AppData\\Local\\Temp\\tmp3ydingba:\x1b[m\x1b[K In function '\x1b[01m\x1b[Kmain\x1b[m\x1b[K':\n\x1b[01m\x1b[KC:\\Users\\stefa\\AppData\\Local\\Temp\\tmp3ydingba:3:5:\x1b[m\x1b[K \x1b[01;35m\x1b[Kwarning: \x1b[m\x1b[Kunused variable '\x1b[01m\x1b[Ka\x1b[m\x1b[K' [\x1b[01;35m\x1b[K-Wunused-variable\x1b[m\x1b[K]\n 3 | int \x1b[01;35m\x1b[Ka\x1b[m\x1b[K=1;\n | \x1b[01;35m\x1b[K^\x1b[m\x1b[K\n" |
| 38 |
ansi_text = ansi_bytes.decode("utf-8") |
| 39 |
|
| 40 |
text = Text.from_ansi(ansi_text) |
| 41 |
|
| 42 |
console = Console( |
| 43 |
force_terminal=True, legacy_windows=False, color_system="truecolor" |
| 44 |
) |
| 45 |
with console.capture() as capture: |
| 46 |
console.print(text) |
| 47 |
result = capture.get() |
| 48 |
print(repr(result)) |
| 49 |
print(result) |
| 50 |
expected = "\x1b[1mC:\\Users\\stefa\\AppData\\Local\\Temp\\tmp3ydingba:\x1b[0m In function '\x1b[1mmain\x1b[0m':\n\x1b[1mC:\\Users\\stefa\\AppData\\Local\\Temp\\tmp3ydingba:3:5:\x1b[0m \x1b[1;35mwarning: \x1b[0munused variable '\x1b[1ma\x1b[0m' \n[\x1b[1;35m-Wunused-variable\x1b[0m]\n 3 | int \x1b[1;35ma\x1b[0m=1;\n | \x1b[1;35m^\x1b[0m\n\n" |
| 51 |
assert result == expected |
| 52 |
|
| 53 |
|
| 54 |
@pytest.mark.parametrize( |
| 55 |
"ansi_bytes, expected_text", |
| 56 |
[ |
| 57 |
# https://github.com/Textualize/rich/issues/2688 |
| 58 |
( |
| 59 |
b"\x1b[31mFound 4 errors in 2 files (checked 18 source files)\x1b(B\x1b[m\n", |
| 60 |
"Found 4 errors in 2 files (checked 18 source files)\n", |
| 61 |
), |
| 62 |
# https://mail.python.org/pipermail/python-list/2007-December/424756.html |
| 63 |
(b"Hallo", "Hallo"), |
| 64 |
(b"\x1b(BHallo", "Hallo"), |
| 65 |
(b"\x1b(JHallo", "Hallo"), |
| 66 |
(b"\x1b(BHal\x1b(Jlo", "Hallo"), |
| 67 |
], |
| 68 |
) |
| 69 |
def test_decode_issue_2688(ansi_bytes, expected_text): |
| 70 |
text = Text.from_ansi(ansi_bytes.decode()) |
| 71 |
|
| 72 |
assert str(text) == expected_text |
| 73 |
|
| 74 |
|
| 75 |
@pytest.mark.parametrize("code", [*"0123456789:;<=>?"]) |
| 76 |
def test_strip_private_escape_sequences(code): |
| 77 |
text = Text.from_ansi(f"\x1b{code}x") |
| 78 |
|
| 79 |
console = Console(force_terminal=True) |
| 80 |
|
| 81 |
with console.capture() as capture: |
| 82 |
console.print(text) |
| 83 |
|
| 84 |
expected = "x\n" |
| 85 |
|
| 86 |
assert capture.get() == expected |
| 87 |
|
| 88 |
|
| 89 |
def test_decode_newlines(): |
| 90 |
"""Test newlines are preserved. |
| 91 |
Regression test for https://github.com/Textualize/rich/issues/3577 |
| 92 |
""" |
| 93 |
assert Text.from_ansi("").plain == "" |
| 94 |
assert Text.from_ansi("\n").plain == "\n" |
| 95 |
assert Text.from_ansi("\n\n").plain == "\n\n" |
| 96 |
assert Text.from_ansi("Hello").plain == "Hello" |
| 97 |
assert Text.from_ansi("\nHello").plain == "\nHello" |
| 98 |
assert Text.from_ansi("Hello\n").plain == "Hello\n" |
| 99 |
assert Text.from_ansi("Hello\n\n").plain == "Hello\n\n" |
| 100 |
assert Text.from_ansi("Hello\nWorld").plain == "Hello\nWorld" |
| 101 |
assert Text.from_ansi("Hello\n\nWorld").plain == "Hello\n\nWorld" |
| 102 |
assert Text.from_ansi("Hello\nWorld\n").plain == "Hello\nWorld\n" |
| 103 |
assert Text.from_ansi("Hello\nWorld\n\n").plain == "Hello\nWorld\n\n" |
| 104 |
assert Text.from_ansi("\nHello\nWorld\n\n").plain == "\nHello\nWorld\n\n" |