clairlune-cli 0.1.0

Developer CLI for Clairlune: render procedural ASCII-art wallpapers to PNG from a seed
//! `clairlune-cli`: developer tool for rendering scenes without the desktop app.

use clairlune_render::{render_still, save_png};
use clairlune_scene::{
    APP_NAME, Aspect, BuildOptions, ContentContext, SceneRecipe, Theme, compose,
};
use clap::{Args, Parser, Subcommand, ValueEnum};
use std::collections::hash_map::RandomState;
use std::hash::BuildHasher;
use std::path::PathBuf;
use std::process::ExitCode;
use std::time::Instant;

#[derive(Parser)]
#[command(
    name = "clairlune-cli",
    version,
    about = "Render Clairlune ASCII-art wallpapers from a seed"
)]
struct Cli {
    #[command(subcommand)]
    command: Command,
}

#[derive(Subcommand)]
enum Command {
    /// Compose a scene from a seed (or load a recipe) and render a still PNG.
    Generate(GenerateArgs),
}

#[derive(Args)]
struct GenerateArgs {
    /// Scene seed. A random seed is used (and printed) when omitted.
    #[arg(long)]
    seed: Option<u64>,
    /// Render an existing recipe JSON instead of composing from a seed.
    #[arg(long, conflicts_with = "seed")]
    recipe: Option<PathBuf>,
    #[arg(long, value_enum, default_value_t = ThemeArg::Dark)]
    theme: ThemeArg,
    /// Output size, e.g. 3840x2160.
    #[arg(long, default_value = "1920x1080", value_parser = parse_size)]
    size: (u32, u32),
    /// Output PNG path.
    #[arg(long)]
    out: PathBuf,
    /// Render without the character (the unattended variant).
    #[arg(long)]
    no_buddy: bool,
    /// Also write the scene recipe as JSON.
    #[arg(long)]
    recipe_out: Option<PathBuf>,
    /// Animation time in milliseconds. Defaults to the scene's hero time.
    #[arg(long)]
    time_ms: Option<u64>,
}

#[derive(Clone, Copy, ValueEnum)]
enum ThemeArg {
    Light,
    Dark,
}

impl From<ThemeArg> for Theme {
    fn from(t: ThemeArg) -> Self {
        match t {
            ThemeArg::Light => Theme::Light,
            ThemeArg::Dark => Theme::Dark,
        }
    }
}

fn parse_size(s: &str) -> Result<(u32, u32), String> {
    let (w, h) = s
        .split_once(['x', 'X'])
        .ok_or_else(|| format!("expected WIDTHxHEIGHT, got `{s}`"))?;
    let w: u32 = w
        .trim()
        .parse()
        .map_err(|_| format!("bad width in `{s}`"))?;
    let h: u32 = h
        .trim()
        .parse()
        .map_err(|_| format!("bad height in `{s}`"))?;
    if w == 0 || h == 0 {
        return Err("width and height must be positive".into());
    }
    Ok((w, h))
}

/// A seed from OS entropy: `RandomState` keys come from the OS random source.
fn random_seed() -> u64 {
    RandomState::new().hash_one(0x5EED_u64)
}

fn generate(args: GenerateArgs) -> Result<(), String> {
    let (width, height) = args.size;
    let started = Instant::now();
    let recipe = match &args.recipe {
        Some(path) => {
            let text = std::fs::read_to_string(path)
                .map_err(|e| format!("reading {}: {e}", path.display()))?;
            SceneRecipe::from_json(&text).map_err(|e| format!("parsing {}: {e}", path.display()))?
        }
        None => {
            let seed = args.seed.unwrap_or_else(random_seed);
            compose(
                seed,
                Aspect::from_size(width, height),
                &ContentContext::default(),
            )
        }
    };
    let opts = BuildOptions {
        width,
        height,
        theme: args.theme.into(),
        buddy: !args.no_buddy,
    };
    let still = render_still(&recipe, &opts, args.time_ms).map_err(|e| e.to_string())?;
    let rendered = started.elapsed();

    if let Some(parent) = args.out.parent().filter(|p| !p.as_os_str().is_empty()) {
        std::fs::create_dir_all(parent)
            .map_err(|e| format!("creating {}: {e}", parent.display()))?;
    }
    save_png(&args.out, &still.image, &still.palette)
        .map_err(|e| format!("writing {}: {e}", args.out.display()))?;
    if let Some(path) = &args.recipe_out {
        std::fs::write(path, recipe.to_json())
            .map_err(|e| format!("writing {}: {e}", path.display()))?;
    }
    let total = started.elapsed();
    let bytes = std::fs::metadata(&args.out).map(|m| m.len()).unwrap_or(0);

    println!("{APP_NAME} scene");
    println!("  seed      {}", recipe.seed);
    println!(
        "  archetype {}  ({} {:?})",
        recipe.archetype,
        opts.theme.as_str(),
        still.graph.tod
    );
    println!(
        "  size      {width}x{height}  grid {}x{} cells of {}x{} px",
        still.graph.grid.cols,
        still.graph.grid.rows,
        still.graph.grid.cell_w,
        still.graph.grid.cell_h
    );
    println!(
        "  frame     t = {} ms (hero {} ms)",
        still.t_ms, still.graph.t_hero_ms
    );
    println!("  palette   {} colours", still.palette.len());
    println!(
        "  time      render {:.0} ms, total {:.0} ms",
        rendered.as_secs_f64() * 1e3,
        total.as_secs_f64() * 1e3
    );
    println!(
        "  wrote     {} ({:.1} KB)",
        args.out.display(),
        bytes as f64 / 1024.0
    );
    Ok(())
}

fn main() -> ExitCode {
    let cli = Cli::parse();
    let result = match cli.command {
        Command::Generate(args) => generate(args),
    };
    match result {
        Ok(()) => ExitCode::SUCCESS,
        Err(e) => {
            eprintln!("error: {e}");
            ExitCode::FAILURE
        }
    }
}