mom_task/
print_utils.rs

1#[cfg(test)]
2#[path = "print_utils_test.rs"]
3mod print_utils_test;
4
5use colored::{Color, ColoredString, Colorize};
6
7const PREFIX: &str = "[mom]";
8pub(crate) const INFO_COLOR: Color = Color::BrightBlue;
9pub(crate) const WARN_COLOR: Color = Color::BrightYellow;
10pub(crate) const ERROR_COLOR: Color = Color::BrightRed;
11
12pub trait MomOutput {
13    /// Just adds the `[mom]` prefix to the given string, with no color.
14    fn mom_just_prefix(&self) -> String;
15    /// Returns the given string with the `[mom]` prefix in each line. The prefix will also take the given color.
16    fn mom_prefix<S: Into<Color> + Clone>(&self, color: S) -> String;
17    /// Adds the `[mom]` prefix to the given string. The whole string will have the given color.
18    fn mom_colorize<S: Into<Color> + Clone>(&self, color: S) -> String;
19    /// Returns the given string with the `[mom]` prefix in each line. The whole string will be blue.
20    fn mom_info(&self) -> String;
21    /// Returns the given string with the `[mom]` prefix in each line. The prefix will be blue.
22    fn mom_prefix_info(&self) -> String;
23    /// Returns the given string with the `[mom]` prefix in each line. The whole string will be yellow.
24    fn mom_warn(&self) -> String;
25    /// Returns the given string with the `[mom]` prefix in each line. The prefix will be yellow.
26    fn mom_prefix_warn(&self) -> String;
27    /// Returns the given string with the `[mom]` prefix in each line. The whole string will be red.
28    fn mom_error(&self) -> String;
29    /// Returns the given string with the `[mom]` prefix in each line. The prefix will be red.
30    fn mom_prefix_error(&self) -> String;
31}
32
33impl MomOutput for str {
34    fn mom_just_prefix(&self) -> String {
35        let lines = self.split_inclusive('\n');
36
37        let mut result = String::new();
38        for line in lines {
39            result.push_str(PREFIX);
40            result.push(' ');
41            result.push_str(line);
42        }
43        result
44    }
45    fn mom_prefix<S: Into<Color> + Clone>(&self, color: S) -> String {
46        let lines = self.split_inclusive('\n');
47        let prefix = PREFIX.color(color).to_string();
48
49        let mut result = String::new();
50        for line in lines {
51            result.push_str(&prefix);
52            result.push(' ');
53            result.push_str(line);
54        }
55        result
56    }
57
58    fn mom_colorize<S: Into<Color> + Clone>(&self, color: S) -> String {
59        let lines = self.split_inclusive('\n');
60
61        let mut result = String::new();
62        for line in lines {
63            result.push_str(PREFIX);
64            result.push(' ');
65            result.push_str(line);
66        }
67        result.color(color).to_string()
68    }
69
70    fn mom_info(&self) -> String {
71        self.mom_colorize(INFO_COLOR)
72    }
73
74    fn mom_prefix_info(&self) -> String {
75        self.mom_prefix(INFO_COLOR)
76    }
77
78    fn mom_warn(&self) -> String {
79        self.mom_colorize(WARN_COLOR)
80    }
81
82    fn mom_prefix_warn(&self) -> String {
83        self.mom_prefix(WARN_COLOR)
84    }
85
86    fn mom_error(&self) -> String {
87        self.mom_colorize(ERROR_COLOR)
88    }
89
90    fn mom_prefix_error(&self) -> String {
91        self.mom_prefix(ERROR_COLOR)
92    }
93}
94
95// Calling the function in a ColoredString instance removes the color from it,
96// so we need to transform it to a string first to keep it.
97impl MomOutput for ColoredString {
98    fn mom_just_prefix(&self) -> String {
99        self.to_string().mom_just_prefix()
100    }
101
102    fn mom_prefix<S: Into<Color> + Clone>(&self, color: S) -> String {
103        self.to_string().mom_prefix(color)
104    }
105
106    fn mom_colorize<S: Into<Color> + Clone>(&self, color: S) -> String {
107        self.to_string().mom_colorize(color)
108    }
109
110    fn mom_info(&self) -> String {
111        self.to_string().mom_info()
112    }
113
114    fn mom_prefix_info(&self) -> String {
115        self.to_string().mom_prefix_info()
116    }
117
118    fn mom_warn(&self) -> String {
119        self.to_string().mom_warn()
120    }
121
122    fn mom_prefix_warn(&self) -> String {
123        self.to_string().mom_prefix_warn()
124    }
125
126    fn mom_error(&self) -> String {
127        self.to_string().mom_error()
128    }
129
130    fn mom_prefix_error(&self) -> String {
131        self.to_string().mom_prefix_error()
132    }
133}