forked from The-DevOps-Daily/devops-daily
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlisting.py
More file actions
74 lines (53 loc) · 1.84 KB
/
listing.py
File metadata and controls
74 lines (53 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
"""Command utilities for listing available lessons."""
from __future__ import annotations
from pathlib import Path
from typing import Iterable, Optional
import typer
from state import set_verbose, verbose_active
app = typer.Typer(help="List available Linux command lessons.")
_CONTENT_DIR = Path(__file__).resolve().parents[2] / "ebook" / "en" / "content"
def _get_lessons() -> Iterable[Path]:
if not _CONTENT_DIR.exists():
return []
return sorted(_CONTENT_DIR.glob("*.md"))
def _format_title(path: Path) -> str:
stem = path.stem
prefix, _, slug = stem.partition("-")
title = slug.replace("-", " ").strip().title() if slug else prefix.replace("-", " ")
if prefix.isdigit():
return f"{prefix} {title}".strip()
return title
@app.callback(invoke_without_command=True)
def list_commands(
ctx: typer.Context,
limit: Optional[int] = typer.Option(
None,
"--limit",
"-l",
min=1,
help="Limit the number of commands displayed. Shows all when omitted.",
),
verbose: bool = typer.Option(
False,
"--verbose",
"-v",
is_flag=True,
help="Enable verbose output for this command.",
),
) -> None:
"""Display the available Linux command lessons."""
if verbose:
set_verbose(ctx, True)
lessons = list(_get_lessons())
total = len(lessons)
if verbose_active(ctx):
typer.echo(f"[verbose] Located {total} command lessons", err=True)
if total == 0:
typer.echo("No command lessons found.")
return
limit_value = total if limit is None else min(limit, total)
if verbose_active(ctx) and limit is not None:
typer.echo(f"[verbose] Limiting output to {limit_value} entries", err=True)
for path in lessons[:limit_value]:
typer.echo(_format_title(path))
__all__ = ["app"]