added regex highlighter

This commit is contained in:
Will McGugan 2020-09-09 13:47:26 +01:00
parent 1cdcd1ae69
commit 455db4fe9a
3 changed files with 20 additions and 1 deletions

View File

@ -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 <willmcgugan@gmail.com>"]
license = "MIT"

View File

@ -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"),

View File

@ -84,6 +84,16 @@ class ReprHighlighter(RegexHighlighter):
]
class IPHighlighter(RegexHighlighter):
"""Highlights IP addresses."""
base_style = "ip."
highlights = [
r"(?P<v4>[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3})",
r"(?P<v6>(([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"
)
)