offline-cli 0.4.0

The Offline Protocol developer CLI
//! Output helpers: consistent human formatting + `--json` machine output.

use console::style;
use serde::Serialize;

/// An error for a command that has already printed why it failed: `main`
/// exits with this status and prints nothing more.
#[derive(Debug)]
pub struct QuietExit(pub i32);

impl std::fmt::Display for QuietExit {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "exit status {}", self.0)
    }
}

impl std::error::Error for QuietExit {}

/// Print a value as pretty JSON (for `--json` mode).
pub fn json<T: Serialize>(value: &T) -> anyhow::Result<()> {
    println!("{}", serde_json::to_string_pretty(value)?);
    Ok(())
}

pub fn heading(text: &str) {
    println!("\n{}", style(text).bold().underlined());
}

pub fn success(text: &str) {
    println!("{} {text}", style("✓").green().bold());
}

pub fn warn(text: &str) {
    println!("{} {text}", style("!").yellow().bold());
}

/// Progress while work happens: on stderr, so `--json` output on stdout stays
/// one JSON document.
pub fn progress(text: &str) {
    eprintln!("  {}", style(text).dim());
}

pub fn note(text: &str) {
    println!("  {}", style(text).dim());
}

pub fn item(name: &str, detail: &str) {
    println!("  {} {}", style(name).cyan(), style(detail).dim());
}