use std::fmt::Display;
use std::io::IsTerminal;
use anyhow::Result;
use comfy_table::{Cell, Table, presets::UTF8_FULL};
use serde::Serialize;
pub fn use_color() -> bool {
std::io::stderr().is_terminal() && std::env::var("NO_COLOR").is_err()
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum OutputFormat {
Table,
Json,
}
impl OutputFormat {
pub fn from_json_flag(json: bool) -> Self {
if json { Self::Json } else { Self::Table }
}
}
pub fn print_json<T: Serialize>(value: &T) -> Result<()> {
println!("{}", serde_json::to_string_pretty(value)?);
Ok(())
}
pub fn print_table(headers: &[&str], rows: &[Vec<String>]) {
let mut table = Table::new();
table.load_preset(UTF8_FULL);
table.set_header(headers.iter().map(|h| Cell::new(*h)).collect::<Vec<_>>());
for row in rows {
table.add_row(row.iter().map(Cell::new).collect::<Vec<_>>());
}
println!("{table}");
}
pub fn print_list<T: Display>(items: &[T]) {
for item in items {
println!("{item}");
}
}