ASCII-art banners¶
A banner is a word drawn with pyfiglet at the top
of a command's or group's --help page.
On a command or group¶
Pass ascii_name to command or add_typer:
import typer
from typer_swissknife import add_typer, command
app = typer.Typer()
admin = typer.Typer(help="Administrative commands.")
@command(app, ascii_name="Deploy")
def deploy(env: str) -> None:
"""Deploy the app to ENV.
Builds, uploads and restarts the service.
"""
print(f"Deploying to {env}")
add_typer(app, admin, name="admin", ascii_name="Admin")
$ python app.py deploy --help
██████╗ ███████╗██████╗ ██╗ ██████╗ ██╗ ██╗
██╔══██╗██╔════╝██╔══██╗██║ ██╔═══██╗╚██╗ ██╔╝
██║ ██║█████╗ ██████╔╝██║ ██║ ██║ ╚████╔╝
██║ ██║██╔══╝ ██╔═══╝ ██║ ██║ ██║ ╚██╔╝
██████╔╝███████╗██║ ███████╗╚██████╔╝ ██║
╚═════╝ ╚══════╝╚═╝ ╚══════╝ ╚═════╝ ╚═╝
Deploy the app to ENV.
Builds, uploads and restarts the service.
...
What changes and what does not:
- The help page shows the banner, then the help text (the
helpargument, else the docstring) as its own paragraph. - The commands list of the parent still shows the plain one-line help: the first
paragraph of the help, joined on one line ("Deploy the app to ENV."). Pass
short_helpto choose that text yourself. - With no help text at all, the commands list shows an empty description instead of the banner.
- The banner applies to every alias too.
- An empty or whitespace-only
ascii_nameraisesValueError.
Fonts¶
ascii_font picks any pyfiglet font. The default is "ansi_shadow", also exported as
DEFAULT_ASCII_FONT.
@command(app, ascii_name="Backup", ascii_font="slant")
def backup() -> None:
"""Back up the database."""
An unknown font raises pyfiglet.FontNotFound when the decorator runs. List the fonts
with pyfiglet.FigletFont.getFonts().
Markup modes¶
Typer rewraps help text differently in each rich_markup_mode, which would break the
banner's lines. The banner is wrapped for the mode of the app you register on
(app.rich_markup_mode), so it stays intact in all three:
rich_markup_mode |
How the banner is wrapped |
|---|---|
"rich" (Typer's default when Rich is installed) |
a paragraph starting with \b, with Rich markup escaped |
"markdown" |
a fenced text code block |
None |
a \b paragraph, which Click does not rewrap |
Building the help text yourself¶
Use get_ascii_help_text when you register commands with Typer directly, or want a
banner on the main app:
import typer
from typer_swissknife import get_ascii_help_text
app = typer.Typer(help=get_ascii_help_text("Shop", body="Manage the online shop."))
@app.command(help=get_ascii_help_text("Users", body="Manage users."))
def users() -> None: ...
@app.command()
def orders() -> None: ...
Pass the same markup_mode as the app's rich_markup_mode (it defaults to Typer's
default mode). The function does not set short_help, so with @app.command(help=...)
the commands list would show the banner; pass short_help="Manage users." as well when
that matters.
render_ascii_art(text, font=...) returns the bare art, with trailing spaces and blank
edge lines removed: