tiger_lib/report/
output_style.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
use std::collections::HashMap;

use ansiterm::Colour::{Black, Blue, Cyan, Green, Purple, Red, White, Yellow};
use ansiterm::Style;

use crate::report::Severity;

/// For looking up the style to use for the various parts of the output.
#[derive(Debug, Default, Clone, Copy, Hash, PartialEq, Eq)]
pub enum Styled {
    #[default]
    Default,
    Tag(Severity, IsTag),
    /// The actual error message, telling the user what is wrong.
    ErrorMessage,
    /// Introduces additional info on a report.
    InfoTag,
    /// The actual info message. Optionally attached to a report.
    Info,
    /// Filename, line number, column number.
    Location,
    /// The caret, pointing at the exact location of the error.
    /// TODO: Maybe this should depend on error level.
    Caret,
    /// Text from the source file.
    /// By default, this should probably be unstyled,
    /// but we might want to add the option to configure it.
    SourceText,
    /// We could potentially give emphasis to certain parts of an error message.
    /// For example; in the following message, the word limit could perhaps be italicized.
    /// "required field `limit` missing"
    Emphasis,
}

/// Whether the style applies to the `ErrorLevel` tag itself or the `ErrorKey` that follows it.
pub type IsTag = bool;

#[derive(Debug)]
pub struct OutputStyle {
    map: HashMap<Styled, Style>,
}

impl Default for OutputStyle {
    /// Constructs an instance of `OutputStyles` that uses default, hard-coded color values.
    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 {
    /// Construct a version of the `OutputStyles` that always returns the default, no-colour style.
    /// Use this to effectively disable any ANSI characters in the output.
    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.")
    }
    /// Allows overriding a color for a given `ErrorLevel`.
    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.");
        }
    }
}