use std::sync::atomic::{AtomicBool, Ordering};
use comfy_table::presets::UTF8_FULL;
use comfy_table::{Attribute, Cell, Color as TableColor, ContentArrangement, Table};
use owo_colors::OwoColorize;
use crate::classify::Class;
use crate::heatmap::CellColor;
static QUIET: AtomicBool = AtomicBool::new(false);
pub fn set_quiet(quiet: bool) {
QUIET.store(quiet, Ordering::SeqCst);
}
pub fn is_quiet() -> bool {
QUIET.load(Ordering::SeqCst)
}
fn class_palette(class: Class) -> (TableColor, CellColor, &'static str) {
match class {
Class::Safe => (TableColor::Green, CellColor::Green, "SAFE"),
Class::Zero => (TableColor::Blue, CellColor::Blue, "ZERO"),
Class::Ones => (TableColor::Magenta, CellColor::Magenta, "ONES"),
Class::Changed => (TableColor::Red, CellColor::Red, "CHANGED"),
}
}
pub fn class_color(class: Class) -> CellColor {
class_palette(class).1
}
pub fn section(title: &str) {
if is_quiet() {
return;
}
println!();
println!("{}", title.bold().cyan());
println!("{}", "─".repeat(title.chars().count()).bright_black());
}
pub fn step(msg: &str) {
if is_quiet() {
return;
}
println!("{} {}", "›".bright_black(), msg);
}
pub fn info_kv(key: &str, value: impl std::fmt::Display) {
if is_quiet() {
return;
}
println!(" {} {}", format!("{key}:").dimmed(), value);
}
pub fn make_table() -> Table {
let mut t = Table::new();
t.load_preset(UTF8_FULL)
.set_content_arrangement(ContentArrangement::Dynamic);
t
}
pub fn header_cell(text: &str) -> Cell {
Cell::new(text)
.add_attribute(Attribute::Bold)
.fg(TableColor::White)
}
pub fn class_cell(class: Class) -> Cell {
let (color, _, text) = class_palette(class);
Cell::new(text).fg(color).add_attribute(Attribute::Bold)
}
pub fn class_inline(class: Class) -> String {
match class {
Class::Safe => "SAFE".green().bold().to_string(),
Class::Zero => "ZERO".blue().bold().to_string(),
Class::Ones => "ONES".magenta().bold().to_string(),
Class::Changed => "CHANGED".red().bold().to_string(),
}
}