hw_msg/
lib.rs

1#![cfg_attr(docsrs, feature(doc_cfg))]
2//! This crate provides macros for the style of CLI messages I like to use in my programs.
3//!
4//! The base macros are:
5//! - [`info`] - Print an info-styled message
6//! - [`warning`] - Print a warning-styled message
7//! - [`error`] - Print an error-styled message
8//! - [`question`] - Print a question-styled message
9//! - [`debug`] - Print a debug-styled message
10//!
11//! These macros will print the message **without** a trailing newline, like how [`print`] does. Use the `*ln` variants of each of these macros to get [`println`]-like functionality (i.e. [`infoln`]).
12//!
13//! The `*_fmt` variants work like the base macros, but return the string instead of printing it.
14use colored::{ColoredString, Colorize};
15
16#[cfg(feature = "logging")]
17mod logging;
18#[cfg_attr(docsrs, doc(cfg(feature = "logging")))]
19#[cfg(feature = "logging")]
20pub use logging::HwLogger;
21
22#[doc(hidden)]
23pub fn get_info<T: ToString>(string: T) -> ColoredString {
24    string.to_string().cyan().bold()
25}
26
27#[doc(hidden)]
28pub fn get_warning<T: ToString>(string: T) -> ColoredString {
29    string.to_string().yellow().bold()
30}
31
32#[doc(hidden)]
33pub fn get_error<T: ToString>(string: T) -> ColoredString {
34    string.to_string().red().bold()
35}
36
37#[doc(hidden)]
38pub fn get_question<T: ToString>(string: T) -> ColoredString {
39    string.to_string().magenta().bold()
40}
41
42#[doc(hidden)]
43pub fn get_debug<T: ToString>(string: T) -> ColoredString {
44    string.to_string().green().bold()
45}
46
47#[macro_export]
48macro_rules! info_fmt {
49    ($($arg:tt)*) => {{
50        format!("{} ", $crate::get_info("Info:")) + &format!($($arg)*)
51    }}
52}
53
54#[macro_export]
55macro_rules! warning_fmt {
56    ($($arg:tt)*) => {{
57        format!("{} ", $crate::get_warning("Warning:")) + &format!($($arg)*)
58    }}
59}
60
61#[macro_export]
62macro_rules! error_fmt {
63    ($($arg:tt)*) => {{
64        format!("{} ", $crate::get_error("Error:")) + &format!($($arg)*)
65    }}
66}
67
68#[macro_export]
69macro_rules! question_fmt {
70    ($($arg:tt)*) => {{
71        format!("{} ", $crate::get_question("Question:")) + &format!($($arg)*)
72    }}
73}
74
75#[macro_export]
76macro_rules! debug_fmt {
77    ($($arg:tt)*) => {{
78        format!("{} ", $crate::get_debug("Debug:")) + &format!($($arg)*)
79    }}
80}
81
82#[macro_export]
83macro_rules! info {
84    ($($arg:tt)*) => {{
85        print!("{}", $crate::info_fmt!($($arg)*))
86    }}
87}
88
89#[macro_export]
90macro_rules! infoln {
91    ($($arg:tt)*) => {{
92        $crate::info!($($arg)*);
93        println!()
94    }}
95}
96
97#[macro_export]
98macro_rules! warning {
99    ($($arg:tt)*) => {{
100        print!("{}", $crate::warning_fmt!($($arg)*))
101    }}
102}
103
104#[macro_export]
105macro_rules! warningln {
106    ($($arg:tt)*) => {{
107        $crate::warning!($($arg)*);
108        println!()
109    }}
110}
111#[macro_export]
112macro_rules! error {
113    ($($arg:tt)*) => {{
114        print!("{}", $crate::error_fmt!($($arg)*))
115    }}
116}
117
118#[macro_export]
119macro_rules! errorln {
120    ($($arg:tt)*) => {{
121        $crate::error!($($arg)*);
122        println!()
123    }}
124}
125#[macro_export]
126macro_rules! debug {
127    ($($arg:tt)*) => {{
128        print!("{}", $crate::debug_fmt!($($arg)*))
129    }}
130}
131
132#[macro_export]
133macro_rules! debugln {
134    ($($arg:tt)*) => {{
135        $crate::debug!($($arg)*);
136        println!()
137    }}
138}
139
140#[macro_export]
141macro_rules! question {
142    ($($arg:tt)*) => {{
143        print!("{}", $crate::question_fmt!($($arg)*))
144    }}
145}