🐛 Fix `--help` text alignment when using `typer.style()` in option descriptions (#1356)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Mahimai Raja J · committed by GitHub · parent a6ea8c0 · Browse files · 2 files·+98 added−1 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Trailers
- Co-authored-by: Sofie Van Landeghem <svlandeg@users.noreply.github.com>
- Co-authored-by: svlandeg <svlandeg@github.com>
Diff stats
2 files+98 added−1 removed
- tests:+89 added−0 removed
- other:+9 added−1 removed
Provenance
- Commit
f35ae0d- Tree
ec6a140- Author
Mahimai Raja J <mahimairaja3@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
a6ea8c0
2 files changed+98 added−1 removed
modified:tests/test_rich_utils.py+89 added
97 unmodified lines98 99 result = runner.invoke(app, ["--help"])100 assert "Usage" in result.stdout101 assert "BAR" in result.stdout102+103+104+def test_make_rich_text_with_ansi_escape_sequences():105+ from typer.rich_utils import Text, _make_rich_text106+107+ ansi_text = "This is \x1b[4munderlined\x1b[0m text"108+ result = _make_rich_text(text=ansi_text, markup_mode="rich")109+110+ assert isinstance(result, Text)111+ assert "\x1b[" not in result.plain112+ assert "underlined" in result.plain113+114+ mixed_text = "Start \x1b[31mred\x1b[0m middle \x1b[32mgreen\x1b[0m end"115+ result = _make_rich_text(text=mixed_text, markup_mode="rich")116+ assert isinstance(result, Text)117+ assert "\x1b[" not in result.plain118+ assert "red" in result.plain119+ assert "green" in result.plain120+121+ fake_ansi = "This contains \x1b[ but not a complete sequence"122+ result = _make_rich_text(text=fake_ansi, markup_mode="rich")123+ assert isinstance(result, Text)124+ assert "\x1b[" not in result.plain125+ assert "This contains " in result.plain126+127+128+def test_make_rich_text_with_typer_style_in_help():129+ app = typer.Typer()130+131+ @app.command()132+ def example(133+ a: str = typer.Option(help="This is A"),134+ b: str = typer.Option(help=f"This is {typer.style('B', underline=True)}"),135+ ):136+ """Example command with styled help text."""137+ pass # pragma: no cover138+139+ result = runner.invoke(app, ["--help"])140+141+ assert result.exit_code == 0142+ assert "This is A" in result.stdout143+ assert "This is B" in result.stdout144+ assert "\x1b[" not in result.stdout145+146+147+def test_help_table_alignment_with_styled_text():148+ app = typer.Typer()149+150+ @app.command()151+ def example(152+ a: str = typer.Option(help="This is A"),153+ b: str = typer.Option(help=f"This is {typer.style('B', underline=True)}"),154+ c: str = typer.Option(help="This is C"),155+ ):156+ """Example command with styled help text."""157+ pass # pragma: no cover158+159+ result = runner.invoke(app, ["--help"])160+161+ assert result.exit_code == 0162+163+ lines = result.stdout.split("\n")164+165+ option_a_line = None166+ option_b_line = None167+ option_c_line = None168+169+ for line in lines:170+ if "--a" in line and "This is A" in line:171+ option_a_line = line172+ elif "--b" in line and "This is B" in line:173+ option_b_line = line174+ elif "--c" in line and "This is C" in line:175+ option_c_line = line176+177+ assert option_a_line is not None, "Option A line not found"178+ assert option_b_line is not None, "Option B line not found"179+ assert option_c_line is not None, "Option C line not found"180+181+ def find_right_boundary_pos(line):182+ return line.rfind("|")183+184+ pos_a = find_right_boundary_pos(option_a_line)185+ pos_b = find_right_boundary_pos(option_b_line)186+ pos_c = find_right_boundary_pos(option_c_line)187+188+ assert pos_a == pos_b == pos_c, (189+ f"Right boundaries not aligned: A={pos_a}, B={pos_b}, C={pos_c}"190+ )modified:typer/rich_utils.py+9 added-1 removed
95 unmodified lines96 97 MARKUP_MODE_MARKDOWN = "markdown"98 MARKUP_MODE_RICH = "rich"99 _RICH_HELP_PANEL_NAME = "rich_help_panel"100+ANSI_PREFIX = "\033["101 102 MarkupModeStrict = Literal["markdown", "rich"]103 104 19 unmodified lines124 highlighter = OptionHighlighter()125 negative_highlighter = NegativeOptionHighlighter()126 127 128+def _has_ansi_character(text: str) -> bool:129+ return ANSI_PREFIX in text130+131+132 def _get_rich_console(stderr: bool = False) -> Console:133 return Console(134 theme=Theme(135 {28 unmodified lines164 text = Emoji.replace(text)165 return Markdown(text, style=style)166 else:167 assert markup_mode == MARKUP_MODE_RICH168+ if _has_ansi_character(text):169+ return highlighter(Text.from_ansi(text, style=style))170+ else:163- return highlighter(Text.from_markup(text, style=style))171+ return highlighter(Text.from_markup(text, style=style))172 173 174 @group()175 def _get_help_text(585 unmodified lines