dnd-item-generator/dnd_item/cli.py

89 lines
2.4 KiB
Python
Raw Normal View History

2023-12-04 22:28:03 -08:00
import logging
import os
2023-12-26 18:41:43 -08:00
from enum import Enum
from pathlib import Path
2023-12-04 22:28:03 -08:00
import typer
2023-12-26 18:41:43 -08:00
from rich import print
2023-12-04 22:28:03 -08:00
from rich.console import Console
2023-12-29 19:24:26 -08:00
from rich.logging import RichHandler
2023-12-26 18:41:43 -08:00
from rich.table import Table
2023-12-04 22:28:03 -08:00
2023-12-29 19:24:26 -08:00
from dnd_item import five_e
2023-12-27 22:25:12 -08:00
from dnd_item.types import RollTable
from dnd_item.weapons import WeaponGenerator
2023-12-04 22:28:03 -08:00
app = typer.Typer()
app_state = {}
2023-12-04 22:28:03 -08:00
2023-12-26 18:41:43 -08:00
class OUTPUT_FORMATS(Enum):
2023-12-29 19:24:26 -08:00
text = "text"
yaml = "yaml"
markdown = "markdown"
2023-12-26 18:41:43 -08:00
2023-12-04 22:28:03 -08:00
@app.callback()
2023-12-24 12:24:33 -08:00
def main(
2023-12-29 19:24:26 -08:00
cr: int = typer.Option(default=None, help="The Challenge Rating to use when determining rarity."),
2023-12-24 12:24:33 -08:00
):
2023-12-04 22:28:03 -08:00
debug = os.getenv("FANITEM_DEBUG", None)
logging.basicConfig(
format="%(name)s %(message)s",
level=logging.DEBUG if debug else logging.INFO,
handlers=[RichHandler(rich_tracebacks=True, tracebacks_suppress=[typer])],
)
2023-12-29 19:24:26 -08:00
logging.getLogger("markdown_it").setLevel(logging.ERROR)
2023-12-04 22:28:03 -08:00
2023-12-29 19:24:26 -08:00
app_state["cr"] = cr or 0
app_state["data"] = Path(__file__).parent / Path("sources")
2023-12-04 22:28:03 -08:00
@app.command()
2023-12-23 16:41:21 -08:00
def weapon(count: int = typer.Option(1, help="The number of weapons to generate.")):
console = Console()
2023-12-29 19:24:26 -08:00
for weapon in WeaponGenerator().random(count=count, challenge_rating=app_state["cr"]):
2023-12-23 16:41:21 -08:00
console.print(weapon.details)
2023-12-26 18:41:43 -08:00
@app.command("roll-table")
def table(
2023-12-29 19:24:26 -08:00
die: int = typer.Option(20, help="The size of the die for which to create a table"),
2023-12-26 18:41:43 -08:00
hide_rolls: bool = typer.Option(
False,
2023-12-29 19:24:26 -08:00
help="If True, do not show the Roll column.",
2023-12-26 18:41:43 -08:00
),
2023-12-29 19:24:26 -08:00
collapsed: bool = typer.Option(True, help="If True, collapse multiple die values with the same option."),
width: int = typer.Option(180, help="Width of the table."),
2023-12-26 18:41:43 -08:00
output: OUTPUT_FORMATS = typer.Option(
2023-12-29 19:24:26 -08:00
"text",
help="The output format to use.",
),
2023-12-26 18:41:43 -08:00
):
"""
CLI for creating roll tables of randomly-generated items.
"""
rt = RollTable(
2023-12-27 00:42:31 -08:00
sources=[WeaponGenerator],
2023-12-26 18:41:43 -08:00
die=die,
hide_rolls=hide_rolls,
2023-12-29 19:24:26 -08:00
challenge_rating=app_state["cr"],
2023-12-26 18:41:43 -08:00
)
if output == OUTPUT_FORMATS.yaml:
print(rt.as_yaml())
elif output == OUTPUT_FORMATS.markdown:
print(rt.as_markdown)
else:
rows = rt.rows if collapsed else rt.expanded_rows
table = Table(*rows[0], width=width)
for row in rows[1:]:
table.add_row(*row)
print(table)
@app.command()
def convert():
src = five_e.weapons()
print(src.as_yaml)