use std::io::IsTerminal;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ColorConfig {
Auto,
}
impl ColorConfig {
pub fn should_colorize(&self) -> bool {
match self {
ColorConfig::Auto => std::io::stderr().is_terminal(),
}
}
}
pub struct Styles {
enabled: bool,
}
impl Styles {
pub fn new(config: ColorConfig) -> Self {
Self {
enabled: config.should_colorize(),
}
}
pub fn error(&self, text: &str) -> String {
if self.enabled {
format!("\x1b[1;31m{}\x1b[0m", text) } else {
text.to_string()
}
}
pub fn warning(&self, text: &str) -> String {
if self.enabled {
format!("\x1b[1;33m{}\x1b[0m", text) } else {
text.to_string()
}
}
pub fn success(&self, text: &str) -> String {
if self.enabled {
format!("\x1b[1;32m{}\x1b[0m", text) } else {
text.to_string()
}
}
pub fn note(&self, text: &str) -> String {
if self.enabled {
format!("\x1b[1;36m{}\x1b[0m", text) } else {
text.to_string()
}
}
pub fn help(&self, text: &str) -> String {
if self.enabled {
format!("\x1b[1;36m{}\x1b[0m", text) } else {
text.to_string()
}
}
pub fn bold(&self, text: &str) -> String {
if self.enabled {
format!("\x1b[1m{}\x1b[0m", text)
} else {
text.to_string()
}
}
pub fn dim(&self, text: &str) -> String {
if self.enabled {
format!("\x1b[2m{}\x1b[0m", text)
} else {
text.to_string()
}
}
pub fn primary_label(&self, text: &str) -> String {
if self.enabled {
format!("\x1b[31m{}\x1b[0m", text) } else {
text.to_string()
}
}
pub fn secondary_label(&self, text: &str) -> String {
if self.enabled {
format!("\x1b[34m{}\x1b[0m", text) } else {
text.to_string()
}
}
pub fn line_number(&self, text: &str) -> String {
if self.enabled {
format!("\x1b[34;1m{}\x1b[0m", text) } else {
text.to_string()
}
}
}
impl Default for Styles {
fn default() -> Self {
Self::new(ColorConfig::Auto)
}
}