Skip to content

Commit

Permalink
Merge pull request #110 from ealap/fix-max-width
Browse files Browse the repository at this point in the history
fix: Use the full width of the terminal for output if it is less than the MAX_WIDTH setting
  • Loading branch information
dwreeves authored Oct 2, 2023
2 parents c8797a7 + 13860e1 commit 277a5e4
Show file tree
Hide file tree
Showing 54 changed files with 153 additions and 55 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 @@ -411,7 +419,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 @@ -73,7 +73,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 @@ -798,6 +799,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 @@ -72,6 +72,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
7 changes: 5 additions & 2 deletions src/rich_click/rich_help_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def create_console(config: RichHelpConfiguration, file: Optional[IO[str]] = None
file: Optional IO stream to write Rich Console output
Defaults to None.
"""
return Console(
console = Console(
theme=rich.theme.Theme(
{
"option": config.style_option,
Expand All @@ -51,10 +51,13 @@ def create_console(config: RichHelpConfiguration, file: Optional[IO[str]] = None
highlighter=config.highlighter,
color_system=config.color_system,
force_terminal=config.force_terminal,
width=config.max_width,
file=file,
width=config.width,
legacy_windows=config.legacy_windows,
)
if isinstance(config.max_width, int):
console.width = min(config.max_width, console.size.width)
return console


def get_module_config() -> RichHelpConfiguration:
Expand Down
5 changes: 4 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,15 @@ 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"

# Click <8 tests fail unless we set the COLOR_SYSTEM to None.
if CLICK_IS_BEFORE_VERSION_8X:
rc.COLOR_SYSTEM = None
else:
rc.COLOR_SYSTEM = "truecolor"

rc.FORCE_TERMINAL = True


Expand Down Expand Up @@ -271,6 +273,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 @@ -41,7 +41,8 @@
"align_errors_panel": "left",
"style_errors_suggestion": "dim",
"style_aborted": "red",
"max_width": 100,
"width": 100,
"max_width": null,
"color_system": null,
"force_terminal": true,
"header_text": null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,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 @@ -41,7 +41,8 @@
"align_errors_panel": "left",
"style_errors_suggestion": "dim",
"style_aborted": "red",
"max_width": 100,
"width": 100,
"max_width": null,
"color_system": null,
"force_terminal": true,
"header_text": null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,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 @@ -41,7 +41,8 @@
"align_errors_panel": "left",
"style_errors_suggestion": "dim",
"style_aborted": "red",
"max_width": 100,
"width": 100,
"max_width": null,
"color_system": null,
"force_terminal": true,
"header_text": null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,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 @@ -41,7 +41,8 @@
"align_errors_panel": "left",
"style_errors_suggestion": "dim",
"style_aborted": "red",
"max_width": 100,
"width": 100,
"max_width": null,
"color_system": null,
"force_terminal": true,
"header_text": null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,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 @@ -41,7 +41,8 @@
"align_errors_panel": "left",
"style_errors_suggestion": "magenta italic",
"style_aborted": "red",
"max_width": 100,
"width": 100,
"max_width": null,
"color_system": null,
"force_terminal": true,
"header_text": null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,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 @@ -41,7 +41,8 @@
"align_errors_panel": "left",
"style_errors_suggestion": "magenta italic",
"style_aborted": "red",
"max_width": 100,
"width": 100,
"max_width": null,
"color_system": null,
"force_terminal": true,
"header_text": null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,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 @@ -41,7 +41,8 @@
"align_errors_panel": "left",
"style_errors_suggestion": "dim",
"style_aborted": "red",
"max_width": 100,
"width": 100,
"max_width": null,
"color_system": null,
"force_terminal": true,
"header_text": null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,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 @@ -41,7 +41,8 @@
"align_errors_panel": "left",
"style_errors_suggestion": "dim",
"style_aborted": "red",
"max_width": 100,
"width": 100,
"max_width": null,
"color_system": null,
"force_terminal": true,
"header_text": null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,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 @@ -41,7 +41,8 @@
"align_errors_panel": "left",
"style_errors_suggestion": "dim",
"style_aborted": "red",
"max_width": 100,
"width": 100,
"max_width": null,
"color_system": null,
"force_terminal": true,
"header_text": null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,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 @@ -41,7 +41,8 @@
"align_errors_panel": "left",
"style_errors_suggestion": "dim",
"style_aborted": "red",
"max_width": 100,
"width": 100,
"max_width": null,
"color_system": null,
"force_terminal": true,
"header_text": null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,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 @@ -41,7 +41,8 @@
"align_errors_panel": "left",
"style_errors_suggestion": "dim",
"style_aborted": "red",
"max_width": 100,
"width": 100,
"max_width": null,
"color_system": null,
"force_terminal": true,
"header_text": null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,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 @@ -41,7 +41,8 @@
"align_errors_panel": "left",
"style_errors_suggestion": "dim",
"style_aborted": "red",
"max_width": 100,
"width": 100,
"max_width": null,
"color_system": null,
"force_terminal": true,
"header_text": null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,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 @@ -41,7 +41,8 @@
"align_errors_panel": "left",
"style_errors_suggestion": "dim",
"style_aborted": "red",
"max_width": 100,
"width": 100,
"max_width": null,
"color_system": null,
"force_terminal": true,
"header_text": null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,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 @@ -41,7 +41,8 @@
"align_errors_panel": "left",
"style_errors_suggestion": "dim",
"style_aborted": "red",
"max_width": 100,
"width": 100,
"max_width": null,
"color_system": null,
"force_terminal": true,
"header_text": null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,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 @@ -41,7 +41,8 @@
"align_errors_panel": "left",
"style_errors_suggestion": "dim",
"style_aborted": "red",
"max_width": 100,
"width": 100,
"max_width": null,
"color_system": null,
"force_terminal": true,
"header_text": null,
Expand Down
Loading

0 comments on commit 277a5e4

Please sign in to comment.