Skip to content

Commit 0d3d5cf

Browse files
author
Zoltán Nagy
committed
fix(traceback): highlight win32 paths
`PathHighlighter` uses a regex to parse paths for highlighting. It used `/` as the final path component delimiter. This diff changes that to "either `/` or `\`". I toyed with using `os.path.sep`, but there's no reason to *not* highlight paths from other OSes than the one we're running on at the moment.
1 parent 8732dc5 commit 0d3d5cf

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

rich/traceback.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ class Trace:
246246

247247

248248
class PathHighlighter(RegexHighlighter):
249-
highlights = [r"(?P<dim>.*/)(?P<bold>.+)"]
249+
highlights = [r"(?P<dim>.*(/|\\))(?P<bold>.+)"]
250250

251251

252252
class Traceback:

tests/test_traceback.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
import pytest
77

88
from rich.console import Console
9+
from rich.text import Span
910
from rich.theme import Theme
10-
from rich.traceback import Traceback, install
11+
from rich.traceback import install, PathHighlighter, Traceback
1112

1213

1314
def test_handler():
@@ -373,3 +374,18 @@ def test_notes() -> None:
373374
traceback = Traceback()
374375

375376
assert traceback.trace.stacks[0].notes == ["Hello", "World"]
377+
378+
379+
def test_path_highlighter() -> None:
380+
"""Check that PathHighlighter correctly highlights both win32 and *nix paths"""
381+
path_highlighter = PathHighlighter()
382+
383+
assert path_highlighter("/foo/bar/baz").spans == [
384+
Span(0, 9, "dim"),
385+
Span(9, 12, "bold"),
386+
]
387+
388+
assert path_highlighter("'\\\\?\\C:\\foo\\bar\\baz").spans == [
389+
Span(0, 16, "dim"),
390+
Span(16, 19, "bold"),
391+
]

0 commit comments

Comments
 (0)