diff --git a/CHANGELOG.md b/CHANGELOG.md index 2cd0594b..29f7aaa4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,11 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). -## [8.0.1] - unreleased +## [8.1.0] - unreleased + +### Added + +- Added highlighting of EUI-48 and EUI-64 (MAC addresses) ### Changed diff --git a/rich/default_styles.py b/rich/default_styles.py index e320e3ca..ce161437 100644 --- a/rich/default_styles.py +++ b/rich/default_styles.py @@ -61,6 +61,8 @@ DEFAULT_STYLES: Dict[str, Style] = { "repr.comma": Style(bold=True), "repr.ipv4": Style(bold=True, color="bright_green"), "repr.ipv6": Style(bold=True, color="bright_green"), + "repr.eui48": Style(bold=True, color="bright_green"), + "repr.eui64": Style(bold=True, color="bright_green"), "repr.tag_start": Style(bold=True), "repr.tag_name": Style(color="bright_magenta", bold=True), "repr.tag_contents": Style(color="default"), diff --git a/rich/highlighter.py b/rich/highlighter.py index 4a770aea..564127f7 100644 --- a/rich/highlighter.py +++ b/rich/highlighter.py @@ -80,6 +80,12 @@ class ReprHighlighter(RegexHighlighter): r"(?P\B(\/[\w\.\-\_\+]+)*\/)(?P[\w\.\-\_\+]*)?", r"(?P[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})", r"(?P([A-Fa-f0-9]{1,4}::?){1,7}[A-Fa-f0-9]{1,4})", + r"(?P([0-9A-Fa-f]{1,2}-){5}[0-9A-Fa-f]{1,2})", # EUI-48 6x2 hyphen + r"(?P([0-9A-Fa-f]{1,2}-){7}[0-9A-Fa-f]{1,2})", # EUI-64 8x2 hyphen + r"(?P([0-9A-Fa-f]{1,2}:){5}[0-9A-Fa-f]{1,2})", # EUI-48 6x2 colon + r"(?P([0-9A-Fa-f]{1,2}:){7}[0-9A-Fa-f]{1,2})", # EUI-64 8x2 colon + r"(?P([0-9A-Fa-f]{4}\.){2}[0-9A-Fa-f]{4})", # EUI-48 3x4 dot + r"(?P([0-9A-Fa-f]{4}\.){3}[0-9A-Fa-f]{4})", # EUI-64 4x4 dot r"(?b?\'\'\'.*?(?https?:\/\/[0-9a-zA-Z\$\-\_\+\!`\(\)\,\.\?\/\;\:\&\=\%\#]*)", r"(?P[a-fA-F0-9]{8}\-[a-fA-F0-9]{4}\-[a-fA-F0-9]{4}\-[a-fA-F0-9]{4}\-[a-fA-F0-9]{12})", diff --git a/tests/test_highlighter.py b/tests/test_highlighter.py index 5e3798e8..242452db 100644 --- a/tests/test_highlighter.py +++ b/tests/test_highlighter.py @@ -1,8 +1,34 @@ +"""Tests for the higlighter classes.""" import pytest -from rich.highlighter import NullHighlighter + +from rich.highlighter import NullHighlighter, ReprHighlighter +from rich.text import Span, Text def test_wrong_type(): highlighter = NullHighlighter() with pytest.raises(TypeError): highlighter([]) + + +@pytest.mark.parametrize( + "style_name, test_str", + [ + ("repr.eui48", "01-23-45-67-89-AB"), # 6x2 hyphen + ("repr.eui64", "01-23-45-FF-FE-67-89-AB"), # 8x2 hyphen + ("repr.eui48", "01:23:45:67:89:AB"), # 6x2 colon + ("repr.eui64", "01:23:45:FF:FE:67:89:AB"), # 8x2 colon + ("repr.eui48", "0123.4567.89AB"), # 3x4 dot + ("repr.eui64", "0123.45FF.FE67.89AB"), # 4x4 dot + ("repr.eui48", "ed-ed-ed-ed-ed-ed"), # lowercase + ("repr.eui48", "ED-ED-ED-ED-ED-ED"), # uppercase + ("repr.eui48", "Ed-Ed-Ed-Ed-Ed-Ed"), # mixed case + ("repr.eui48", "0-00-1-01-2-02"), # dropped zero + ], +) +def test_highlight_regex(style_name: str, test_str: str): + """Tests for the regular expressions used in ReprHighlighter.""" + text = Text(test_str) + highlighter = ReprHighlighter() + highlighter.highlight(text) + assert text._spans[-1] == Span(0, len(test_str), style_name)