use std::fmt;
use clap::ValueEnum;
use owo_colors::{OwoColorize, Stream, Style};
#[derive(Copy, Clone, Debug, ValueEnum)]
pub enum ColorChoice {
Auto,
Always,
Never,
}
pub fn apply(choice: ColorChoice) {
match choice {
ColorChoice::Auto => owo_colors::unset_override(),
ColorChoice::Always => owo_colors::set_override(true),
ColorChoice::Never => owo_colors::set_override(false),
}
}
const DIM: Style = Style::new().bright_black();
const BOLD: Style = Style::new().bold();
const ERROR: Style = Style::new().red();
const ERROR_COUNT: Style = Style::new().red().bold();
const WARNING: Style = Style::new().yellow();
const WARNING_COUNT: Style = Style::new().yellow().bold();
const INFO: Style = Style::new().blue();
const BANNER: Style = Style::new().bold().green();
fn paint<T: fmt::Display + ?Sized>(stream: Stream, value: &T, style: Style) -> String {
value
.if_supports_color(stream, |t| t.style(style))
.to_string()
}
pub fn dim<T: fmt::Display + ?Sized>(value: &T) -> String {
paint(Stream::Stdout, value, DIM)
}
pub fn bold<T: fmt::Display + ?Sized>(value: &T) -> String {
paint(Stream::Stdout, value, BOLD)
}
pub fn error_label(text: &str) -> String {
paint(Stream::Stdout, text, ERROR)
}
pub fn warning_label(text: &str) -> String {
paint(Stream::Stdout, text, WARNING)
}
pub fn info_label(text: &str) -> String {
paint(Stream::Stdout, text, INFO)
}
pub fn banner() -> String {
paint(Stream::Stderr, "mir", BANNER)
}
pub fn error_count(n: usize) -> String {
paint(Stream::Stderr, &n.to_string(), ERROR_COUNT)
}
pub fn error_word() -> String {
paint(Stream::Stderr, "errors", ERROR)
}
pub fn warning_count(n: usize) -> String {
paint(Stream::Stderr, &n.to_string(), WARNING_COUNT)
}
pub fn warning_word() -> String {
paint(Stream::Stderr, "warnings", WARNING)
}