Skip to content

Commit

Permalink
more template work
Browse files Browse the repository at this point in the history
  • Loading branch information
rikardg committed Apr 7, 2024
1 parent ca42f99 commit a9297d5
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/bygg/cmd/dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
dump_schema,
read_config_file,
)
from bygg.cmd.templates import complete_templates, describe_templates
from bygg.cmd.tree import display_tree
from bygg.core.runner import ProcessRunner
from bygg.core.scheduler import Scheduler
Expand Down Expand Up @@ -290,6 +291,10 @@ def dispatcher():
print_version()
sys.exit(0)

if args.describe_templates:
describe_templates()
sys.exit(0)

directory = args.directory[0] if args.directory else None
is_restarted_with_env = (
args.is_restarted_with_env[0] if args.is_restarted_with_env else None
Expand Down Expand Up @@ -599,5 +604,17 @@ def create_argument_parser():
action="store_true",
help="Output instructions for how to set up shell completions via the shell's startup script.",
)
meta_group.add_argument(
"--describe-templates",
action="store_true",
help="Describe templates.",
)
meta_group.add_argument(
"--init-template",
nargs=1,
type=str,
choices=complete_templates(),
help="Create init files from a template.",
)

return parser
47 changes: 47 additions & 0 deletions src/bygg/cmd/templates.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from pathlib import Path
import shutil
import textwrap

from bygg.output.output import (
TerminalStyle as TS,
)

TEMPLATE_PATH = Path(__file__).parent.parent / "templates"


def complete_templates() -> list[str]:
return [x.name for x in list_template_dirs()]


def describe_templates():
terminal_cols, terminal_rows = shutil.get_terminal_size()
output = [f"{TS.BOLD}Available templates:{TS.RESET}"]

for name, description in [
create_template_description(template) for template in list_template_dirs()
]:
output.append(f"{TS.BOLD}{name}{TS.RESET}")
for paragraph in description.split("\n\n"):
output.append(
textwrap.fill(
paragraph.strip(),
initial_indent=" ",
subsequent_indent=" ",
width=min(80, terminal_cols),
)
)

print("\n\n".join(output))


def create_template_description(path: Path) -> tuple[str, str]:
description_file = path / "description.txt"
try:
description = description_file.read_text("utf-8").strip()
except FileNotFoundError:
description = "No description provided"
return path.name, description


def list_template_dirs():
return (x for x in TEMPLATE_PATH.iterdir() if x.is_dir())
1 change: 1 addition & 0 deletions src/bygg/templates/standard/description.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Setup with a YAML file and Python files.
1 change: 1 addition & 0 deletions src/bygg/templates/taskrunner/description.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Only a YAML file for running tasks.

0 comments on commit a9297d5

Please sign in to comment.