From b0b596caa7e45e48ef8c4f734bd943dbaeee9bb7 Mon Sep 17 00:00:00 2001 From: Sergey Serebryakov Date: Wed, 7 Oct 2020 20:58:23 -0700 Subject: [PATCH 1/7] Fix typos, comments, unused imports --- rich/console.py | 8 ++++---- rich/pretty.py | 2 +- rich/style.py | 2 +- rich/syntax.py | 6 +++--- rich/traceback.py | 6 ++---- 5 files changed, 11 insertions(+), 13 deletions(-) diff --git a/rich/console.py b/rich/console.py index 10873637..545a85cf 100644 --- a/rich/console.py +++ b/rich/console.py @@ -620,7 +620,7 @@ class Console: return ConsoleDimensions(80, 25) width, height = shutil.get_terminal_size() - # get_terminal_size can report 0, 0 if run from psuedo-terminal + # get_terminal_size can report 0, 0 if run from pseudo-terminal width = width or 80 height = height or 25 return ConsoleDimensions( @@ -749,7 +749,7 @@ class Console: is required, such as the Panel class which draws a border around any renderable object. Args: - renderables (Iterable[RenderableType]): Any object or objects renderable in the console. + renderable (RenderableType): Any object renderable in the console. options (Optional[ConsoleOptions], optional): Console options, or None to use self.options. Default to ``None``. style (Style, optional): Optional style to apply to renderables. Defaults to ``None``. pad (bool, optional): Pad lines shorter than render width. Defaults to ``True``. @@ -858,7 +858,7 @@ class Console: """Combined a number of renderables and text in to one renderable. Args: - renderables (Iterable[Union[str, ConsoleRenderable]]): Anything that Rich can render. + objects (Iterable[Any]): Anything that Rich can render. sep (str, optional): String to write between print data. Defaults to " ". end (str, optional): String to write at end of print data. Defaults to "\\n". justify (str, optional): One of "left", "right", "center", or "full". Defaults to ``None``. @@ -1366,7 +1366,7 @@ class Console: clear (bool, optional): Clear record buffer after exporting. Defaults to ``True``. code_format (str, optional): Format string to render HTML, should contain {foreground} {background} and {code}. - inline_styes (bool, optional): If ``True`` styles will be inlined in to spans, which makes files + inline_styles (bool, optional): If ``True`` styles will be inlined in to spans, which makes files larger but easier to cut and paste markup. If ``False``, styles will be embedded in a style tag. Defaults to False. diff --git a/rich/pretty.py b/rich/pretty.py index d9289006..2e334397 100644 --- a/rich/pretty.py +++ b/rich/pretty.py @@ -359,7 +359,7 @@ def pretty_repr( Args: _object (Any): Object to repr. - max_width (int, optional): Diresired maximum width of repr string. Defaults to 80. + max_width (int, optional): Desired maximum width of repr string. Defaults to 80. indent_size (int, optional): Number of spaces to indent. Defaults to 4. expand_all (bool, optional): Expand all containers regardless of available width. Defaults to False. diff --git a/rich/style.py b/rich/style.py index b51393b5..a67e2c67 100644 --- a/rich/style.py +++ b/rich/style.py @@ -345,7 +345,7 @@ class Style: return self._link @property - def transaprent_background(self) -> bool: + def transparent_background(self) -> bool: """Check if the style specified a transparent background.""" return self.bgcolor is None or self.bgcolor.is_default diff --git a/rich/syntax.py b/rich/syntax.py index 8055730c..670ddba7 100644 --- a/rich/syntax.py +++ b/rich/syntax.py @@ -353,7 +353,7 @@ class Syntax(JupyterMixin): base_style = self._get_base_style() justify: JustifyMethod = ( - "default" if base_style.transaprent_background else "left" + "default" if base_style.transparent_background else "left" ) text = Text( @@ -401,7 +401,7 @@ class Syntax(JupyterMixin): def _get_number_styles(self, console: Console) -> Tuple[Style, Style, Style]: """Get background, number, and highlight styles for line numbers.""" background_style = self._get_base_style() - if background_style.transaprent_background: + if background_style.transparent_background: return Style.null(), Style(dim=True), Style.null() if console.color_system in ("256", "truecolor"): number_style = Style.chain( @@ -428,7 +428,7 @@ class Syntax(JupyterMixin): def __rich_console__( self, console: Console, options: ConsoleOptions ) -> RenderResult: - transparent_background = self._get_base_style().transaprent_background + transparent_background = self._get_base_style().transparent_background code_width = ( (options.max_width - self._numbers_column_width - 1) if self.code_width is None diff --git a/rich/traceback.py b/rich/traceback.py index 37d4015c..71ce631b 100644 --- a/rich/traceback.py +++ b/rich/traceback.py @@ -9,8 +9,6 @@ from typing import Callable, Dict, List, Optional, Type from pygments.lexers import guess_lexer_for_filename from pygments.token import ( - Comment, - Generic, Keyword, Name, Number, @@ -212,7 +210,7 @@ class Traceback: traceback: Optional[TracebackType], show_locals: bool = False, ) -> Trace: - """Extrace traceback information. + """Extract traceback information. Args: exc_type (Type[BaseException]): Exception type. @@ -342,7 +340,7 @@ class Traceback: if syntax_error.filename != "": text = Text.assemble( (f" {syntax_error.filename}", "pygments.string"), - (":", "pgments.text"), + (":", "pygments.text"), (str(syntax_error.lineno), "pygments.number"), style="pygments.text", ) From b1e676d2fd6ac41d0b319d6a8f8472411970b15e Mon Sep 17 00:00:00 2001 From: Sergey Serebryakov Date: Thu, 8 Oct 2020 06:09:53 -0700 Subject: [PATCH 2/7] Amend CHANGELOG --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3a05e681..2cd0594b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Change the render prefix to correspond to the decimal units in progress +### Fixed + +- Fixed typo in `Style.transparent_background` method name. + ## [8.0.0] - 2020-10-03 ### Added From f484e4707f52271259e482ec1fb79f40f5a39985 Mon Sep 17 00:00:00 2001 From: Will McGugan Date: Thu, 8 Oct 2020 14:35:12 +0100 Subject: [PATCH 3/7] docs --- docs/source/tables.rst | 5 +++++ docs/source/text.rst | 4 +++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/docs/source/tables.rst b/docs/source/tables.rst index 957992a6..ec946e6e 100644 --- a/docs/source/tables.rst +++ b/docs/source/tables.rst @@ -69,6 +69,11 @@ This allows you to specify the text of the column only. If you want to set other title="Star Wars Movies" ) +Lines +~~~~~ + +By default, Tables will show a line under the header only. If you want to show lines between all rows add ``show_lines=True`` to the constructor. + Grids ~~~~~ diff --git a/docs/source/text.rst b/docs/source/text.rst index d5922d80..ed5f06cb 100644 --- a/docs/source/text.rst +++ b/docs/source/text.rst @@ -3,7 +3,9 @@ Rich Text ========= -Rich has a :class:`~rich.text.Text` class you can use to mark up strings with color and style attributes. You can consider this class to be like a mutable string which also contains style information. +Rich has a :class:`~rich.text.Text` class you can use to mark up strings with color and style attributes. You can use a Text instance anywhere a string is accepted, which gives you a lot of control over presentation. + +You can consider this class to be like a string with marked up regions of text. Unlike a builtin ``str``, a Text instance is mutable, and most methods operate in-place rather than returning a new instance. One way to add a style to Text is the :meth:`~rich.text.Text.stylize` method which applies a style to a start and end offset. Here is an example:: From 91b1016f838c2e551fdb041b6baec212907b317c Mon Sep 17 00:00:00 2001 From: David Besau Date: Thu, 8 Oct 2020 18:34:44 +0200 Subject: [PATCH 4/7] Fix typo in ipv4 regex --- rich/highlighter.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rich/highlighter.py b/rich/highlighter.py index 9fe85955..4a770aea 100644 --- a/rich/highlighter.py +++ b/rich/highlighter.py @@ -78,7 +78,7 @@ class ReprHighlighter(RegexHighlighter): r"(?P(?0x[0-9a-f]*)", r"(?P\B(\/[\w\.\-\_\+]+)*\/)(?P[\w\.\-\_\+]*)?", - r"(?P[0-9]{1,3}\.[0-9]{1,3}\.[0-gt9]{1,3}\.[0-9]{1,3})", + 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"(?b?\'\'\'.*?(?https?:\/\/[0-9a-zA-Z\$\-\_\+\!`\(\)\,\.\?\/\;\:\&\=\%\#]*)", From c2265eb103f3ade1230f057e61d2bc14568e1cc3 Mon Sep 17 00:00:00 2001 From: Edward Knight Date: Thu, 8 Oct 2020 20:30:15 +0100 Subject: [PATCH 5/7] Add highlight for EUI-48 and EUI-64 (MAC address) --- rich/default_styles.py | 2 ++ rich/highlighter.py | 6 ++++++ 2 files changed, 8 insertions(+) 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 9fe85955..02a606bd 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-gt9]{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})", From d47ba3167b60710efe07e40113150b53c88e7d85 Mon Sep 17 00:00:00 2001 From: Edward Knight Date: Thu, 8 Oct 2020 20:41:47 +0100 Subject: [PATCH 6/7] Add tests for EUI-48 and EUI-64 in ReprHighlighter --- tests/test_highlighter.py | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) 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) From efb3008b9f4c1f648daf65bbaf2b26101758908b Mon Sep 17 00:00:00 2001 From: Edward Knight Date: Thu, 8 Oct 2020 20:44:03 +0100 Subject: [PATCH 7/7] Updated changelog with EUI-48 EUI-64 highlighting --- CHANGELOG.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3a05e681..0913a790 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