Skip to content

Commit

Permalink
Add WIDTH and refactor MAX_WIDTH
Browse files Browse the repository at this point in the history
- 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
#
  • Loading branch information
ealap committed Jun 13, 2023
1 parent 491f643 commit c92c1e7
Show file tree
Hide file tree
Showing 28 changed files with 96 additions and 27 deletions.
15 changes: 12 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion src/rich_click/rich_click.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 3 additions & 0 deletions src/rich_click/rich_help_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
1 change: 1 addition & 0 deletions src/rich_click/rich_help_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
3 changes: 2 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion tests/expectations/test_rich_click[test envvar].config.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion tests/expectations/test_rich_click[test simple].config.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
31 changes: 31 additions & 0 deletions tests/test_help.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from importlib import reload
from typing import Optional, Type

import click
Expand Down Expand Up @@ -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()}
Expand Down

0 comments on commit c92c1e7

Please sign in to comment.