"""kdc preview command — standalone preview rendering.""" from __future__ import annotations from pathlib import Path import typer from rich.console import Console from rich.progress import track from app.utils.container import Container from app.utils.file import iter_pdfs app = typer.Typer(help="Render page preview images from PDFs.") console = Console() @app.command() def run( ctx: typer.Context, pdf_dir: Path = typer.Option(Path("pdf"), "--pdf-dir", help="PDF directory."), ) -> None: """Render page preview images for all PDFs in PDF_DIR.""" container: Container = ctx.obj renderer = container.renderer if not renderer: console.print("[red]Renderer service not available.[/red]") raise typer.Exit(1) pdf_dir = pdf_dir.resolve() pdfs = iter_pdfs(pdf_dir) if not pdfs: console.print(f"[yellow]No PDFs found in {pdf_dir}[/yellow]") raise typer.Exit() rendered = 0 for pdf in track(pdfs, description="Rendering previews..."): try: renderer.render(pdf) rendered += 1 except Exception as exc: console.print(f"[red]Failed {pdf.name}: {exc}[/red]") console.print(f"[green]✓ Rendered preview images for {rendered} PDF(s).[/green]")