Skip to content

Commit cfae1d2

Browse files
ssbarneasvlandegtiangolo
authored
✨ Improve column help display, ensure commands column width is the same on all panels (#567)
Co-authored-by: svlandeg <[email protected]> Co-authored-by: Sebastián Ramírez <[email protected]>
1 parent 475359f commit cfae1d2

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

typer/rich_utils.py

+22-1
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,7 @@ def _print_commands_panel(
466466
commands: List[click.Command],
467467
markup_mode: MarkupMode,
468468
console: Console,
469+
cmd_len: int,
469470
) -> None:
470471
t_styles: Dict[str, Any] = {
471472
"show_lines": STYLE_COMMANDS_TABLE_SHOW_LINES,
@@ -487,7 +488,16 @@ def _print_commands_panel(
487488
)
488489
# Define formatting in first column, as commands don't match highlighter
489490
# regex
490-
commands_table.add_column(style="bold cyan", no_wrap=True)
491+
commands_table.add_column(
492+
style="bold cyan",
493+
no_wrap=True,
494+
width=cmd_len,
495+
)
496+
497+
# A big ratio makes the description column be greedy and take all the space
498+
# available instead of allowing the command column to grow and misalign with
499+
# other panels.
500+
commands_table.add_column("Description", justify="left", no_wrap=False, ratio=10)
491501
rows: List[List[Union[RenderableType, None]]] = []
492502
deprecated_rows: List[Union[RenderableType, None]] = []
493503
for command in commands:
@@ -628,13 +638,23 @@ def rich_format_help(
628638
)
629639
panel_to_commands[panel_name].append(command)
630640

641+
# Identify the longest command name in all panels
642+
max_cmd_len = max(
643+
[
644+
len(command.name or "")
645+
for commands in panel_to_commands.values()
646+
for command in commands
647+
]
648+
)
649+
631650
# Print each command group panel
632651
default_commands = panel_to_commands.get(COMMANDS_PANEL_TITLE, [])
633652
_print_commands_panel(
634653
name=COMMANDS_PANEL_TITLE,
635654
commands=default_commands,
636655
markup_mode=markup_mode,
637656
console=console,
657+
cmd_len=max_cmd_len,
638658
)
639659
for panel_name, commands in panel_to_commands.items():
640660
if panel_name == COMMANDS_PANEL_TITLE:
@@ -645,6 +665,7 @@ def rich_format_help(
645665
commands=commands,
646666
markup_mode=markup_mode,
647667
console=console,
668+
cmd_len=max_cmd_len,
648669
)
649670

650671
# Epilogue if we have it

0 commit comments

Comments
 (0)