tiger_lib/report/
output_style.rsuse std::collections::HashMap;
use ansiterm::Colour::{Black, Blue, Cyan, Green, Purple, Red, White, Yellow};
use ansiterm::Style;
use crate::report::Severity;
#[derive(Debug, Default, Clone, Copy, Hash, PartialEq, Eq)]
pub enum Styled {
#[default]
Default,
Tag(Severity, IsTag),
ErrorMessage,
InfoTag,
Info,
Location,
Caret,
SourceText,
Emphasis,
}
pub type IsTag = bool;
#[derive(Debug)]
pub struct OutputStyle {
map: HashMap<Styled, Style>,
}
impl Default for OutputStyle {
fn default() -> Self {
let mut map = HashMap::new();
map.insert(Styled::Default, Style::new());
map.insert(Styled::InfoTag, Style::new().bold());
map.insert(Styled::Info, Style::new());
map.insert(Styled::ErrorMessage, Style::new().bold());
map.insert(Styled::Location, Blue.bold());
map.insert(Styled::Caret, Style::new().bold());
map.insert(Styled::SourceText, Style::new());
map.insert(Styled::Emphasis, Style::new().italic());
map.insert(Styled::Tag(Severity::Fatal, true), White.bold());
map.insert(Styled::Tag(Severity::Fatal, false), White.bold());
map.insert(Styled::Tag(Severity::Error, true), Red.bold());
map.insert(Styled::Tag(Severity::Error, false), Red.bold());
map.insert(Styled::Tag(Severity::Warning, true), Yellow.bold());
map.insert(Styled::Tag(Severity::Warning, false), Yellow.normal());
map.insert(Styled::Tag(Severity::Untidy, true), Cyan.bold());
map.insert(Styled::Tag(Severity::Untidy, false), Cyan.normal());
map.insert(Styled::Tag(Severity::Tips, true), Green.bold());
map.insert(Styled::Tag(Severity::Tips, false), Green.normal());
OutputStyle { map }
}
}
impl OutputStyle {
pub fn no_color() -> Self {
let mut map = HashMap::new();
map.insert(Styled::Default, Style::new());
OutputStyle { map }
}
pub fn style(&self, output: Styled) -> &Style {
self.map
.get(&output)
.or_else(|| self.map.get(&Styled::Default))
.expect("Failed to retrieve output style.")
}
pub fn set(&mut self, severity: Severity, color_str: &str) {
if let Some(color) = match color_str.to_ascii_lowercase().as_str() {
"black" => Some(Black),
"red" => Some(Red),
"green" => Some(Green),
"yellow" => Some(Yellow),
"blue" => Some(Blue),
"purple" => Some(Purple),
"cyan" => Some(Cyan),
"white" => Some(White),
_ => None,
} {
self.map.insert(Styled::Tag(severity, true), color.bold());
self.map.insert(Styled::Tag(severity, false), color.normal());
} else {
eprintln!("Tried to set ErrorLevel::{severity} to color {color_str}, but that color was not recognised! Defaulting to regular color instead.\nSupported colors are Black, Red, Green, Yellow, Blue, Purple, Cyan, White.");
}
}
}