1use crate::logger::Importance;
2use chrono::offset::Utc;
3use colored::{ColoredString, Colorize};
4
5pub trait Style {
7 fn format(&self, imp: Importance, msg: &str) -> String;
8}
9
10fn with_color(imp: Importance, msg: &str) -> ColoredString {
11 match imp {
12 Importance::Fail => msg.red(),
13 Importance::Warn => msg.yellow(),
14 Importance::Debug => msg.blue(),
15 Importance::Success => msg.green(),
16 }
17}
18
19pub struct DefaultStyle {
23 date: bool,
24 colored: bool,
25}
26
27impl DefaultStyle {
28 pub fn date(self, date: bool) -> Self {
29 DefaultStyle { date, ..self }
30 }
31
32 pub fn colored(self, colored: bool) -> Self {
33 DefaultStyle { colored, ..self }
34 }
35}
36
37impl Default for DefaultStyle {
38 fn default() -> Self {
39 DefaultStyle {
40 date: false,
41 colored: true,
42 }
43 }
44}
45
46impl Style for DefaultStyle {
47 fn format(&self, imp: Importance, msg: &str) -> String {
48 let log = if self.date {
49 let today = Utc::today();
50 format!("{} [{}]: {}", imp, today, msg)
51 } else {
52 format!("{}: {}", imp, msg)
53 };
54
55 if self.colored {
56 with_color(imp, &log).to_string()
57 } else {
58 log
59 }
60 }
61}
62
63pub struct Arrow {
67 colored: bool,
68 padding: usize,
69}
70
71impl Default for Arrow {
72 fn default() -> Self {
73 Arrow {
74 colored: true,
75 padding: 5,
76 }
77 }
78}
79
80impl Arrow {
81 pub fn colored(self, colored: bool) -> Self {
82 Arrow { colored, ..self }
83 }
84
85 pub fn padding(self, padding: usize) -> Self {
86 Arrow { padding, ..self }
87 }
88}
89
90impl Style for Arrow {
91 fn format(&self, imp: Importance, msg: &str) -> String {
92 let log = format!("{:width$}▶ {}", "", msg, width = self.padding);
93 if self.colored {
94 with_color(imp, &log).to_string()
95 } else {
96 log
97 }
98 }
99}