2024-02-17 15:24:13 -08:00
|
|
|
from collections import defaultdict
|
2023-12-23 15:36:42 -08:00
|
|
|
from rolltable.types import RollTable
|
2024-01-17 21:15:39 -08:00
|
|
|
from rolltable import tables
|
2022-07-30 14:20:26 -07:00
|
|
|
import typer
|
2023-04-12 10:37:31 -07:00
|
|
|
from enum import Enum
|
2022-07-30 14:20:26 -07:00
|
|
|
from rich import print
|
2022-07-31 15:03:19 -07:00
|
|
|
from pathlib import Path
|
|
|
|
from typing import List
|
2022-07-30 14:20:26 -07:00
|
|
|
|
|
|
|
|
|
|
|
app = typer.Typer()
|
2024-02-17 15:24:13 -08:00
|
|
|
app_state = defaultdict(
|
|
|
|
str,
|
|
|
|
options=defaultdict(str),
|
|
|
|
)
|
2022-07-30 14:20:26 -07:00
|
|
|
|
|
|
|
|
2023-04-12 10:37:31 -07:00
|
|
|
class OUTPUT_FORMATS(Enum):
|
|
|
|
text = 'text'
|
|
|
|
yaml = 'yaml'
|
|
|
|
markdown = 'markdown'
|
|
|
|
|
|
|
|
|
2024-02-17 15:24:13 -08:00
|
|
|
@app.callback()
|
|
|
|
def main(
|
2022-07-30 14:20:26 -07:00
|
|
|
frequency: str = typer.Option(
|
|
|
|
'default',
|
2024-02-17 15:24:13 -08:00
|
|
|
help='use the specified frequency from the source file'
|
|
|
|
),
|
2022-07-30 14:20:26 -07:00
|
|
|
die: int = typer.Option(
|
|
|
|
20,
|
2024-02-17 15:24:13 -08:00
|
|
|
help='The size of the die for which to create a table'
|
|
|
|
),
|
2023-04-12 10:37:31 -07:00
|
|
|
hide_rolls: bool = typer.Option(
|
|
|
|
False,
|
|
|
|
help='If True, do not show the Roll column.',
|
|
|
|
),
|
2022-07-31 15:03:19 -07:00
|
|
|
collapsed: bool = typer.Option(
|
2022-07-30 14:20:26 -07:00
|
|
|
True,
|
2024-02-17 15:24:13 -08:00
|
|
|
help='If True, collapse multiple die values with the same option.'
|
|
|
|
),
|
2023-04-12 10:37:31 -07:00
|
|
|
width: int = typer.Option(
|
|
|
|
120,
|
2024-02-17 15:24:13 -08:00
|
|
|
help='Width of the table.'
|
|
|
|
),
|
2023-04-12 10:37:31 -07:00
|
|
|
output: OUTPUT_FORMATS = typer.Option(
|
|
|
|
'text',
|
|
|
|
help='The output format to use.',
|
|
|
|
)
|
2024-02-17 15:24:13 -08:00
|
|
|
):
|
|
|
|
app_state['options'] = {
|
|
|
|
'frequency': frequency,
|
|
|
|
'die': die,
|
|
|
|
'hide_rolls': hide_rolls,
|
|
|
|
}
|
|
|
|
app_state['collapsed'] = collapsed
|
|
|
|
app_state['width'] = width
|
|
|
|
app_state['output'] = output
|
|
|
|
|
|
|
|
|
|
|
|
@app.command("custom")
|
|
|
|
def custom(
|
|
|
|
sources: List[Path] = typer.Argument(
|
|
|
|
...,
|
|
|
|
help="Path to one or more yaml-formatted source file."),
|
2022-07-30 14:20:26 -07:00
|
|
|
):
|
|
|
|
"""
|
2024-01-16 22:05:06 -08:00
|
|
|
Create roll tables from custom sources.
|
2022-07-30 14:20:26 -07:00
|
|
|
"""
|
2022-07-31 15:03:19 -07:00
|
|
|
|
2024-02-17 15:24:13 -08:00
|
|
|
rt = RollTable([Path(s).read_text() for s in sources], **app_state['options'])
|
|
|
|
print_table(rt)
|
|
|
|
|
2022-07-31 15:03:19 -07:00
|
|
|
|
2024-02-17 15:24:13 -08:00
|
|
|
def print_table(table):
|
|
|
|
if app_state['output'] == OUTPUT_FORMATS.yaml:
|
|
|
|
print(table.as_yaml())
|
|
|
|
elif app_state['output'] == OUTPUT_FORMATS.markdown:
|
|
|
|
print(table.as_markdown())
|
2023-04-12 10:37:31 -07:00
|
|
|
else:
|
2024-02-17 15:24:13 -08:00
|
|
|
print(table.as_table(
|
|
|
|
width=app_state['width'],
|
|
|
|
expanded=not app_state['collapsed']
|
|
|
|
))
|
2022-07-30 14:20:26 -07:00
|
|
|
|
|
|
|
|
2024-01-17 21:15:39 -08:00
|
|
|
def make_callback(roll_table_instance):
|
|
|
|
def inner():
|
2024-02-17 15:24:13 -08:00
|
|
|
roll_table_instance.frequency = app_state['options']['frequency']
|
|
|
|
roll_table_instance.die = app_state['options']['die']
|
|
|
|
print_table(roll_table_instance)
|
2024-01-17 21:15:39 -08:00
|
|
|
return inner
|
|
|
|
|
|
|
|
|
|
|
|
# step through all the predfined tables and create a cli for each
|
|
|
|
for name, table in tables.index.items():
|
|
|
|
help_text = name.replace('_', ' ').title()
|
|
|
|
app.command(name=name, help=f"Create a roll table of {help_text}")(
|
|
|
|
make_callback(table)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-07-30 14:20:26 -07:00
|
|
|
if __name__ == '__main__':
|
|
|
|
app()
|