Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Improve column help display, ensure commands column width is the same on all panels #567

Merged
merged 6 commits into from
Apr 8, 2024
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 20 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,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)
tiangolo marked this conversation as resolved.
Show resolved Hide resolved
rows: List[List[Union[RenderableType, None]]] = []
deprecated_rows: List[Union[RenderableType, None]] = []
for command in commands:
Expand Down Expand Up @@ -628,13 +639,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 _, commands in panel_to_commands.items():
for command in commands:
max_cmd_len = max(max_cmd_len, len(command.name or ""))
tiangolo marked this conversation as resolved.
Show resolved Hide resolved

# 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 +663,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