Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make sure rich_markup_mode=None disables rich #726

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 34 additions & 0 deletions tests/test_rich_markup_mode.py
@@ -0,0 +1,34 @@
import typer
import typer.completion
from typer.testing import CliRunner

runner = CliRunner()
rounded = ["╭", "─", "┬", "╮", "│", "├", "┼", "┤", "╰", "┴", "╯"]


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

@app.command()
def main(arg: str):
"""Main function"""
print("Hello World")

assert app.rich_markup_mode == None

result = runner.invoke(app, ["--help"])
assert all(c not in result.stdout for c in rounded)


def test_rich_markup_mode_rich():
app = typer.Typer(rich_markup_mode="rich")

@app.command()
def main(arg: str):
"""Main function"""
print("Hello World")

assert app.rich_markup_mode == "rich"

result = runner.invoke(app, ["--help"])
assert any(c in result.stdout for c in rounded)
Expand Up @@ -9,7 +9,7 @@

runner = CliRunner()

app = typer.Typer()
app = typer.Typer(rich_markup_mode="rich")
app.command()(mod.main)


Expand Down
Expand Up @@ -9,7 +9,7 @@

runner = CliRunner()

app = typer.Typer()
app = typer.Typer(rich_markup_mode="rich")
app.command()(mod.main)


Expand Down
Expand Up @@ -6,6 +6,7 @@
from docs_src.commands.help import tutorial003 as mod

app = mod.app
app.rich_markup_mode = "rich"

runner = CliRunner()

Expand Down
Expand Up @@ -8,7 +8,7 @@

runner = CliRunner()

app = typer.Typer()
app = typer.Typer(rich_markup_mode="rich")
app.command()(mod.main)


Expand Down
Expand Up @@ -8,7 +8,7 @@

runner = CliRunner()

app = typer.Typer()
app = typer.Typer(rich_markup_mode="rich")
app.command()(mod.main)


Expand Down
13 changes: 9 additions & 4 deletions typer/core.py
Expand Up @@ -178,6 +178,7 @@ def _main(
complete_var: Optional[str] = None,
standalone_mode: bool = True,
windows_expand_args: bool = True,
rich_markup_mode: MarkupMode = None,
**extra: Any,
) -> Any:
# Typer override, duplicated from click.main() to handle custom rich exceptions
Expand Down Expand Up @@ -232,7 +233,7 @@ def _main(
if not standalone_mode:
raise
# Typer override
if rich:
if rich and rich_markup_mode is not None:
rich_utils.rich_format_error(e)
else:
e.show()
Expand Down Expand Up @@ -262,7 +263,7 @@ def _main(
if not standalone_mode:
raise
# Typer override
if rich:
if rich and rich_markup_mode is not None:
rich_utils.rich_abort_error()
else:
click.echo(_("Aborted!"), file=sys.stderr)
Expand Down Expand Up @@ -712,6 +713,7 @@ def main(
complete_var: Optional[str] = None,
standalone_mode: bool = True,
windows_expand_args: bool = True,
rich_markup_mode: MarkupMode = None,
**extra: Any,
) -> Any:
return _main(
Expand All @@ -721,11 +723,12 @@ def main(
complete_var=complete_var,
standalone_mode=standalone_mode,
windows_expand_args=windows_expand_args,
rich_markup_mode=rich_markup_mode,
**extra,
)

def format_help(self, ctx: click.Context, formatter: click.HelpFormatter) -> None:
if not rich:
if not rich or self.rich_markup_mode is None:
return super().format_help(ctx, formatter)
return rich_utils.rich_format_help(
obj=self,
Expand Down Expand Up @@ -774,6 +777,7 @@ def main(
complete_var: Optional[str] = None,
standalone_mode: bool = True,
windows_expand_args: bool = True,
rich_markup_mode: MarkupMode = None,
**extra: Any,
) -> Any:
return _main(
Expand All @@ -783,11 +787,12 @@ def main(
complete_var=complete_var,
standalone_mode=standalone_mode,
windows_expand_args=windows_expand_args,
rich_markup_mode=rich_markup_mode,
**extra,
)

def format_help(self, ctx: click.Context, formatter: click.HelpFormatter) -> None:
if not rich:
if not rich or self.rich_markup_mode is None:
return super().format_help(ctx, formatter)
return rich_utils.rich_format_help(
obj=self,
Expand Down