From 455db4fe9a59508c382c594b9389fef2ab644152 Mon Sep 17 00:00:00 2001 From: Will McGugan Date: Wed, 9 Sep 2020 13:47:26 +0100 Subject: [PATCH] added regex highlighter --- pyproject.toml | 2 +- rich/default_styles.py | 2 ++ rich/highlighter.py | 17 +++++++++++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index c03857c4..a3f4e133 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -2,7 +2,7 @@ name = "rich" homepage = "https://github.com/willmcgugan/rich" documentation = "https://rich.readthedocs.io/en/latest/" -version = "6.1.0" +version = "6.1.1" description = "Render rich text, tables, progress bars, syntax highlighting, markdown and more to the terminal" authors = ["Will McGugan "] license = "MIT" diff --git a/rich/default_styles.py b/rich/default_styles.py index 4a7362ff..bf9e23ba 100644 --- a/rich/default_styles.py +++ b/rich/default_styles.py @@ -44,6 +44,8 @@ DEFAULT_STYLES: Dict[str, Style] = { "inspect.equals": Style(), "inspect.help": Style(color="cyan"), "inspect.doc": Style(dim=True), + "ip.v4": Style(bold=True, color="bright_green"), + "ip.v6": Style(bold=True, color="bright_green"), "logging.keyword": Style(bold=True, color="yellow"), "logging.level.notset": Style(dim=True), "logging.level.debug": Style(color="green"), diff --git a/rich/highlighter.py b/rich/highlighter.py index 13fc0ac1..0ac74d7f 100644 --- a/rich/highlighter.py +++ b/rich/highlighter.py @@ -84,6 +84,16 @@ class ReprHighlighter(RegexHighlighter): ] +class IPHighlighter(RegexHighlighter): + """Highlights IP addresses.""" + + base_style = "ip." + highlights = [ + r"(?P[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3})", + r"(?P(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])))", + ] + + if __name__ == "__main__": # pragma: no cover from .console import Console @@ -109,3 +119,10 @@ if __name__ == "__main__": # pragma: no cover console.print(1234567.34) console.print(1 / 2) console.print(-1 / 123123123123) + + regex_highlighter = IPHighlighter() + console.print( + regex_highlighter( + "127.0.1.1 bar 192.168.1.4 2001:0db8:85a3:0000:0000:8a2e:0370:7334 foo" + ) + )