diff --git a/tests/test_rich_markup_mode.py b/tests/test_rich_markup_mode.py index df5d0323f0..5002f999f7 100644 --- a/tests/test_rich_markup_mode.py +++ b/tests/test_rich_markup_mode.py @@ -284,3 +284,50 @@ def main(arg: str): arg_start = [i for i, row in enumerate(result_lines) if "Arguments" in row][0] assert help_start != -1 assert result_lines[help_start:arg_start] == lines + + +@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 d4c3676aea..e0f8c4efc5 100644 --- a/typer/rich_utils.py +++ b/typer/rich_utils.py @@ -208,19 +208,7 @@ def _get_help_text( if remaining_paragraphs: # Add a newline inbetween the header and the remaining paragraphs yield Text("") - if markup_mode not in (MARKUP_MODE_RICH, MARKUP_MODE_MARKDOWN): - # Remove single linebreaks - remaining_paragraphs = [ - x.replace("\n", " ").strip() - if not x.startswith("\b") - else "{}\n".format(x.strip("\b\n")) - for x in remaining_paragraphs - ] - # Join back together - remaining_lines = "\n".join(remaining_paragraphs) - else: - # Join with double linebreaks if markdown or Rich markup - remaining_lines = "\n\n".join(remaining_paragraphs) + remaining_lines = _fix_linebreaks(remaining_paragraphs, markup_mode) yield _make_rich_text( text=remaining_lines, @@ -229,6 +217,22 @@ def _get_help_text( ) +def _fix_linebreaks(paragraphs: List[str], markup_mode: MarkupMode) -> str: + if markup_mode not in (MARKUP_MODE_RICH, MARKUP_MODE_MARKDOWN): + # Remove single linebreaks + paragraphs = [ + x.replace("\n", " ").strip() + if not x.startswith("\b") + else "{}\n".format(x.strip("\b\n")) + for x in paragraphs + ] + # Join back together + return "\n".join(paragraphs) + else: + # Join with double linebreaks if markdown or Rich markup + return "\n\n".join(paragraphs) + + def _get_parameter_help( *, param: Union[click.Option, click.Argument, click.Parameter], @@ -686,7 +690,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))