1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
|
use colored::Colorize;
/// Formats a message in cargo-style format with a bold green prefix.
///
/// The message should be in the format `prefix: content`. The prefix will be
/// bold green and right-padded to 12 characters. If there is no colon in the
/// string, the entire string is printed as content with an empty prefix.
///
/// # Macros
///
/// - `format_cargo!("prefix: {}", arg)` — format-style invocation
/// - `format_cargo!(expr)` — direct expression invocation
///
/// # Panics
///
/// Panics if the prefix (text before the first `:`) exceeds 12 characters.
///
/// # Examples
///
/// ```ignore
/// format_cargo!("Compiling: hello.rs");
/// // Output: " Compiling hello.rs" (green bold "Compiling" padded to 12)
/// ```
#[macro_export]
macro_rules! format_cargo {
($fmt:literal, $($arg:tt)*) => {
$crate::get_cargo_info_format(format!($fmt, $($arg)*))
};
($cmd:expr) => {
$crate::get_cargo_info_format($cmd)
};
}
/// Formats an error message in cargo-style format with a bold red "error" prefix.
///
/// # Macros
///
/// - `eformat_cargo!("prefix: {}", arg)` — format-style invocation
/// - `eformat_cargo!(expr)` — direct expression invocation
///
/// # Examples
///
/// ```ignore
/// eformat_cargo!("failed to parse input");
/// // Output: "error: failed to parse input" (red bold "error")
/// ```
#[macro_export]
macro_rules! eformat_cargo {
($fmt:literal, $($arg:tt)*) => {
$crate::get_cargo_error_format(format!($fmt, $($arg)*))
};
($cmd:expr) => {
$crate::get_cargo_error_format($cmd)
};
}
/// Formats a help message in cargo-style format with a bright white "help" prefix.
///
/// # Macros
///
/// - `hformat_cargo!("prefix: {}", arg)` — format-style invocation
/// - `hformat_cargo!(expr)` — direct expression invocation
///
/// # Examples
///
/// ```ignore
/// hformat_cargo!("use --verbose for more info");
/// // Output: "help: use --verbose for more info" (bright white "help")
/// ```
#[macro_export]
macro_rules! hformat_cargo {
($fmt:literal, $($arg:tt)*) => {
$crate::get_cargo_help_format(format!($fmt, $($arg)*))
};
($cmd:expr) => {
$crate::get_cargo_help_format($cmd)
};
}
/// Print a message in cargo-style format with a bold green prefix.
///
/// # Macros
///
/// - `println_cargo!("prefix: {}", arg)` — format-style invocation
/// - `println_cargo!(expr)` — direct expression invocation
///
/// # Examples
///
/// ```ignore
/// println_cargo!("Compiling: hello.rs");
/// ```
#[macro_export]
macro_rules! println_cargo {
($fmt:literal, $($arg:tt)*) => {
println!("{}", $crate::get_cargo_info_format(format!($fmt, $($arg)*)))
};
($cmd:expr) => {
println!("{}", $crate::get_cargo_info_format($cmd))
};
}
/// Print an error message in cargo-style format with a bold red "error" prefix.
///
/// # Macros
///
/// - `eprintln_cargo!("prefix: {}", arg)` — format-style invocation
/// - `eprintln_cargo!(expr)` — direct expression invocation
///
/// # Examples
///
/// ```ignore
/// eprintln_cargo!("failed to parse input");
/// ```
#[macro_export]
macro_rules! eprintln_cargo {
($fmt:literal, $($arg:tt)*) => {
eprintln!("{}", $crate::get_cargo_error_format(format!($fmt, $($arg)*)))
};
($cmd:expr) => {
eprintln!("{}", $crate::get_cargo_error_format($cmd))
};
}
/// Print a help message in cargo-style format with a bright white "help" prefix.
///
/// # Macros
///
/// - `hprintln_cargo!("prefix: {}", arg)` — format-style invocation
/// - `hprintln_cargo!(expr)` — direct expression invocation
///
/// # Examples
///
/// ```ignore
/// hprintln_cargo!("use --verbose for more info");
/// ```
#[macro_export]
macro_rules! hprintln_cargo {
($fmt:literal, $($arg:tt)*) => {
println!("{}", $crate::get_cargo_help_format(format!($fmt, $($arg)*)))
};
($cmd:expr) => {
println!("{}", $crate::get_cargo_help_format($cmd))
};
}
/// Format a message in cargo style format, with bold green prefix.
///
/// The input string is split at the first `:`. The part before the colon becomes
/// the prefix (bold green, right-padded to 12 characters), and the part after
/// becomes the content.
///
/// If no colon is found, the entire string is treated as content and no prefix
/// is shown.
///
/// # Panics
///
/// Panics if the prefix (text before the first `:`) exceeds 12 characters.
///
/// # Examples
///
/// ```ignore
/// get_cargo_info_format("Compiling: my_program.rs");
/// // returns " Compiling my_program.rs"
/// ```
pub fn get_cargo_info_format(str: impl Into<String>) -> String {
let s = str.into();
let (prefix, content) = if let Some(pos) = s.find(':') {
(
s[..pos].trim().to_string(),
s[pos + 1..].trim_start().to_string(),
)
} else {
(String::new(), s.trim().to_string())
};
assert!(
prefix.len() <= 12,
"prefix length exceeds 12: '{}' has length {}",
prefix,
prefix.len()
);
let padding = " ".repeat(12 - prefix.len());
format!(
"{}{} {}",
padding,
prefix.bold().bright_green(),
content.trim()
)
}
/// Format an error message in cargo style format, with bold red "error" prefix.
///
/// The input string is printed as the error content, prefixed by a bold red
/// `error:` label.
///
/// # Examples
///
/// ```ignore
/// get_cargo_error_format("something went wrong");
/// // returns "error: something went wrong"
/// ```
pub fn get_cargo_error_format(str: impl Into<String>) -> String {
format!("{}: {}", "error".bold().bright_red(), str.into())
}
/// Format a help message in cargo style format, with bright white "help" prefix.
///
/// The input string is printed as the help content, prefixed by a bright white
/// `help:` label (not bold).
///
/// # Examples
///
/// ```ignore
/// get_cargo_help_format("use --verbose for more info");
/// // returns "help: use --verbose for more info"
/// ```
pub fn get_cargo_help_format(str: impl Into<String>) -> String {
format!("{}: {}", "help".bright_white(), str.into())
}
|