From 0e5fa16dece7ca15bfad5be7aeb0bf364dd4aa9b Mon Sep 17 00:00:00 2001 From: Sorin Sbarnea Date: Mon, 13 Mar 2023 14:52:35 +0000 Subject: [PATCH 1/4] Ensure commands column is the same on all panels Previously the width of table columns for commands in each panel used auto-width, making hard to read the commands. --- typer/rich_utils.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/typer/rich_utils.py b/typer/rich_utils.py index 3f602ee651..9a6d08abef 100644 --- a/typer/rich_utils.py +++ b/typer/rich_utils.py @@ -469,6 +469,7 @@ def _print_commands_panel( commands: List[click.Command], markup_mode: MarkupMode, console: Console, + cmd_len: int, ) -> None: t_styles: Dict[str, Any] = { "show_lines": STYLE_COMMANDS_TABLE_SHOW_LINES, @@ -490,7 +491,17 @@ def _print_commands_panel( ) # Define formatting in first column, as commands don't match highlighter # regex - commands_table.add_column(style="bold cyan", no_wrap=True) + commands_table.add_column( + style="bold cyan", + no_wrap=True, + width=cmd_len, + ) + + # A big ratio makes the description column be greedy and take all the space + # available instead of allowing the command column to grow and misalign with + # other panels. + commands_table.add_column("Description", justify="left", no_wrap=False, ratio=10) + commands_table.add_column("xx", style="bold yellow", justify="left", no_wrap=False) rows: List[List[Union[RenderableType, None]]] = [] deprecated_rows: List[Union[RenderableType, None]] = [] for command in commands: @@ -633,6 +644,12 @@ def rich_format_help( ) panel_to_commands[panel_name].append(command) + # Identify the longest command name in all panels + max_cmd_len = 0 + for panel_name, commands in panel_to_commands.items(): + for command in commands: + max_cmd_len = max(max_cmd_len, len(command.name or "")) + # Print each command group panel default_commands = panel_to_commands.get(COMMANDS_PANEL_TITLE, []) _print_commands_panel( @@ -640,6 +657,7 @@ def rich_format_help( commands=default_commands, markup_mode=markup_mode, console=console, + cmd_len=max_cmd_len, ) for panel_name, commands in panel_to_commands.items(): if panel_name == COMMANDS_PANEL_TITLE: @@ -650,6 +668,7 @@ def rich_format_help( commands=commands, markup_mode=markup_mode, console=console, + cmd_len=max_cmd_len, ) # Epilogue if we have it From fe27cf7342fb7b20e76dc0783e011e666f8edba0 Mon Sep 17 00:00:00 2001 From: svlandeg Date: Fri, 5 Apr 2024 14:38:47 +0200 Subject: [PATCH 2/4] fix ruff complaint --- typer/rich_utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/typer/rich_utils.py b/typer/rich_utils.py index e0f248e046..0310b638dc 100644 --- a/typer/rich_utils.py +++ b/typer/rich_utils.py @@ -641,7 +641,7 @@ def rich_format_help( # Identify the longest command name in all panels max_cmd_len = 0 - for panel_name, commands in panel_to_commands.items(): + for _, commands in panel_to_commands.items(): for command in commands: max_cmd_len = max(max_cmd_len, len(command.name or "")) From ef6a34b9398498b42af5684af50bad3255d8fb99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Ram=C3=ADrez?= Date: Mon, 8 Apr 2024 14:02:50 -0500 Subject: [PATCH 3/4] =?UTF-8?q?=F0=9F=94=A5=20Remove=20unnecessary=20colum?= =?UTF-8?q?n?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- typer/rich_utils.py | 1 - 1 file changed, 1 deletion(-) diff --git a/typer/rich_utils.py b/typer/rich_utils.py index 0310b638dc..1913c8e7a7 100644 --- a/typer/rich_utils.py +++ b/typer/rich_utils.py @@ -498,7 +498,6 @@ def _print_commands_panel( # available instead of allowing the command column to grow and misalign with # other panels. commands_table.add_column("Description", justify="left", no_wrap=False, ratio=10) - commands_table.add_column("xx", style="bold yellow", justify="left", no_wrap=False) rows: List[List[Union[RenderableType, None]]] = [] deprecated_rows: List[Union[RenderableType, None]] = [] for command in commands: From 069d9f61ef2757f931aa0b91aa8d6cc95929733b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Ram=C3=ADrez?= Date: Mon, 8 Apr 2024 14:03:39 -0500 Subject: [PATCH 4/4] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Refactor=20list=20in?= =?UTF-8?q?=20comprehension?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- typer/rich_utils.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/typer/rich_utils.py b/typer/rich_utils.py index 1913c8e7a7..2b91f265ab 100644 --- a/typer/rich_utils.py +++ b/typer/rich_utils.py @@ -639,10 +639,13 @@ def rich_format_help( panel_to_commands[panel_name].append(command) # Identify the longest command name in all panels - max_cmd_len = 0 - for _, commands in panel_to_commands.items(): - for command in commands: - max_cmd_len = max(max_cmd_len, len(command.name or "")) + max_cmd_len = max( + [ + len(command.name or "") + for commands in panel_to_commands.values() + for command in commands + ] + ) # Print each command group panel default_commands = panel_to_commands.get(COMMANDS_PANEL_TITLE, [])