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
use crate::logger::Importance;
use chrono::offset::Utc;
use colored::{ColoredString, Colorize};

/// Trait used to style the logs
pub trait Style {
    fn format(&self, imp: Importance, msg: &str) -> String;
}

fn with_color(imp: Importance, msg: &str) -> ColoredString {
    match imp {
        Importance::Fail => msg.red(),
        Importance::Warn => msg.yellow(),
        Importance::Debug => msg.blue(),
        Importance::Success => msg.green(),
    }
}

/// Default style used for the logs
/// E.g:
/// [Error]: This is an error message
pub struct DefaultStyle {
    date: bool,
    colored: bool,
}

impl DefaultStyle {
    pub fn date(self, date: bool) -> Self {
        DefaultStyle { date, ..self }
    }

    pub fn colored(self, colored: bool) -> Self {
        DefaultStyle { colored, ..self }
    }
}

impl Default for DefaultStyle {
    fn default() -> Self {
        DefaultStyle {
            date: false,
            colored: true,
        }
    }
}

impl Style for DefaultStyle {
    fn format(&self, imp: Importance, msg: &str) -> String {
        let log = if self.date {
            let today = Utc::today();
            format!("{} [{}]: {}", imp, today, msg)
        } else {
            format!("{}: {}", imp, msg)
        };

        if self.colored {
            with_color(imp, &log).to_string()
        } else {
            log
        }
    }
}

/// Simple and minimalist style.
/// E.g:
/// ▶ This is an error messsage.
pub struct Arrow {
    colored: bool,
    padding: usize,
}

impl Default for Arrow {
    fn default() -> Self {
        Arrow {
            colored: true,
            padding: 5,
        }
    }
}

impl Arrow {
    pub fn colored(self, colored: bool) -> Self {
        Arrow { colored, ..self }
    }

    pub fn padding(self, padding: usize) -> Self {
        Arrow { padding, ..self }
    }
}

impl Style for Arrow {
    fn format(&self, imp: Importance, msg: &str) -> String {
        let log = format!("{:width$}▶ {}", "", msg, width = self.padding);
        if self.colored {
            with_color(imp, &log).to_string()
        } else {
            log
        }
    }
}