Skip to content
Myr

🐛 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

2 files+98 added−1 removed

modified:tests/test_rich_utils.py+89 added
97 unmodified lines
9899 result = runner.invoke(app, ["--help"])100 assert "Usage" in result.stdout101 assert "BAR" in result.stdout102103104def test_make_rich_text_with_ansi_escape_sequences():105 from typer.rich_utils import Text, _make_rich_text106107 ansi_text = "This is \x1b[4munderlined\x1b[0m text"108 result = _make_rich_text(text=ansi_text, markup_mode="rich")109110 assert isinstance(result, Text)111 assert "\x1b[" not in result.plain112 assert "underlined" in result.plain113114 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.plain120121 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.plain126127128def test_make_rich_text_with_typer_style_in_help():129 app = typer.Typer()130131 @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 cover138139 result = runner.invoke(app, ["--help"])140141 assert result.exit_code == 0142 assert "This is A" in result.stdout143 assert "This is B" in result.stdout144 assert "\x1b[" not in result.stdout145146147def test_help_table_alignment_with_styled_text():148 app = typer.Typer()149150 @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 cover158159 result = runner.invoke(app, ["--help"])160161 assert result.exit_code == 0162163 lines = result.stdout.split("\n")164165 option_a_line = None166 option_b_line = None167 option_c_line = None168169 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 = line176177 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"180181 def find_right_boundary_pos(line):182 return line.rfind("|")183184 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)187188 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 lines
9697MARKUP_MODE_MARKDOWN = "markdown"98MARKUP_MODE_RICH = "rich"99_RICH_HELP_PANEL_NAME = "rich_help_panel"100ANSI_PREFIX = "\033["101102MarkupModeStrict = Literal["markdown", "rich"]103104
19 unmodified lines
124highlighter = OptionHighlighter()125negative_highlighter = NegativeOptionHighlighter()126127128def _has_ansi_character(text: str) -> bool:129 return ANSI_PREFIX in text130131132def _get_rich_console(stderr: bool = False) -> Console:133 return Console(134 theme=Theme(135 {
28 unmodified lines
164 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))172173174@group()175def _get_help_text(
585 unmodified lines