Skip to content

Commit

Permalink
Ensure commands column is the same on all panels
Browse files Browse the repository at this point in the history
Previously the width of table columns for commands in each panel used
auto-width, making hard to read the commands.
  • Loading branch information
ssbarnea committed Mar 13, 2023
1 parent 50caff2 commit f70ff2c
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion typer/rich_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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:
Expand Down Expand Up @@ -633,13 +644,20 @@ 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))

# 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 @@ -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
Expand Down

0 comments on commit f70ff2c

Please sign in to comment.