Skip to content

Fix single newlines not collapsed in commands panel in rich mode#1844

Open
JSap0914 wants to merge 2 commits into
fastapi:masterfrom
JSap0914:fix/commands-panel-single-newline-rich-mode
Open

Fix single newlines not collapsed in commands panel in rich mode#1844
JSap0914 wants to merge 2 commits into
fastapi:masterfrom
JSap0914:fix/commands-panel-single-newline-rich-mode

Conversation

@JSap0914

Copy link
Copy Markdown

Bug

In _make_command_help (used to render each command's help text in the Commands panel), the condition guarding newline collapsing was inverted:

# Before (buggy)
if markup_mode != MARKUP_MODE_RICH and not paragraphs[0].startswith("\b"):

This meant:

  • rich mode (the default): single newlines within a subcommand's first help paragraph were not collapsed → the Commands panel showed each line on its own table row.
  • markdown mode: single newlines were collapsed unnecessarily (Markdown renders them as spaces anyway).

Every other function in the file uses markup_mode != MARKUP_MODE_MARKDOWN for the same guard (_get_help_text, _get_parameter_help).

Reproducer

import typer
from typer.testing import CliRunner

app = typer.Typer(rich_markup_mode="rich")

@app.command()
def cmd1():
    """First line
    second line"""

@app.command()
def cmd2():
    """Normal help"""

result = CliRunner().invoke(app, ["--help"])
print(result.output)

Before fix – Commands panel:

╭─ Commands ─────────────────────╮
│ cmd1  First line               │
│       second line              │  ← broken: extra row
│ cmd2  Normal help              │
╰────────────────────────────────╯

After fix:

╭─ Commands ─────────────────────╮
│ cmd1  First line second line   │  ← correct: collapsed
│ cmd2  Normal help              │
╰────────────────────────────────╯

Fix

Change the one condition in _make_command_help:

# After (fix)
if markup_mode != MARKUP_MODE_MARKDOWN and not paragraphs[0].startswith("\b"):

Verification

pytest tests/test_rich_markup_mode.py::test_commands_panel_single_newline_collapsed -v

Result: 2 passed (rich mode and markdown mode both collapse correctly).

AI-assisted contribution.

In _make_command_help, the condition guarding newline collapsing was
inverted: it used `markup_mode != MARKUP_MODE_RICH` instead of
`markup_mode != MARKUP_MODE_MARKDOWN`, which is consistent with the
rest of the file (_get_help_text and _get_parameter_help both use the
MARKUP_MODE_MARKDOWN form).

Effect of the bug: when rich_markup_mode='rich' (the default), any
subcommand whose help text contained a single newline within its first
paragraph would appear as multiple rows in the Commands panel—one row
per line—instead of collapsing to a single space-joined line.

The fix changes the one condition so that:
- rich mode: single newlines are collapsed to spaces (correct behaviour)
- markdown mode: single newlines are left for the Markdown renderer

Adds a parametrized regression test covering both modes.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@svlandeg svlandeg added the bug Something isn't working label Jun 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants