diff --git a/tests/test_rich_markup_mode.py b/tests/test_rich_markup_mode.py index 0afbc52b35..7470183f23 100644 --- a/tests/test_rich_markup_mode.py +++ b/tests/test_rich_markup_mode.py @@ -289,3 +289,50 @@ def test_markup_mode_default(): # We're assuming the test suite is run with rich installed app = typer.Typer() assert app.rich_markup_mode == "rich" + + +@pytest.mark.parametrize( + "mode", + ["markdown", "rich", None], +) +def test_markup_mode_epilog(mode): + app = typer.Typer(rich_markup_mode=mode) + + help_string = "Here is a help string" + + epilog_string = """ + Just wrapping up: + + This is the first conclusion + Here is conclusion two + + That's all folks! + """ + + @app.command(help=help_string, epilog=epilog_string) + def main(): + print("Hello World") + + assert app.rich_markup_mode == mode + + result = runner.invoke(app) + assert "Hello World" in result.stdout + + result = runner.invoke(app, ["--help"]) + result_lines = [line.strip() for line in result.stdout.split("\n")] + first_index = result_lines.index("Just wrapping up:") + assert first_index > 0 + assert result_lines[first_index + 1] == "" + + if mode == "rich": + assert result_lines[first_index + 2] == "This is the first conclusion" + assert result_lines[first_index + 3] == "Here is conclusion two" + assert result_lines[first_index + 4] == "" + assert result_lines[first_index + 5] == "That's all folks!" + else: + assert ( + result_lines[first_index + 2] + == "This is the first conclusion Here is conclusion two" + ) + assert result_lines[first_index + 3] == "" + assert result_lines[first_index + 4] == "That's all folks!" diff --git a/typer/rich_utils.py b/typer/rich_utils.py index ad110cb8d6..291fc470dc 100644 --- a/typer/rich_utils.py +++ b/typer/rich_utils.py @@ -201,8 +201,7 @@ def _get_help_text( if remaining_paragraphs: # Add a newline inbetween the header and the remaining paragraphs yield Text("") - # Join with double linebreaks for markdown and Rich markup - remaining_lines = "\n\n".join(remaining_paragraphs) + remaining_lines = _fix_linebreaks(remaining_paragraphs, markup_mode) yield _make_rich_text( text=remaining_lines, @@ -211,6 +210,11 @@ def _get_help_text( ) +def _fix_linebreaks(paragraphs: list[str], markup_mode: MarkupModeStrict) -> str: + # Join with double linebreaks for markdown and Rich markup + return "\n\n".join(paragraphs) + + def _get_parameter_help( *, param: Union[click.Option, click.Argument, click.Parameter], @@ -668,7 +672,7 @@ def rich_format_help( if obj.epilog: # Remove single linebreaks, replace double with single lines = obj.epilog.split("\n\n") - epilogue = "\n".join([x.replace("\n", " ").strip() for x in lines]) + epilogue = _fix_linebreaks(lines, markup_mode) epilogue_text = _make_rich_text(text=epilogue, markup_mode=markup_mode) console.print(Padding(Align(epilogue_text, pad=False), 1))