use std::sync::atomic::{AtomicBool, Ordering};
use owo_colors::{OwoColorize, Style};
static PAINTS: AtomicBool = AtomicBool::new(true);
pub fn silence() {
PAINTS.store(false, Ordering::Relaxed);
}
fn painted(style: Style) -> Style {
chosen(style, PAINTS.load(Ordering::Relaxed))
}
fn chosen(style: Style, paints: bool) -> Style {
if paints {
style
} else {
Style::new()
}
}
pub fn accent() -> Style {
painted(Style::new().magenta())
}
pub fn muted() -> Style {
painted(Style::new().bright_black())
}
pub fn strong() -> Style {
painted(Style::new().bold())
}
pub fn emphasis() -> Style {
painted(Style::new().italic())
}
pub fn success() -> Style {
painted(Style::new().green())
}
pub fn warning() -> Style {
painted(Style::new().yellow())
}
pub fn danger() -> Style {
painted(Style::new().red())
}
pub fn state_style(state: &str) -> Style {
match state {
"active" => success(),
"deploying" => accent(),
"sleeping" => Style::new().cyan(),
"degraded" => warning(),
"crashed" | "failed" => danger(),
_ => muted(),
}
}
pub fn state_word(state: &str) -> &str {
match state {
"draft" | "idle" => "sin desplegar",
"deploying" => "desplegando",
"active" => "en marcha",
"sleeping" => "dormido",
"degraded" => "con problemas",
"crashed" => "ha caído",
"failed" => "ha fallado",
"removing" => "eliminando",
other => other,
}
}
pub fn deployment_status_style(status: &str) -> Style {
match status {
"active" => success(),
"queued" | "building" | "deploying" => accent(),
"failed" | "crashed" => danger(),
_ => muted(),
}
}
pub fn state_dot(state: &str) -> String {
format!("{}", "●".style(state_style(state)))
}
pub const CHECK: &str = "✔";
pub const CROSS: &str = "✖";
pub const SKIP: &str = "–";
pub const PENDING: &str = "○";
pub const ARROW: &str = "→";
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_should_translate_a_running_service() {
assert_eq!(state_word("active"), "en marcha");
}
#[test]
fn it_should_pass_an_unknown_state_through_unchanged() {
assert_eq!(state_word("something-new"), "something-new");
}
fn painted_with(paints: bool) -> String {
format!("{}", "x".style(chosen(Style::new().red(), paints)))
}
#[test]
fn it_should_paint_when_colour_is_on() {
assert_ne!(painted_with(true), "x");
}
#[test]
fn it_should_leave_the_text_bare_when_colour_is_off() {
assert_eq!(painted_with(false), "x");
}
}