Skip to content

Commit 54d224f

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 54d224f

File tree

4 files changed

+25
-2
lines changed

4 files changed

+25
-2
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## Unreleased
9+
10+
### Fixed
11+
12+
- Fixed highlighting of Windows paths in tracebacks. https://github.com/Textualize/rich/pull/3734
13+
814
## [14.0.0] - 2025-03-30
915

1016
### Added

CONTRIBUTORS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ The following people have contributed to the development of Rich:
4646
- [Paul McGuire](https://github.com/ptmcg)
4747
- [Antony Milne](https://github.com/AntonyMilneQB)
4848
- [Michael Milton](https://github.com/multimeric)
49+
- [Zoltan Nagy](https://github.com/abesto)
4950
- [Martina Oefelein](https://github.com/oefe)
5051
- [Nathan Page](https://github.com/nathanrpage97)
5152
- [Dave Pearson](https://github.com/davep/)

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)