tiger_lib/report/
output_style.rs1use std::collections::HashMap;
2
3use ansiterm::Colour::{Black, Blue, Cyan, Green, Purple, Red, White, Yellow};
4use ansiterm::Style;
5
6use crate::report::Severity;
7
8#[derive(Debug, Default, Clone, Copy, Hash, PartialEq, Eq)]
10pub enum Styled {
11 #[default]
12 Default,
13 Tag(Severity, IsTag),
14 ErrorMessage,
16 InfoTag,
18 Info,
20 Location,
22 Caret,
25 SourceText,
29 Emphasis,
33}
34
35pub type IsTag = bool;
37
38#[derive(Debug)]
39pub struct OutputStyle {
40 map: HashMap<Styled, Style>,
41}
42
43impl Default for OutputStyle {
44 fn default() -> Self {
46 let mut map = HashMap::new();
47 map.insert(Styled::Default, Style::new());
48
49 map.insert(Styled::InfoTag, Style::new().bold());
50 map.insert(Styled::Info, Style::new());
51 map.insert(Styled::ErrorMessage, Style::new().bold());
52 map.insert(Styled::Location, Blue.bold());
53 map.insert(Styled::Caret, Style::new().bold());
54 map.insert(Styled::SourceText, Style::new());
55 map.insert(Styled::Emphasis, Style::new().italic());
56
57 map.insert(Styled::Tag(Severity::Fatal, true), White.bold());
58 map.insert(Styled::Tag(Severity::Fatal, false), White.bold());
59 map.insert(Styled::Tag(Severity::Error, true), Red.bold());
60 map.insert(Styled::Tag(Severity::Error, false), Red.bold());
61 map.insert(Styled::Tag(Severity::Warning, true), Yellow.bold());
62 map.insert(Styled::Tag(Severity::Warning, false), Yellow.normal());
63 map.insert(Styled::Tag(Severity::Untidy, true), Cyan.bold());
64 map.insert(Styled::Tag(Severity::Untidy, false), Cyan.normal());
65 map.insert(Styled::Tag(Severity::Tips, true), Green.bold());
66 map.insert(Styled::Tag(Severity::Tips, false), Green.normal());
67
68 OutputStyle { map }
69 }
70}
71
72impl OutputStyle {
73 pub fn no_color() -> Self {
76 let mut map = HashMap::new();
77 map.insert(Styled::Default, Style::new());
78 OutputStyle { map }
79 }
80 pub fn style(&self, output: Styled) -> &Style {
81 self.map
82 .get(&output)
83 .or_else(|| self.map.get(&Styled::Default))
84 .expect("Failed to retrieve output style.")
85 }
86 pub fn set(&mut self, severity: Severity, color_str: &str) {
88 if let Some(color) = match color_str.to_ascii_lowercase().as_str() {
89 "black" => Some(Black),
90 "red" => Some(Red),
91 "green" => Some(Green),
92 "yellow" => Some(Yellow),
93 "blue" => Some(Blue),
94 "purple" => Some(Purple),
95 "cyan" => Some(Cyan),
96 "white" => Some(White),
97 _ => None,
98 } {
99 self.map.insert(Styled::Tag(severity, true), color.bold());
100 self.map.insert(Styled::Tag(severity, false), color.normal());
101 } else {
102 eprintln!(
103 "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."
104 );
105 }
106 }
107}