🐛 Ensure that the default `rich_markup_mode` is interpreted correctly (#1304)
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 fd8311f · Browse files · 3 files·+19 added−26 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Trailers
- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Diff stats
3 files+19 added−26 removed
- tests:+6 added−0 removed
- other:+13 added−26 removed
Provenance
- Commit
23665f4- Tree
cc3ca46- Author
Sofie Van Landeghem <svlandeg@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
fd8311f
3 files changed+19 added−26 removed
modified:tests/test_rich_markup_mode.py+6 added
282 unmodified lines283 help_start = result_lines.index("First line")284 arg_start = [i for i, row in enumerate(result_lines) if "Arguments" in row][0]285 assert help_start != -1286 assert result_lines[help_start:arg_start] == lines287+288+289+def test_markup_mode_default():290+ # We're assuming the test suite is run with rich installed291+ app = typer.Typer()292+ assert app.rich_markup_mode == "rich"modified:typer/main.py+1 added-1 removed
133 unmodified lines134 hidden: bool = Default(False),135 deprecated: bool = Default(False),136 add_completion: bool = True,137 # Rich settings138- rich_markup_mode: MarkupMode = Default(DEFAULT_MARKUP_MODE),138+ rich_markup_mode: MarkupMode = DEFAULT_MARKUP_MODE,139 rich_help_panel: Union[str, None] = Default(None),140 suggest_commands: bool = True,141 pretty_exceptions_enable: bool = True,142 pretty_exceptions_show_locals: bool = True,1002 unmodified linesmodified:typer/rich_utils.py+12 added-25 removed
101 unmodified lines102 MARKUP_MODE_MARKDOWN = "markdown"103 MARKUP_MODE_RICH = "rich"104 _RICH_HELP_PANEL_NAME = "rich_help_panel"105 106-MarkupMode = Literal["markdown", "rich", None]106+MarkupModeStrict = Literal["markdown", "rich"]107 108 109 # Rich regex highlighter110 class OptionHighlighter(RegexHighlighter):39 unmodified lines150 )151 152 153 def _make_rich_text(154- *, text: str, style: str = "", markup_mode: MarkupMode154+ *, text: str, style: str = "", markup_mode: MarkupModeStrict155 ) -> Union[Markdown, Text]:156 """Take a string, remove indentations, and return styled text.157 158- By default, the text is not parsed for any special formatting.158 If `markup_mode` is `"rich"`, the text is parsed for Rich markup strings.159 If `markup_mode` is `"markdown"`, parse as Markdown.160 """161 # Remove indentations from input text162 text = inspect.cleandoc(text)163 if markup_mode == MARKUP_MODE_MARKDOWN:164 text = Emoji.replace(text)165 return Markdown(text, style=style)167- if markup_mode == MARKUP_MODE_RICH:168- return highlighter(Text.from_markup(text, style=style))166 else:167+ assert markup_mode == MARKUP_MODE_RICH170- return highlighter(Text(text, style=style))168+ return highlighter(Text.from_markup(text, style=style))169 170 171 @group()172 def _get_help_text(173 *,174 obj: Union[click.Command, click.Group],177- markup_mode: MarkupMode,175+ markup_mode: MarkupModeStrict,176 ) -> Iterable[Union[Markdown, Text]]:177 """Build primary help text for a click command or group.178 179 Returns the prose help text for a command or group, rendered either as a25 unmodified lines205 # Get remaining lines, remove single line breaks and format as dim206 if remaining_paragraphs:207 # Add a newline inbetween the header and the remaining paragraphs208 yield Text("")211- if markup_mode not in (MARKUP_MODE_RICH, MARKUP_MODE_MARKDOWN):212- # Remove single linebreaks213- remaining_paragraphs = [214- x.replace("\n", " ").strip()215- if not x.startswith("\b")216- else "{}\n".format(x.strip("\b\n"))217- for x in remaining_paragraphs218- ]219- # Join back together220- remaining_lines = "\n".join(remaining_paragraphs)221- else:222- # Join with double linebreaks if markdown or Rich markup223- remaining_lines = "\n\n".join(remaining_paragraphs)209+ # Join with double linebreaks for markdown and Rich markup210+ remaining_lines = "\n\n".join(remaining_paragraphs)211 212 yield _make_rich_text(213 text=remaining_lines,214 style=STYLE_HELPTEXT,4 unmodified lines219 def _get_parameter_help(220 *,221 param: Union[click.Option, click.Argument, click.Parameter],222 ctx: click.Context,236- markup_mode: MarkupMode,223+ markup_mode: MarkupModeStrict,224 ) -> Columns:225 """Build primary help text for a click option or argument.226 227 Returns the prose help text for an option or argument, rendered either79 unmodified lines307 308 def _make_command_help(309 *,310 help_text: str,324- markup_mode: MarkupMode,311+ markup_mode: MarkupModeStrict,312 ) -> Union[Text, Markdown]:313 """Build cli help text for a click group command.314 315 That is, when calling help on groups with multiple subcommands20 unmodified lines336 *,337 name: str,338 params: Union[List[click.Option], List[click.Argument]],339 ctx: click.Context,353- markup_mode: MarkupMode,340+ markup_mode: MarkupModeStrict,341 console: Console,342 ) -> None:343 options_rows: List[List[RenderableType]] = []344 required_rows: List[Union[str, Text]] = []118 unmodified lines463 def _print_commands_panel(464 *,465 name: str,466 commands: List[click.Command],480- markup_mode: MarkupMode,467+ markup_mode: MarkupModeStrict,468 console: Console,469 cmd_len: int,470 ) -> None:471 t_styles: Dict[str, Any] = {67 unmodified lines539 def rich_format_help(540 *,541 obj: Union[click.Command, click.Group],542 ctx: click.Context,556- markup_mode: MarkupMode,543+ markup_mode: MarkupModeStrict,544 ) -> None:545 """Print nicely formatted help text using rich.546 547 Based on original code from rich-cli, by @willmcgugan.210 unmodified lines