use owo_colors::{OwoColorize, Stream, Style};
use serde_json::Value;
#[derive(Debug, Clone, Copy)]
pub struct Out {
pub json: bool,
}
impl Out {
pub fn new(json: bool) -> Self {
Out { json }
}
pub fn json_value(&self, value: &Value) {
println!(
"{}",
serde_json::to_string_pretty(value).expect("serializable")
);
}
pub fn ok(&self, human: impl AsRef<str>, value: Value) {
if self.json {
self.json_value(&value);
} else {
println!(
"{} {}",
"✓".if_supports_color(Stream::Stdout, |t| t.green()),
human.as_ref()
);
}
}
pub fn line(&self, text: impl AsRef<str>) {
if !self.json {
println!("{}", text.as_ref());
}
}
}
pub fn id_style(id: &str) -> String {
id.if_supports_color(Stream::Stdout, |t| t.bold().to_string())
.to_string()
}
pub fn dim(text: &str) -> String {
text.if_supports_color(Stream::Stdout, |t| t.dimmed().to_string())
.to_string()
}
pub fn emit_error(json: bool, msg: &str) {
if json {
let v = serde_json::json!({ "ok": false, "error": msg });
println!(
"{}",
serde_json::to_string_pretty(&v).expect("serializable")
);
} else {
let tag =
"error:".if_supports_color(Stream::Stderr, |t| t.style(Style::new().red().bold()));
eprintln!("{tag} {msg}");
}
}