pikopiko-cli 0.1.0

Command-line tool and MCP server to check, inspect, render and play MML with pikopiko.
Documentation
//! `info`:曲の概要(演奏時間・テンポ・推定した調・トラックごとの音数と音域など)を表示する。
//!
//! `--json` の形は MCP の `describe_mml` と同じ(`pikopiko::SongInfo`)。

use super::*;

pub(crate) fn info(src: &str, path: &str, o: &Opts) -> Result<ExitCode, String> {
    let json = o.json;
    let (song, diags) = compile(src, o);
    let Some(song) = song else {
        if json {
            return Ok(check(src, path, o));
        }
        print_diagnostics(src, path, &diags);
        return Ok(ExitCode::from(1));
    };
    let info = pikopiko::describe_with(&song, pikopiko::synth::all());
    if json {
        println!(
            "{}",
            serde_json::to_string_pretty(&info).map_err(json_error)?
        );
        return Ok(ExitCode::SUCCESS);
    }
    print_diagnostics(src, path, &diags);
    let mut out = std::io::stdout().lock();
    for (key, name) in [
        ("cli-info-title", "title"),
        ("cli-info-composer", "composer"),
        ("cli-info-artist", "artist"),
        ("cli-info-copyright", "copyright"),
        ("cli-info-comment", "comment"),
    ] {
        if let Some(v) = info.meta.get(name) {
            let _ = writeln!(out, "{}: {}", t(key), v.replace('\n', "\n  "));
        }
    }
    let _ = writeln!(
        out,
        "{}",
        ta(
            "cli-info-duration",
            &[
                ("secs", format!("{:.2}", info.duration_secs).into()),
                ("ticks", info.length_ticks.to_string().into()),
                ("ppq", info.ppq.to_string().into()),
            ]
        )
    );
    let tempos: Vec<String> = info.tempos.iter().map(|t| t.to_string()).collect();
    let _ = writeln!(
        out,
        "{}",
        ta("cli-info-tempo", &[("tempos", tempos.join(", ").into())])
    );
    if let Some(k) = &info.key {
        let _ = writeln!(
            out,
            "{}",
            ta(
                "cli-info-key",
                &[
                    ("name", k.name.as_str().into()),
                    ("confidence", format!("{:.2}", k.confidence).into()),
                ]
            )
        );
    }
    let opts: Vec<String> = song
        .synth
        .options
        .iter()
        .map(|(k, v)| format!("{k}={v}"))
        .collect();
    let synth = format!("{} {}", song.synth.name, opts.join(" "));
    let _ = writeln!(out, "{}", ta("cli-info-synth", &[("synth", synth.into())]));
    let _ = writeln!(
        out,
        "{}",
        ta("cli-info-tracks", &[("count", info.tracks.len().into())])
    );
    let (notes, range_label, tones_label, envs_label) = (
        t("cli-info-notes"),
        t("cli-info-range"),
        t("cli-info-tones"),
        t("cli-info-envelopes"),
    );
    for t in &info.tracks {
        let range = match (&t.lowest_note, &t.highest_note) {
            (Some(l), Some(h)) => ta(
                "cli-info-range-value",
                &[("low", l.as_str().into()), ("high", h.as_str().into())],
            ),
            _ => "—".into(),
        };
        let tones: Vec<String> = t.tones.iter().map(|x| format!("@{x}")).collect();
        let envs: Vec<String> = t.envelopes.iter().map(|x| format!("@env {x}")).collect();
        let _ = writeln!(
            out,
            "  {}: {notes} {:>4}  {range_label} {:<10}  {tones_label} {}{}",
            t.name,
            t.note_count,
            range,
            if tones.is_empty() {
                "—".into()
            } else {
                tones.join(" ")
            },
            if envs.is_empty() {
                String::new()
            } else {
                format!("  {envs_label} {}", envs.join(" "))
            }
        );
    }
    Ok(ExitCode::SUCCESS)
}