use crate::verdict::Severity;
use owo_colors::OwoColorize;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Stream {
Stdout,
Stderr,
}
pub fn use_color_for(stream: Stream) -> bool {
if std::env::var_os("NO_COLOR").is_some() {
return false;
}
match stream {
Stream::Stderr => is_terminal::is_terminal(std::io::stderr()),
Stream::Stdout => is_terminal::is_terminal(std::io::stdout()),
}
}
pub fn severity_label(severity: &Severity, stream: Stream) -> String {
let label = format!("[{}]", severity);
if !use_color_for(stream) {
return label;
}
match severity {
Severity::Critical => label.bright_red().to_string(),
Severity::High => label.red().to_string(),
Severity::Medium => label.yellow().to_string(),
Severity::Low => label.cyan().to_string(),
Severity::Info => label.dimmed().to_string(),
}
}
pub fn bold(text: &str, stream: Stream) -> String {
if use_color_for(stream) {
text.bold().to_string()
} else {
text.to_string()
}
}
pub fn bold_red(text: &str, stream: Stream) -> String {
if use_color_for(stream) {
text.bold().red().to_string()
} else {
text.to_string()
}
}
pub fn green(text: &str, stream: Stream) -> String {
if use_color_for(stream) {
text.green().to_string()
} else {
text.to_string()
}
}
pub fn red(text: &str, stream: Stream) -> String {
if use_color_for(stream) {
text.red().to_string()
} else {
text.to_string()
}
}
pub fn yellow(text: &str, stream: Stream) -> String {
if use_color_for(stream) {
text.yellow().to_string()
} else {
text.to_string()
}
}
pub fn dim(text: &str, stream: Stream) -> String {
if use_color_for(stream) {
text.dimmed().to_string()
} else {
text.to_string()
}
}
pub fn pass_mark(stream: Stream) -> String {
if use_color_for(stream) {
"\u{2713}".green().to_string()
} else {
"[ok]".to_string()
}
}
pub fn fail_mark(stream: Stream) -> String {
if use_color_for(stream) {
"\u{2717}".red().to_string()
} else {
"[!!]".to_string()
}
}