farve/
lib.rs

1extern crate owo_colors;
2
3/// Wraps a string on either side with square brackets.
4/// The brightness of the brackets can be set with the second argument (0-2).
5///
6/// `( "s" , 0 | 1 | 2 | _ ) -> "[s]"`
7///
8/// # Examples
9/// ```
10/// use owo_colors::OwoColorize;
11/// use farve::brackets;
12///
13/// let i0 = brackets!("INFO");
14/// ```
15#[macro_export]
16macro_rules! brackets {
17    ($msg:expr) => {
18        format!("{}{}{}", '[', $msg, ']')
19    };
20
21    ($level:expr, $bracket_brightness:expr) => {
22        match $bracket_brightness {
23            0 => format!(
24                "{}{}{}",
25                '['.black().dimmed().bold(),
26                $level,
27                ']'.black().dimmed().bold()
28            ),
29            1 => format!(
30                "{}{}{}",
31                '['.bright_black().bold(),
32                $level,
33                ']'.bright_black().bold()
34            ),
35            2 => format!("{}{}{}", '['.white().bold(), $level, ']'.white().bold()),
36            _ => format!("{}{}{}", '[', $level, ']'),
37        }
38    };
39}
40
41/// Create a new println function with a prefix and brackets (0-2 brightness).
42///
43/// `level` - any prefix.
44/// `msg` - it should not be wrapped in quotes.
45/// `bracket_brightness` - the brightness of the brackets (default: 0).
46/// # Examples
47/// ```
48/// use owo_colors::OwoColorize;
49/// use farve::prettyln;
50///
51/// prettyln!("info", "The weather is nice today.");
52/// prettyln!("warn".yellow(), "I almost couldn't, but I did it!");
53/// ```
54#[macro_export]
55macro_rules! prettyln {
56    ($level:expr, $msg:expr, $bracket_brightness:expr) => {
57        println!("{} {}", farve::brackets!($level, $bracket_brightness), $msg)
58    };
59    ($level:expr, $msg:expr) => {
60        println!("{} {}", farve::brackets!($level), $msg)
61    };
62    ($level:expr) => {
63        println!("{} {}", farve::brackets!($level))
64    };
65}
66
67/// Create a new eprintln function with a prefix and brackets (0-2 brightness).
68///
69/// The first argument is the name of the function, it should not be wrapped in quotes.
70/// The second argument is the prefix, it should be wrapped in quotes (default: function name).
71/// The third argument is the brightness of the brackets (default: 0).
72#[macro_export]
73macro_rules! eprettyln {
74    ($level:expr, $msg:expr, $bracket_brightness:expr) => {
75        eprintln!("{} {}", farve::brackets!($level, $bracket_brightness), $msg)
76    };
77    ($level:expr, $msg:expr) => {
78        eprintln!("{} {}", farve::brackets!($level), $msg)
79    };
80    ($level:expr) => {
81        eprintln!("{} {}", farve::brackets!($level))
82    };
83}
84
85/// Create a new println function with a prefix and brackets (0-2 brightness).
86///
87/// `$func` - the name of the function, it should not be wrapped in quotes.
88/// `$prefix` - the prefix, it should be wrapped in quotes (default: function name).
89/// `$bracket_brightness` - `0 | 1 | 2`; brightness of the brackets (default: 0).
90///
91/// # Examples
92/// ```
93/// use owo_colors::OwoColorize;
94/// use farve::farve;
95///
96/// farve!(silly, "silly 😋".white().bold(), 2);
97/// farve!(info);
98/// farve!(warn, "warn".yellow().underline());
99///
100///
101/// silly("Hello, world!");
102/// info("The weather is nice today.");
103/// warn("I almost couldn't, but I did it!");
104/// ```
105/// # Output (imagine color)
106/// ```log
107/// [silly 😋] Hello, world!
108/// [info] The weather is nice today.
109/// [warn] I almost couldn't, but I did it!
110/// ```
111#[macro_export]
112macro_rules! farve {
113    ($func:ident, $prefix:expr, $bracket_brightness:expr) => {
114        pub fn $func<S: std::fmt::Display>(msg: S) {
115            farve::prettyln!($prefix, msg, $bracket_brightness)
116        }
117    };
118    ($func:ident, $prefix:expr) => {
119        pub fn $func<S: std::fmt::Display>(msg: S) {
120            farve::prettyln!($prefix, msg)
121        }
122    };
123    ($func:ident) => {
124        pub fn $func<S: std::fmt::Display>(msg: S) {
125            farve::prettyln!(stringify!($func), msg)
126        }
127    };
128}
129/// Just like `farve!`, but prints to stderr instead of stdout. Creates an eprintln with a prefix and brackets (0-2 brightness).
130///
131/// `$func` - the name of the function, it should not be wrapped in quotes.
132/// `$prefix` - the prefix, it should be wrapped in quotes (default: function name).
133/// `$bracket_brightness` - `0 | 1 | 2`; brightness of the brackets (default: 0).
134///
135/// # Examples
136/// ```
137/// use owo_colors::OwoColorize;
138/// use farve::efarve;
139///
140/// efarve!(warn, "WARN".yellow().underline(), 1);
141/// efarve!(skip, "skip".bright_black().italic());
142#[macro_export]
143macro_rules! efarve {
144    ($func:ident, $prefix:expr, $bracket_brightness:expr) => {
145        pub fn $func<S: std::fmt::Display>(msg: S) {
146            farve::eprettyln!($prefix, msg, $bracket_brightness)
147        }
148    };
149    ($func:ident, $prefix:expr) => {
150        pub fn $func<S: std::fmt::Display>(msg: S) {
151            farve::eprettyln!($prefix, msg)
152        }
153    };
154    ($func:ident) => {
155        pub fn $func<S: std::fmt::Display>(msg: S) {
156            farve::eprettyln!(stringify!($func), msg)
157        }
158    };
159}