From c92c1e745afc37c132d3e8aa704fefdb4c545691 Mon Sep 17 00:00:00 2001 From: Emmanuel Alap <15620712+ealap@users.noreply.github.com> Date: Tue, 13 Jun 2023 13:45:15 +0200 Subject: [PATCH] Add WIDTH and refactor MAX_WIDTH - Fix #109 by changing the behavior of MAX_WIDTH to still use the full width of the terminal if it is less than the limit - Add new flag WIDTH to take over the previous functionality of MAX_WIDTH - Adopt test expectations to new and modified configuration flags - Add new test for MAX_WIDTH Signed-off-by: Emmanuel Alap <15620712+ealap@users.noreply.github.com> # Please enter the commit message for your changes. Lines starting # with '#' will be ignored, and an empty message aborts the commit. # # On branch fix-max-width # Your branch is up to date with 'origin/fix-max-width'. # # Changes to be committed: # modified: README.md # modified: src/rich_click/rich_click.py # modified: src/rich_click/rich_help_configuration.py # modified: src/rich_click/rich_help_formatter.py # modified: tests/conftest.py # modified: tests/expectations/test_rich_click[test arguments with rich_config].config.json # modified: tests/expectations/test_rich_click[test arguments].config.json # modified: tests/expectations/test_rich_click[test custom errors help].config.json # modified: tests/expectations/test_rich_click[test custom errors with rich_config].config.json # modified: tests/expectations/test_rich_click[test declarative with rich_config].config.json # modified: tests/expectations/test_rich_click[test declarative].config.json # modified: tests/expectations/test_rich_click[test environment variables with rich_config].config.json # modified: tests/expectations/test_rich_click[test envvar].config.json # modified: tests/expectations/test_rich_click[test group sorting].config.json # modified: tests/expectations/test_rich_click[test groups sorting with rich_config].config.json # modified: tests/expectations/test_rich_click[test markdown with rich_config].config.json # modified: tests/expectations/test_rich_click[test markdown].config.json # modified: tests/expectations/test_rich_click[test metavars default with rich_config].config.json # modified: tests/expectations/test_rich_click[test metavars default].config.json # modified: tests/expectations/test_rich_click[test metavars with rich_config].config.json # modified: tests/expectations/test_rich_click[test metavars].config.json # modified: tests/expectations/test_rich_click[test rich markup with rich_config].config.json # modified: tests/expectations/test_rich_click[test rich markup].config.json # modified: tests/expectations/test_rich_click[test simple with rich_config].config.json # modified: tests/expectations/test_rich_click[test simple].config.json # modified: tests/expectations/test_rich_click[test table styles with rich_config].config.json # modified: tests/expectations/test_rich_click[test table styles].config.json # modified: tests/test_help.py # --- README.md | 15 +++++++-- src/rich_click/rich_click.py | 4 ++- src/rich_click/rich_help_configuration.py | 3 ++ src/rich_click/rich_help_formatter.py | 1 + tests/conftest.py | 3 +- ...st arguments with rich_config].config.json | 3 +- ...est_rich_click[test arguments].config.json | 3 +- ...click[test custom errors help].config.json | 3 +- ...ustom errors with rich_config].config.json | 3 +- ... declarative with rich_config].config.json | 3 +- ...t_rich_click[test declarative].config.json | 3 +- ...nt variables with rich_config].config.json | 3 +- .../test_rich_click[test envvar].config.json | 3 +- ...rich_click[test group sorting].config.json | 3 +- ...oups sorting with rich_config].config.json | 3 +- ...est markdown with rich_config].config.json | 3 +- ...test_rich_click[test markdown].config.json | 3 +- ...vars default with rich_config].config.json | 3 +- ...h_click[test metavars default].config.json | 3 +- ...est metavars with rich_config].config.json | 3 +- ...test_rich_click[test metavars].config.json | 3 +- ... rich markup with rich_config].config.json | 3 +- ...t_rich_click[test rich markup].config.json | 3 +- ...[test simple with rich_config].config.json | 3 +- .../test_rich_click[test simple].config.json | 3 +- ...table styles with rich_config].config.json | 3 +- ..._rich_click[test table styles].config.json | 3 +- tests/test_help.py | 31 +++++++++++++++++++ 28 files changed, 96 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index 6a6ef3a6..3c392539 100644 --- a/README.md +++ b/README.md @@ -224,12 +224,20 @@ click.rich_click.ERRORS_EPILOGUE = "To find out more, visit [link=https://mytool The default behaviour of rich-click is to use the full width of the terminal for output. However, if you've carefully crafted your help texts for the default narrow click output, you may find that you now have a lot of whitespace at the side of the panels. -To limit the maximum width of the help output, set `MAX_WIDTH` in characters, as follows: +To limit the maximum width of the help output, regardless of the terminal size, set `WIDTH` in characters as follows: ```python -click.rich_click.MAX_WIDTH = 100 +click.rich_click.WIDTH = 128 ``` +To still use the full width of the terminal up to a certain limit, set `MAX_WIDTH` in characters as follows: + +```python +click.rich_click.MAX_WIDTH = 96 +``` + +Setting `MAX_WIDTH` overrides the effect of `WIDTH` + ### Styling Most aspects of rich-click formatting can be customised, from colours to alignment. @@ -406,7 +414,8 @@ STYLE_ERRORS_PANEL_BORDER = "red" ALIGN_ERRORS_PANEL = "left" STYLE_ERRORS_SUGGESTION = "dim" STYLE_ABORTED = "red" -MAX_WIDTH = None # Set to an int to limit to that many characters +WIDTH = None # Set to int for a fixed character limit regardless of the terminal width +MAX_WIDTH = None # Set to int for a max character limit that is less than the terminal width. Overrides WIDTH limit COLOR_SYSTEM = "auto" # Set to None to disable colors # Fixed strings diff --git a/src/rich_click/rich_click.py b/src/rich_click/rich_click.py index 45706d02..0b2db8ce 100644 --- a/src/rich_click/rich_click.py +++ b/src/rich_click/rich_click.py @@ -71,7 +71,8 @@ ALIGN_ERRORS_PANEL = "left" STYLE_ERRORS_SUGGESTION = "dim" STYLE_ABORTED = "red" -MAX_WIDTH = int(getenv("TERMINAL_WIDTH")) if getenv("TERMINAL_WIDTH") else None # type: ignore +WIDTH = int(getenv("TERMINAL_WIDTH")) if getenv("TERMINAL_WIDTH") else None # type: ignore +MAX_WIDTH = int(getenv("TERMINAL_WIDTH")) if getenv("TERMINAL_WIDTH") else WIDTH # type: ignore COLOR_SYSTEM: Optional[ Literal["auto", "standard", "256", "truecolor", "windows"] ] = "auto" # Set to None to disable colors @@ -773,6 +774,7 @@ def get_module_help_configuration() -> RichHelpConfiguration: ALIGN_ERRORS_PANEL, STYLE_ERRORS_SUGGESTION, STYLE_ABORTED, + WIDTH, MAX_WIDTH, COLOR_SYSTEM, FORCE_TERMINAL, diff --git a/src/rich_click/rich_help_configuration.py b/src/rich_click/rich_help_configuration.py index 466a34b0..cc9277d1 100644 --- a/src/rich_click/rich_help_configuration.py +++ b/src/rich_click/rich_help_configuration.py @@ -69,6 +69,9 @@ class RichHelpConfiguration: align_errors_panel: rich.align.AlignMethod = field(default="left") style_errors_suggestion: rich.style.StyleType = field(default="dim") style_aborted: rich.style.StyleType = field(default="red") + width: Optional[int] = field( + default_factory=lambda: (int(getenv("TERMINAL_WIDTH")) if getenv("TERMINAL_WIDTH") else None) # type: ignore + ) max_width: Optional[int] = field( default_factory=lambda: (int(getenv("TERMINAL_WIDTH")) if getenv("TERMINAL_WIDTH") else None) # type: ignore ) diff --git a/src/rich_click/rich_help_formatter.py b/src/rich_click/rich_help_formatter.py index c96bd4b6..264ba853 100644 --- a/src/rich_click/rich_help_formatter.py +++ b/src/rich_click/rich_help_formatter.py @@ -45,6 +45,7 @@ def create_console(config: RichHelpConfiguration, file: Optional[IO[str]] = None color_system=config.color_system, force_terminal=config.force_terminal, file=file, + width=config.width, legacy_windows=config.legacy_windows, ) if isinstance(config.max_width, int): diff --git a/tests/conftest.py b/tests/conftest.py index 6dcdfa15..1f99dd06 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -129,7 +129,7 @@ def initialize_rich_click(): # each test reload(rc) # default config settings from https://github.com/Textualize/rich/blob/master/tests/render.py - rc.MAX_WIDTH = 100 + rc.WIDTH = 100 rc.COLOR_SYSTEM = "truecolor" rc.FORCE_TERMINAL = True @@ -258,6 +258,7 @@ def assertion( help_config: Optional[RichHelpConfiguration] = command.context_settings.get("rich_help_config") if help_config: help_config.color_system = rc.COLOR_SYSTEM + help_config.width = rc.WIDTH help_config.max_width = rc.MAX_WIDTH help_config.force_terminal = rc.FORCE_TERMINAL result = invoke(command, args) diff --git a/tests/expectations/test_rich_click[test arguments with rich_config].config.json b/tests/expectations/test_rich_click[test arguments with rich_config].config.json index e4718921..8138cb14 100644 --- a/tests/expectations/test_rich_click[test arguments with rich_config].config.json +++ b/tests/expectations/test_rich_click[test arguments with rich_config].config.json @@ -40,7 +40,8 @@ "align_errors_panel": "left", "style_errors_suggestion": "dim", "style_aborted": "red", - "max_width": 100, + "width": 100, + "max_width": null, "color_system": "truecolor", "force_terminal": true, "header_text": null, diff --git a/tests/expectations/test_rich_click[test arguments].config.json b/tests/expectations/test_rich_click[test arguments].config.json index e4718921..8138cb14 100644 --- a/tests/expectations/test_rich_click[test arguments].config.json +++ b/tests/expectations/test_rich_click[test arguments].config.json @@ -40,7 +40,8 @@ "align_errors_panel": "left", "style_errors_suggestion": "dim", "style_aborted": "red", - "max_width": 100, + "width": 100, + "max_width": null, "color_system": "truecolor", "force_terminal": true, "header_text": null, diff --git a/tests/expectations/test_rich_click[test custom errors help].config.json b/tests/expectations/test_rich_click[test custom errors help].config.json index 2e0a1998..0ebedb42 100644 --- a/tests/expectations/test_rich_click[test custom errors help].config.json +++ b/tests/expectations/test_rich_click[test custom errors help].config.json @@ -40,7 +40,8 @@ "align_errors_panel": "left", "style_errors_suggestion": "magenta italic", "style_aborted": "red", - "max_width": 100, + "width": 100, + "max_width": null, "color_system": "truecolor", "force_terminal": true, "header_text": null, diff --git a/tests/expectations/test_rich_click[test custom errors with rich_config].config.json b/tests/expectations/test_rich_click[test custom errors with rich_config].config.json index 2e0a1998..0ebedb42 100644 --- a/tests/expectations/test_rich_click[test custom errors with rich_config].config.json +++ b/tests/expectations/test_rich_click[test custom errors with rich_config].config.json @@ -40,7 +40,8 @@ "align_errors_panel": "left", "style_errors_suggestion": "magenta italic", "style_aborted": "red", - "max_width": 100, + "width": 100, + "max_width": null, "color_system": "truecolor", "force_terminal": true, "header_text": null, diff --git a/tests/expectations/test_rich_click[test declarative with rich_config].config.json b/tests/expectations/test_rich_click[test declarative with rich_config].config.json index 817ae65e..d7dd62ba 100644 --- a/tests/expectations/test_rich_click[test declarative with rich_config].config.json +++ b/tests/expectations/test_rich_click[test declarative with rich_config].config.json @@ -40,7 +40,8 @@ "align_errors_panel": "left", "style_errors_suggestion": "dim", "style_aborted": "red", - "max_width": 100, + "width": 100, + "max_width": null, "color_system": "truecolor", "force_terminal": true, "header_text": null, diff --git a/tests/expectations/test_rich_click[test declarative].config.json b/tests/expectations/test_rich_click[test declarative].config.json index 817ae65e..d7dd62ba 100644 --- a/tests/expectations/test_rich_click[test declarative].config.json +++ b/tests/expectations/test_rich_click[test declarative].config.json @@ -40,7 +40,8 @@ "align_errors_panel": "left", "style_errors_suggestion": "dim", "style_aborted": "red", - "max_width": 100, + "width": 100, + "max_width": null, "color_system": "truecolor", "force_terminal": true, "header_text": null, diff --git a/tests/expectations/test_rich_click[test environment variables with rich_config].config.json b/tests/expectations/test_rich_click[test environment variables with rich_config].config.json index 817ae65e..d7dd62ba 100644 --- a/tests/expectations/test_rich_click[test environment variables with rich_config].config.json +++ b/tests/expectations/test_rich_click[test environment variables with rich_config].config.json @@ -40,7 +40,8 @@ "align_errors_panel": "left", "style_errors_suggestion": "dim", "style_aborted": "red", - "max_width": 100, + "width": 100, + "max_width": null, "color_system": "truecolor", "force_terminal": true, "header_text": null, diff --git a/tests/expectations/test_rich_click[test envvar].config.json b/tests/expectations/test_rich_click[test envvar].config.json index 817ae65e..d7dd62ba 100644 --- a/tests/expectations/test_rich_click[test envvar].config.json +++ b/tests/expectations/test_rich_click[test envvar].config.json @@ -40,7 +40,8 @@ "align_errors_panel": "left", "style_errors_suggestion": "dim", "style_aborted": "red", - "max_width": 100, + "width": 100, + "max_width": null, "color_system": "truecolor", "force_terminal": true, "header_text": null, diff --git a/tests/expectations/test_rich_click[test group sorting].config.json b/tests/expectations/test_rich_click[test group sorting].config.json index ec87d0eb..fee23b8f 100644 --- a/tests/expectations/test_rich_click[test group sorting].config.json +++ b/tests/expectations/test_rich_click[test group sorting].config.json @@ -40,7 +40,8 @@ "align_errors_panel": "left", "style_errors_suggestion": "dim", "style_aborted": "red", - "max_width": 100, + "width": 100, + "max_width": null, "color_system": "truecolor", "force_terminal": true, "header_text": null, diff --git a/tests/expectations/test_rich_click[test groups sorting with rich_config].config.json b/tests/expectations/test_rich_click[test groups sorting with rich_config].config.json index ec87d0eb..fee23b8f 100644 --- a/tests/expectations/test_rich_click[test groups sorting with rich_config].config.json +++ b/tests/expectations/test_rich_click[test groups sorting with rich_config].config.json @@ -40,7 +40,8 @@ "align_errors_panel": "left", "style_errors_suggestion": "dim", "style_aborted": "red", - "max_width": 100, + "width": 100, + "max_width": null, "color_system": "truecolor", "force_terminal": true, "header_text": null, diff --git a/tests/expectations/test_rich_click[test markdown with rich_config].config.json b/tests/expectations/test_rich_click[test markdown with rich_config].config.json index e4e76c6c..1c35b748 100644 --- a/tests/expectations/test_rich_click[test markdown with rich_config].config.json +++ b/tests/expectations/test_rich_click[test markdown with rich_config].config.json @@ -40,7 +40,8 @@ "align_errors_panel": "left", "style_errors_suggestion": "dim", "style_aborted": "red", - "max_width": 100, + "width": 100, + "max_width": null, "color_system": "truecolor", "force_terminal": true, "header_text": null, diff --git a/tests/expectations/test_rich_click[test markdown].config.json b/tests/expectations/test_rich_click[test markdown].config.json index e4e76c6c..1c35b748 100644 --- a/tests/expectations/test_rich_click[test markdown].config.json +++ b/tests/expectations/test_rich_click[test markdown].config.json @@ -40,7 +40,8 @@ "align_errors_panel": "left", "style_errors_suggestion": "dim", "style_aborted": "red", - "max_width": 100, + "width": 100, + "max_width": null, "color_system": "truecolor", "force_terminal": true, "header_text": null, diff --git a/tests/expectations/test_rich_click[test metavars default with rich_config].config.json b/tests/expectations/test_rich_click[test metavars default with rich_config].config.json index 817ae65e..d7dd62ba 100644 --- a/tests/expectations/test_rich_click[test metavars default with rich_config].config.json +++ b/tests/expectations/test_rich_click[test metavars default with rich_config].config.json @@ -40,7 +40,8 @@ "align_errors_panel": "left", "style_errors_suggestion": "dim", "style_aborted": "red", - "max_width": 100, + "width": 100, + "max_width": null, "color_system": "truecolor", "force_terminal": true, "header_text": null, diff --git a/tests/expectations/test_rich_click[test metavars default].config.json b/tests/expectations/test_rich_click[test metavars default].config.json index 817ae65e..d7dd62ba 100644 --- a/tests/expectations/test_rich_click[test metavars default].config.json +++ b/tests/expectations/test_rich_click[test metavars default].config.json @@ -40,7 +40,8 @@ "align_errors_panel": "left", "style_errors_suggestion": "dim", "style_aborted": "red", - "max_width": 100, + "width": 100, + "max_width": null, "color_system": "truecolor", "force_terminal": true, "header_text": null, diff --git a/tests/expectations/test_rich_click[test metavars with rich_config].config.json b/tests/expectations/test_rich_click[test metavars with rich_config].config.json index a0cf69ed..29a08d8d 100644 --- a/tests/expectations/test_rich_click[test metavars with rich_config].config.json +++ b/tests/expectations/test_rich_click[test metavars with rich_config].config.json @@ -40,7 +40,8 @@ "align_errors_panel": "left", "style_errors_suggestion": "dim", "style_aborted": "red", - "max_width": 100, + "width": 100, + "max_width": null, "color_system": "truecolor", "force_terminal": true, "header_text": null, diff --git a/tests/expectations/test_rich_click[test metavars].config.json b/tests/expectations/test_rich_click[test metavars].config.json index a0cf69ed..29a08d8d 100644 --- a/tests/expectations/test_rich_click[test metavars].config.json +++ b/tests/expectations/test_rich_click[test metavars].config.json @@ -40,7 +40,8 @@ "align_errors_panel": "left", "style_errors_suggestion": "dim", "style_aborted": "red", - "max_width": 100, + "width": 100, + "max_width": null, "color_system": "truecolor", "force_terminal": true, "header_text": null, diff --git a/tests/expectations/test_rich_click[test rich markup with rich_config].config.json b/tests/expectations/test_rich_click[test rich markup with rich_config].config.json index bd6255da..1e323696 100644 --- a/tests/expectations/test_rich_click[test rich markup with rich_config].config.json +++ b/tests/expectations/test_rich_click[test rich markup with rich_config].config.json @@ -40,7 +40,8 @@ "align_errors_panel": "left", "style_errors_suggestion": "dim", "style_aborted": "red", - "max_width": 100, + "width": 100, + "max_width": null, "color_system": "truecolor", "force_terminal": true, "header_text": null, diff --git a/tests/expectations/test_rich_click[test rich markup].config.json b/tests/expectations/test_rich_click[test rich markup].config.json index bd6255da..1e323696 100644 --- a/tests/expectations/test_rich_click[test rich markup].config.json +++ b/tests/expectations/test_rich_click[test rich markup].config.json @@ -40,7 +40,8 @@ "align_errors_panel": "left", "style_errors_suggestion": "dim", "style_aborted": "red", - "max_width": 100, + "width": 100, + "max_width": null, "color_system": "truecolor", "force_terminal": true, "header_text": null, diff --git a/tests/expectations/test_rich_click[test simple with rich_config].config.json b/tests/expectations/test_rich_click[test simple with rich_config].config.json index 817ae65e..d7dd62ba 100644 --- a/tests/expectations/test_rich_click[test simple with rich_config].config.json +++ b/tests/expectations/test_rich_click[test simple with rich_config].config.json @@ -40,7 +40,8 @@ "align_errors_panel": "left", "style_errors_suggestion": "dim", "style_aborted": "red", - "max_width": 100, + "width": 100, + "max_width": null, "color_system": "truecolor", "force_terminal": true, "header_text": null, diff --git a/tests/expectations/test_rich_click[test simple].config.json b/tests/expectations/test_rich_click[test simple].config.json index 817ae65e..d7dd62ba 100644 --- a/tests/expectations/test_rich_click[test simple].config.json +++ b/tests/expectations/test_rich_click[test simple].config.json @@ -40,7 +40,8 @@ "align_errors_panel": "left", "style_errors_suggestion": "dim", "style_aborted": "red", - "max_width": 100, + "width": 100, + "max_width": null, "color_system": "truecolor", "force_terminal": true, "header_text": null, diff --git a/tests/expectations/test_rich_click[test table styles with rich_config].config.json b/tests/expectations/test_rich_click[test table styles with rich_config].config.json index d292272d..db98bfdc 100644 --- a/tests/expectations/test_rich_click[test table styles with rich_config].config.json +++ b/tests/expectations/test_rich_click[test table styles with rich_config].config.json @@ -40,7 +40,8 @@ "align_errors_panel": "left", "style_errors_suggestion": "dim", "style_aborted": "red", - "max_width": 100, + "width": 100, + "max_width": null, "color_system": "truecolor", "force_terminal": true, "header_text": null, diff --git a/tests/expectations/test_rich_click[test table styles].config.json b/tests/expectations/test_rich_click[test table styles].config.json index d292272d..db98bfdc 100644 --- a/tests/expectations/test_rich_click[test table styles].config.json +++ b/tests/expectations/test_rich_click[test table styles].config.json @@ -40,7 +40,8 @@ "align_errors_panel": "left", "style_errors_suggestion": "dim", "style_aborted": "red", - "max_width": 100, + "width": 100, + "max_width": null, "color_system": "truecolor", "force_terminal": true, "header_text": null, diff --git a/tests/test_help.py b/tests/test_help.py index 65903303..bd1b392b 100644 --- a/tests/test_help.py +++ b/tests/test_help.py @@ -1,3 +1,4 @@ +from importlib import reload from typing import Optional, Type import click @@ -207,6 +208,36 @@ def cli(): ) +def test_rich_config_max_width(invoke: InvokeCli, assert_str: AssertStr): + reload(rc) + rc.WIDTH = 100 + rc.MAX_WIDTH = 64 + + @command() + def cli(): + """Some help + + # Header + """ + pass + + result = invoke(cli, "--help") + + assert_str( + result.stdout, + """ +Usage: cli [OPTIONS] + + Some help + # Header + +╭─ Options ────────────────────────────────────────────────────╮ +│ --help Show this message and exit. │ +╰──────────────────────────────────────────────────────────────╯ + """, + ) + + def test_rich_config_context_settings(invoke: InvokeCli): @click.command( cls=RichCommand, context_settings={"rich_console": Console(), "rich_help_config": RichHelpConfiguration()}