Skip to content
Myr

✨ Make `epilog` formatting consistent with other parts of the help string (#1405)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Sofie Van Landeghem · committed by GitHub · parent 32d80ef · Browse files · 2 files+54 added−3 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+54 added−3 removed

  • tests:+47 added−0 removed
  • other:+7 added−3 removed

2 files+54 added−3 removed

modified:tests/test_rich_markup_mode.py+47 added
328 unmodified lines
329def test_markup_mode_default():330 # We're assuming the test suite is run with rich installed331 app = typer.Typer()332 assert app.rich_markup_mode == "rich"333334335@pytest.mark.parametrize(336 "mode",337 ["markdown", "rich", None],338)339def test_markup_mode_epilog(mode):340 app = typer.Typer(rich_markup_mode=mode)341342 help_string = "Here is a help string"343344 epilog_string = """345 Just wrapping up:346347 This is the first conclusion348 Here is conclusion two349350 That's all folks!351 """352353 @app.command(help=help_string, epilog=epilog_string)354 def main():355 print("Hello World")356357 assert app.rich_markup_mode == mode358359 result = runner.invoke(app)360 assert "Hello World" in result.stdout361362 result = runner.invoke(app, ["--help"])363 result_lines = [line.strip() for line in result.stdout.split("\n")]364 first_index = result_lines.index("Just wrapping up:")365 assert first_index > 0366 assert result_lines[first_index + 1] == ""367368 if mode == "rich":369 assert result_lines[first_index + 2] == "This is the first conclusion"370 assert result_lines[first_index + 3] == "Here is conclusion two"371 assert result_lines[first_index + 4] == ""372 assert result_lines[first_index + 5] == "That's all folks!"373 else:374 assert (375 result_lines[first_index + 2]376 == "This is the first conclusion Here is conclusion two"377 )378 assert result_lines[first_index + 3] == ""379 assert result_lines[first_index + 4] == "That's all folks!"
modified:typer/rich_utils.py+7 added-3 removed
220 unmodified lines
221 # Get remaining lines, remove single line breaks and format as dim222 if remaining_paragraphs:223 # Add a newline inbetween the header and the remaining paragraphs224 yield Text("")225 # Join with double linebreaks for markdown and Rich markup226 remaining_lines = "\n\n".join(remaining_paragraphs)225 remaining_lines = _fix_linebreaks(remaining_paragraphs, markup_mode)226227 yield _make_rich_text(228 text=remaining_lines,229 style=STYLE_HELPTEXT,230 markup_mode=markup_mode,231 )232233234def _fix_linebreaks(paragraphs: list[str], markup_mode: MarkupModeStrict) -> str:235 # Join with double linebreaks for markdown and Rich markup236 return "\n\n".join(paragraphs)237238239def _get_parameter_help(240 *,241 param: TyperOption | TyperArgument | _click.Parameter,242 ctx: _click.Context,
445 unmodified lines
688 # Epilogue if we have it689 if obj.epilog:690 # Remove single linebreaks, replace double with single691 lines = obj.epilog.split("\n\n")688 epilogue = "\n".join([x.replace("\n", " ").strip() for x in lines])692 epilogue = _fix_linebreaks(lines, markup_mode)693 epilogue_text = _make_rich_text(text=epilogue, markup_mode=markup_mode)694 console.print(Padding(Align(epilogue_text, pad=False), 1))695696
80 unmodified lines