Skip to content
7 changes: 7 additions & 0 deletions tests/test_rich_markup_mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import pytest
import typer
import typer.completion
from typer.rich_utils import MARKUP_MODE_RICH
from typer.testing import CliRunner

runner = CliRunner()
Expand Down Expand Up @@ -284,3 +285,9 @@ 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


def test_markup_mode_default():
app = typer.Typer()

assert app.rich_markup_mode == MARKUP_MODE_RICH
2 changes: 1 addition & 1 deletion typer/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def __init__(
deprecated: bool = Default(False),
add_completion: bool = True,
# Rich settings
rich_markup_mode: MarkupMode = Default(DEFAULT_MARKUP_MODE),
rich_markup_mode: MarkupMode = DEFAULT_MARKUP_MODE,
rich_help_panel: Union[str, None] = Default(None),
pretty_exceptions_enable: bool = True,
pretty_exceptions_show_locals: bool = True,
Expand Down
36 changes: 12 additions & 24 deletions typer/rich_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@
_RICH_HELP_PANEL_NAME = "rich_help_panel"

MarkupMode = Literal["markdown", "rich", None]
MarkupModeStrict = Literal["markdown", "rich"]


# Rich regex highlighter
Expand Down Expand Up @@ -151,11 +152,10 @@ def _get_rich_console(stderr: bool = False) -> Console:


def _make_rich_text(
*, text: str, style: str = "", markup_mode: MarkupMode
*, text: str, style: str = "", markup_mode: MarkupModeStrict
) -> Union[Markdown, Text]:
"""Take a string, remove indentations, and return styled text.

By default, the text is not parsed for any special formatting.
If `markup_mode` is `"rich"`, the text is parsed for Rich markup strings.
If `markup_mode` is `"markdown"`, parse as Markdown.
"""
Expand All @@ -164,17 +164,16 @@ def _make_rich_text(
if markup_mode == MARKUP_MODE_MARKDOWN:
text = Emoji.replace(text)
return Markdown(text, style=style)
if markup_mode == MARKUP_MODE_RICH:
return highlighter(Text.from_markup(text, style=style))
else:
return highlighter(Text(text, style=style))
assert markup_mode == MARKUP_MODE_RICH
return highlighter(Text.from_markup(text, style=style))


@group()
def _get_help_text(
*,
obj: Union[click.Command, click.Group],
markup_mode: MarkupMode,
markup_mode: MarkupModeStrict,
) -> Iterable[Union[Markdown, Text]]:
"""Build primary help text for a click command or group.

Expand Down Expand Up @@ -208,19 +207,8 @@ 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)
# Join with double linebreaks for markdown and Rich markup
remaining_lines = "\n\n".join(remaining_paragraphs)

yield _make_rich_text(
text=remaining_lines,
Expand All @@ -233,7 +221,7 @@ def _get_parameter_help(
*,
param: Union[click.Option, click.Argument, click.Parameter],
ctx: click.Context,
markup_mode: MarkupMode,
markup_mode: MarkupModeStrict,
) -> Columns:
"""Build primary help text for a click option or argument.

Expand Down Expand Up @@ -321,7 +309,7 @@ def _get_parameter_help(
def _make_command_help(
*,
help_text: str,
markup_mode: MarkupMode,
markup_mode: MarkupModeStrict,
) -> Union[Text, Markdown]:
"""Build cli help text for a click group command.

Expand Down Expand Up @@ -350,7 +338,7 @@ def _print_options_panel(
name: str,
params: Union[List[click.Option], List[click.Argument]],
ctx: click.Context,
markup_mode: MarkupMode,
markup_mode: MarkupModeStrict,
console: Console,
) -> None:
options_rows: List[List[RenderableType]] = []
Expand Down Expand Up @@ -477,7 +465,7 @@ def _print_commands_panel(
*,
name: str,
commands: List[click.Command],
markup_mode: MarkupMode,
markup_mode: MarkupModeStrict,
console: Console,
cmd_len: int,
) -> None:
Expand Down Expand Up @@ -553,7 +541,7 @@ def rich_format_help(
*,
obj: Union[click.Command, click.Group],
ctx: click.Context,
markup_mode: MarkupMode,
markup_mode: MarkupModeStrict,
) -> None:
"""Print nicely formatted help text using rich.

Expand Down
Loading