use atty::Stream;
use console::{Style, Term};
#[derive(Clone)]
pub struct CliStyle {
pub color: bool,
pub unicode: bool,
}
impl CliStyle {
pub fn detect() -> Self {
let term = Term::stdout();
let is_tty = atty::is(Stream::Stdout);
let color = is_tty && term.features().colors_supported();
let unicode = is_tty && cfg!(not(windows)) && std::env::var_os("NO_UNICODE").is_none();
Self { color, unicode }
}
}
#[derive(Clone)]
pub struct Symbols {
pub ok: &'static str,
pub warn: &'static str,
pub err: &'static str,
pub arrow: &'static str,
pub bullet: &'static str,
}
impl Symbols {
pub fn for_term(style: &CliStyle) -> Self {
if style.unicode {
Self {
ok: "✔",
warn: "⚠",
err: "✖",
arrow: "→",
bullet: "•",
}
} else {
Self {
ok: "[OK]",
warn: "[!]",
err: "[X]",
arrow: ">",
bullet: "*",
}
}
}
}
pub struct Styles {
pub ok: Style,
pub warn: Style,
pub err: Style,
pub dim: Style,
}
impl Styles {
pub fn for_term(style: &CliStyle) -> Self {
if style.color {
Self {
ok: Style::new().green().bold(),
warn: Style::new().yellow().bold(),
err: Style::new().red().bold(),
dim: Style::new().dim(),
}
} else {
Self {
ok: Style::new(),
warn: Style::new(),
err: Style::new(),
dim: Style::new(),
}
}
}
}