cprint/ceprint/
mod.rs

1/// Print an error message like Cargo does. If you pass only one string, the first word is the title and the rest is the message. If you pass two strings the first one is the title and the second one is the message.
2/// You can specify the color of the title with a predefined color from the [`colored::Color`] enum or with RGB values `(r, g, b)`. To specify the color use `=>` after the strings.
3/// The end of the title is padded with spaces to make it 12 characters long.
4/// # Examples
5/// ```rust
6/// use cprint::{ceprint, Color, Coloration};
7///
8/// ceprint!("Failed to compile");
9/// ```
10#[macro_export]
11macro_rules! ceprint {
12    ($title:expr, $msg:expr => $color:ident) => {{
13        $crate::Color::$color;
14        eprint!("{}", $crate::cformat!($title, $msg => $color))
15    }};
16
17    ($title:expr, $msg:expr => ($r:expr, $g:expr, $b:expr)) => {{
18        $crate::Color::TrueColor { r: $r, g: $g, b: $b };
19        eprint!("{}", $crate::cformat!($title, $msg => ($r, $g, $b)))
20    }};
21
22    ($title:expr, $msg:expr) => {{
23        eprint!("{}", $crate::cformat!($title, $msg => Red))
24    }};
25
26    ($msg:expr => $color:ident) => {{
27        $crate::Color::$color;
28        eprint!("{}",$crate::cformat!($msg => $color))
29    }};
30
31    ($msg:expr => ($r:expr, $g:expr, $b:expr)) => {{
32        $crate::Color::TrueColor { r: $r, g: $g, b: $b };
33        eprint!("{}",$crate::cformat!($msg => ($r, $g, $b)))
34    }};
35
36    ($msg:expr) => {{
37        eprint!("{}",$crate::cformat!($msg => Red))
38    }};
39}
40
41/// Same as [`ceprint!`] but with a newline at the end.
42#[macro_export]
43macro_rules! ceprintln {
44    ($title:expr, $msg:expr => $color:ident) => {{
45        $crate::Color::$color;
46        eprintln!("{}", $crate::cformat!($title, $msg => $color))
47    }};
48
49    ($title:expr, $msg:expr => ($r:expr, $g:expr, $b:expr)) => {{
50        $crate::Color::TrueColor { r: $r, g: $g, b: $b };
51        eprintln!("{}", $crate::cformat!($title, $msg => ($r, $g, $b)))
52    }};
53
54    ($title:expr, $msg:expr) => {{
55        eprintln!("{}", $crate::cformat!($title, $msg => Red))
56    }};
57
58    ($msg:expr => $color:ident) => {{
59        $crate::Color::$color;
60        eprintln!("{}", $crate::cformat!($msg => $color))
61    }};
62
63    ($msg:expr => ($r:expr, $g:expr, $b:expr)) => {{
64        $crate::Color::TrueColor { r: $r, g: $g, b: $b };
65        eprintln!("{}", $crate::cformat!($msg => ($r, $g, $b)))
66    }};
67
68    ($msg:expr) => {{
69        eprintln!("{}", $crate::cformat!($msg => Red))
70    }};
71}
72
73#[cfg(test)]
74mod tests {
75    #[test]
76    fn ceprint_title_message_color() {
77        ceprint!("Failed", "to compile" => Red);
78    }
79
80    #[test]
81    fn ceprint_title_message_rgb() {
82        ceprint!("Failed", "to compile main.rs" => (255, 0, 0));
83    }
84
85    #[test]
86    fn ceprint_title_message() {
87        ceprint!("Failed", "to compile main.rs");
88    }
89
90    #[test]
91    fn ceprint_message_color() {
92        ceprint!("Failed to compile" => Red);
93    }
94
95    #[test]
96    fn ceprint_message_rgb() {
97        ceprint!("Failed to compile" => (255, 0, 0));
98    }
99
100    #[test]
101    fn ceprint_message() {
102        ceprint!("Failed to compile");
103    }
104}