You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
spiel/spiel/cli.py

141 lines
3.4 KiB
Python

3 years ago
import shutil
3 years ago
from pathlib import Path
from textwrap import dedent
from typing import Optional
3 years ago
import typer.rich_utils as ru
from click.exceptions import Exit
3 years ago
from rich.console import Console
3 years ago
from rich.style import Style
from rich.syntax import Syntax
3 years ago
from rich.text import Text
from typer import Argument, Option, Typer
3 years ago
from spiel.app import present
from spiel.constants import DEMO_DIR, DEMO_FILE, PACKAGE_DIR, PACKAGE_NAME, __version__
from spiel.renderables.debug import DebugTable
3 years ago
ru.STYLE_HELPTEXT = ""
3 years ago
console = Console()
cli = Typer(
name=PACKAGE_NAME,
no_args_is_help=True,
rich_markup_mode="rich",
help=dedent(
f"""\
Display [italic yellow]Rich[/italic yellow]ly-styled presentations using your terminal.
To see what {PACKAGE_NAME.capitalize()} can do, take a look at the demo deck:
$ spiel demo present
A {PACKAGE_NAME.capitalize()} presentation (a "[italic green]deck[/italic green] of slides")
is defined programmatically using a Python script.
"""
),
)
3 years ago
@cli.command(name="present")
def _present(
path: Path = Argument(
...,
dir_okay=False,
exists=True,
readable=True,
help="The path to the slide deck file.",
),
watch: Optional[Path] = Option(
default=None,
help="When filesystem changes are detected below this path (recursively), reload the deck from the deck path. Defaults to the parent directory of the deck path.",
3 years ago
),
) -> None:
3 years ago
"""
Present a deck.
"""
present(deck_path=path, watch_path=watch)
3 years ago
3 years ago
demo = Typer(
name="demo",
no_args_is_help=True,
rich_markup_mode="rich",
help=dedent(
"""\
Use the demonstration deck (present it, display source, etc.).
"""
),
3 years ago
)
cli.add_typer(demo)
3 years ago
@demo.command(name="present")
def present_demo() -> None:
"""
Present the demo deck.
"""
present(deck_path=DEMO_FILE, watch_path=PACKAGE_DIR)
3 years ago
@demo.command()
def source() -> None:
"""
Display the source code for the demo deck in your PAGER.
"""
console = Console()
with console.pager(styles=True):
console.print(Syntax(DEMO_FILE.read_text(encoding="utf-8"), lexer="python"))
3 years ago
@demo.command()
def copy(
path: Path = Argument(
default=...,
exists=False,
3 years ago
writable=True,
help="The path to copy the demo deck source code and assets to.",
)
) -> None:
"""
Copy the demo deck source code and assets to a new directory.
If you're looking for a more stripped-down starting point, try the init command:
$ spiel init --help
3 years ago
"""
console = Console()
if path.exists():
console.print(Text(f"Error: {path} already exists!", style=Style(color="red")))
raise Exit(code=1)
3 years ago
try:
shutil.copytree(DEMO_DIR, path)
except Exception as e:
console.print(Text(f"Failed to copy demo deck directory: {e}", style=Style(color="red")))
raise Exit(code=1)
console.print(
Text(f"Wrote demo deck source code and assets to {path}", style=Style(color="green"))
)
@cli.command()
def version(
plain: bool = Option(
default=False,
help=f"Print only {PACKAGE_NAME}'s version.",
)
) -> None:
"""
Display version and debugging information.
"""
if plain:
console.print(Text(__version__))
else:
console.print(DebugTable())