Skip to content

Commit

Permalink
Merge pull request #2799 from jayaddison/issue-2688/nf-escaping
Browse files Browse the repository at this point in the history
ANSI: nF-escape symbol "(" decoding issue
  • Loading branch information
willmcgugan committed Mar 4, 2023
2 parents fa09487 + 24ce364 commit bb1a56b
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Use `Console(stderr=True)` in `rich.traceback.install` to support io redirection.
- Fixes superfluous spaces in html output https://github.com/Textualize/rich/issues/2832
- Fixed duplicate output in Jupyter https://github.com/Textualize/rich/pulls/2804
- Filter ANSI character-encoding-change codes in `Text.from_ansi` parser
- Fixes traceback failing when a frame filename is unreadable https://github.com/Textualize/rich/issues/2821

### Added
Expand Down
1 change: 1 addition & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,4 @@ The following people have contributed to the development of Rich:
- [Zhe Huang](https://github.com/onlyacat)
- [Ke Sun](https://github.com/ksun212)
- [Qiming Xu](https://github.com/xqm32)
- [James Addison](https://github.com/jayaddison)
3 changes: 3 additions & 0 deletions rich/ansi.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ def _ansi_tokenize(ansi_text: str) -> Iterable[_AnsiToken]:
if start > position:
yield _AnsiToken(ansi_text[position:start])
if sgr:
if sgr == "(":
position = end + 1
continue
if sgr.endswith("m"):
yield _AnsiToken("", sgr[1:-1], osc)
else:
Expand Down
23 changes: 23 additions & 0 deletions tests/test_ansi.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import pytest

from rich.ansi import AnsiDecoder
from rich.console import Console
from rich.style import Style
Expand Down Expand Up @@ -45,3 +47,24 @@ def test_decode_example():
print(repr(result))
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"
assert result == expected


@pytest.mark.parametrize(
"ansi_bytes, expected_text",
[
# https://github.com/Textualize/rich/issues/2688
(
b"\x1b[31mFound 4 errors in 2 files (checked 18 source files)\x1b(B\x1b[m\n",
"Found 4 errors in 2 files (checked 18 source files)",
),
# https://mail.python.org/pipermail/python-list/2007-December/424756.html
(b"Hallo", "Hallo"),
(b"\x1b(BHallo", "Hallo"),
(b"\x1b(JHallo", "Hallo"),
(b"\x1b(BHal\x1b(Jlo", "Hallo"),
],
)
def test_decode_issue_2688(ansi_bytes, expected_text):
text = Text.from_ansi(ansi_bytes.decode())

assert str(text) == expected_text

0 comments on commit bb1a56b

Please sign in to comment.