From 66cd2f8567d3c739db7dd5980b3babc90d819633 Mon Sep 17 00:00:00 2001 From: svlandeg Date: Fri, 30 Aug 2024 11:41:58 +0200 Subject: [PATCH] remove yielding of additional newline after first_line if it's markdown --- tests/test_rich_markup_mode.py | 14 ++++++++------ typer/rich_utils.py | 5 +++-- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/tests/test_rich_markup_mode.py b/tests/test_rich_markup_mode.py index 8a4cbb4007..825a35f039 100644 --- a/tests/test_rich_markup_mode.py +++ b/tests/test_rich_markup_mode.py @@ -46,9 +46,9 @@ def main(arg: str): @pytest.mark.parametrize( "mode,lines", [ - ("markdown", ["First line", "", "Line 1", "", "Line 2", ""]), - ("rich", ["First line", "", "Line 1", "", "Line 2", ""]), - ("none", ["First line", "", "Line 1", "Line 2", ""]), + ("markdown", ["First line", "Line 1", "", "Line 2", "", "Line 3", ""]), + ("rich", ["First line", "", "Line 1", "", "Line 2", "", "Line 3", ""]), + ("none", ["First line", "", "Line 1", "Line 2", "Line 3", ""]), ], ) def test_markup_mode_newline_pr815(mode: str, lines: List[str]): @@ -61,6 +61,8 @@ def main(arg: str): Line 1 Line 2 + + Line 3 """ print(f"Hello {arg}") @@ -81,7 +83,7 @@ def main(arg: str): @pytest.mark.parametrize( "mode,lines", [ - ("markdown", ["First line", "", "Line 1 Line 2 Line 3", ""]), + ("markdown", ["First line", "Line 1 Line 2 Line 3", ""]), ("rich", ["First line", "", "Line 1", "Line 2", "Line 3", ""]), ("none", ["First line", "", "Line 1 Line 2 Line 3", ""]), ], @@ -116,7 +118,7 @@ def main(arg: str): @pytest.mark.parametrize( "mode,lines", [ - ("markdown", ["First line", "", "", "• 1", "• 2", "• 3", ""]), + ("markdown", ["First line", "", "• 1", "• 2", "• 3", ""]), ("rich", ["First line", "", "- 1", "- 2", "- 3", ""]), ("none", ["First line", "", "- 1 - 2 - 3", ""]), ], @@ -151,7 +153,7 @@ def main(arg: str): @pytest.mark.parametrize( "mode,lines", [ - ("markdown", ["First line", "", "", "• 1", "• 2", "• a", "• b", "• 3", ""]), + ("markdown", ["First line", "", "• 1", "• 2", "• a", "• b", "• 3", ""]), ("rich", ["First line", "", "- 1", "- 2", "- a", "- b", "- 3", ""]), ("none", ["First line", "", "- 1 - 2 - a - b - 3", ""]), ], diff --git a/typer/rich_utils.py b/typer/rich_utils.py index b72a7c727d..6aadcd5adb 100644 --- a/typer/rich_utils.py +++ b/typer/rich_utils.py @@ -203,7 +203,8 @@ def _get_help_text( # Get remaining lines, remove single line breaks and format as dim if remaining_paragraphs: - yield Text("") + if markup_mode not in MARKUP_MODE_MARKDOWN: + yield Text("") if markup_mode not in (MARKUP_MODE_RICH, MARKUP_MODE_MARKDOWN): # Remove single linebreaks remaining_paragraphs = [ @@ -215,7 +216,7 @@ def _get_help_text( # Join back together remaining_lines = "\n".join(remaining_paragraphs) else: - # Join with double linebreaks if markdown + # Join with double linebreaks if markdown or Rich markup remaining_lines = "\n\n".join(remaining_paragraphs) yield _make_rich_text(