Skip to content

Commit

Permalink
✨ Improve column help display, ensure commands column width is the sa…
Browse files Browse the repository at this point in the history
…me on all panels (#567)

Co-authored-by: svlandeg <sofie.vanlandeghem@gmail.com>
Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com>
  • Loading branch information
3 people committed Apr 8, 2024
1 parent 475359f commit cfae1d2
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion typer/rich_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,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,
Expand All @@ -487,7 +488,16 @@ 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)
rows: List[List[Union[RenderableType, None]]] = []
deprecated_rows: List[Union[RenderableType, None]] = []
for command in commands:
Expand Down Expand Up @@ -628,13 +638,23 @@ def rich_format_help(
)
panel_to_commands[panel_name].append(command)

# Identify the longest command name in all panels
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, [])
_print_commands_panel(
name=COMMANDS_PANEL_TITLE,
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:
Expand All @@ -645,6 +665,7 @@ def rich_format_help(
commands=commands,
markup_mode=markup_mode,
console=console,
cmd_len=max_cmd_len,
)

# Epilogue if we have it
Expand Down

0 comments on commit cfae1d2

Please sign in to comment.