Skip to content

Commit

Permalink
Merge pull request #2219 from Textualize/bugfix-inconsistent-coloring…
Browse files Browse the repository at this point in the history
…-of-complex-numbers

[highlighter] Add complex numbers pattern to our highlighter's `number` matching
  • Loading branch information
willmcgugan committed Apr 26, 2022
2 parents ed6ff7e + a02e5d6 commit a7e8a6b
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fixed markup escaping issue https://github.com/Textualize/rich/issues/2187
- Safari - Box appearing around SVG export https://github.com/Textualize/rich/pull/2201
- Fixed recursion error in Jupyter progress bars https://github.com/Textualize/rich/issues/2047
- Complex numbers are now identified by the highlighter https://github.com/Textualize/rich/issues/2214
- Fix crash on IDLE and forced is_terminal detection to False because IDLE can't do escape codes https://github.com/Textualize/rich/issues/2222

### Changed
Expand Down
1 change: 1 addition & 0 deletions rich/default_styles.py
Expand Up @@ -78,6 +78,7 @@
"repr.attrib_equal": Style(bold=True),
"repr.attrib_value": Style(color="magenta", italic=False),
"repr.number": Style(color="cyan", bold=True, italic=False),
"repr.number_complex": Style(color="cyan", bold=True, italic=False), # same
"repr.bool_true": Style(color="bright_green", italic=True),
"repr.bool_false": Style(color="bright_red", italic=True),
"repr.none": Style(color="magenta", italic=True),
Expand Down
1 change: 1 addition & 0 deletions rich/highlighter.py
Expand Up @@ -94,6 +94,7 @@ class ReprHighlighter(RegexHighlighter):
r"(?P<call>[\w.]*?)\(",
r"\b(?P<bool_true>True)\b|\b(?P<bool_false>False)\b|\b(?P<none>None)\b",
r"(?P<ellipsis>\.\.\.)",
r"(?P<number_complex>(?<!\w)(?:\-?[0-9]+\.?[0-9]*(?:e[-+]?\d+?)?)(?:[-+](?:[0-9]+\.?[0-9]*(?:e[-+]?\d+)?))?j)",
r"(?P<number>(?<!\w)\-?[0-9]+\.?[0-9]*(e[-+]?\d+?)?\b|0x[0-9a-fA-F]*)",
r"(?P<path>\B(/[-\w._+]+)*\/)(?P<filename>[-\w._+]*)?",
r"(?<![\\\w])(?P<str>b?'''.*?(?<!\\)'''|b?'.*?(?<!\\)'|b?\"\"\".*?(?<!\\)\"\"\"|b?\".*?(?<!\\)\")",
Expand Down
34 changes: 34 additions & 0 deletions tests/test_highlighter.py
Expand Up @@ -59,6 +59,40 @@ def test_wrong_type():
(" 1.2 ", [Span(1, 4, "repr.number")]),
(" 0xff ", [Span(1, 5, "repr.number")]),
(" 1e10 ", [Span(1, 5, "repr.number")]),
(" 1j ", [Span(1, 3, "repr.number_complex")]),
(" 3.14j ", [Span(1, 6, "repr.number_complex")]),
(
" (3.14+2.06j) ",
[
Span(1, 2, "repr.brace"),
Span(12, 13, "repr.brace"),
Span(2, 12, "repr.number_complex"),
],
),
(
" (3+2j) ",
[
Span(1, 2, "repr.brace"),
Span(6, 7, "repr.brace"),
Span(2, 6, "repr.number_complex"),
],
),
(
" (123456.4321-1234.5678j) ",
[
Span(1, 2, "repr.brace"),
Span(24, 25, "repr.brace"),
Span(2, 24, "repr.number_complex"),
],
),
(
" (-123123-2.1312342342423422e+25j) ",
[
Span(1, 2, "repr.brace"),
Span(33, 34, "repr.brace"),
Span(2, 33, "repr.number_complex"),
],
),
(" /foo ", [Span(1, 2, "repr.path"), Span(2, 5, "repr.filename")]),
(" /foo/bar.html ", [Span(1, 6, "repr.path"), Span(6, 14, "repr.filename")]),
("01-23-45-67-89-AB", [Span(0, 17, "repr.eui48")]), # 6x2 hyphen
Expand Down

0 comments on commit a7e8a6b

Please sign in to comment.