hf-release / src /hf_release /utils /display.py
MikeChandler's picture
feat: initial release of hf-release v0.1.0
f824e2f verified
"""Centralized Rich display helpers."""
from __future__ import annotations
from dataclasses import dataclass
from rich.console import Console
from rich.panel import Panel
from rich.progress import BarColumn, Progress, SpinnerColumn, TextColumn, TimeElapsedColumn
from rich.table import Table
# Use stderr so stdout stays clean for piped/script usage
console = Console(stderr=True)
@dataclass
class ReleaseArtifact:
version: str
hf_repo: str | None
gh_repo: str | None
hf_url: str | None
gh_url: str | None
hf_tag: str | None
gh_tag: str | None
def make_progress() -> Progress:
return Progress(
SpinnerColumn(),
TextColumn("[progress.description]{task.description}"),
BarColumn(),
TimeElapsedColumn(),
console=console,
)
def print_summary_table(artifact: ReleaseArtifact) -> None:
table = Table(title=f"Release {artifact.version} — Summary", show_lines=True)
table.add_column("Platform", style="bold cyan", no_wrap=True)
table.add_column("Repo")
table.add_column("Tag", style="green")
table.add_column("URL", style="blue")
if artifact.hf_repo:
table.add_row(
"Hugging Face Hub",
artifact.hf_repo,
artifact.hf_tag or "-",
artifact.hf_url or "-",
)
if artifact.gh_repo:
table.add_row(
"GitHub",
artifact.gh_repo,
artifact.gh_tag or "-",
artifact.gh_url or "-",
)
console.print(table)
def print_error(exc: Exception, suggestion: str = "") -> None:
suggestion_text = suggestion or getattr(exc, "suggestion", "")
body = str(exc)
if suggestion_text:
body += f"\n\n[dim]Suggestion:[/dim] {suggestion_text}"
console.print(Panel(body, title="[bold red]Error[/bold red]", border_style="red"))
def print_dry_run_panel(steps: list[str]) -> None:
body = "\n".join(f" • {s}" for s in steps)
console.print(
Panel(
f"[bold]The following actions would be performed:[/bold]\n\n{body}",
title="[bold blue]Dry Run[/bold blue]",
border_style="blue",
)
)
def print_partial_success(completed: list[str], failed: list[str]) -> None:
body = ""
if completed:
body += "[green]Completed:[/green]\n" + "\n".join(f" ✓ {s}" for s in completed) + "\n\n"
if failed:
body += "[red]Failed:[/red]\n" + "\n".join(f" ✗ {s}" for s in failed)
console.print(Panel(body, title="[bold yellow]Partial Success[/bold yellow]", border_style="yellow"))
def confirm_plan(steps: list[str]) -> bool:
"""Print a plan summary and ask for confirmation. Returns True if confirmed."""
body = "\n".join(f" • {s}" for s in steps)
console.print(
Panel(
f"[bold]About to execute:[/bold]\n\n{body}",
title="[bold cyan]Release Plan[/bold cyan]",
border_style="cyan",
)
)
import typer
return typer.confirm("Proceed?")