Skip to content

Commit dccea3d

Browse files
committed
Adjust boldness of logo based on bold flag
1 parent 397343b commit dccea3d

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

src/main.rs

+22-5
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ impl fmt::Display for Info {
248248
None => "",
249249
};
250250

251-
let (logo_line, extra_pad) = colorize_str(logo_line, self.colors());
251+
let (logo_line, extra_pad) = colorize_str(logo_line, self.colors(), self.bold_enabled);
252252
// If the string is empty the extra padding should not be added
253253
let pad = if logo_line.is_empty() {
254254
left_pad
@@ -267,15 +267,18 @@ fn count_newlines(s: &str) -> usize {
267267
}
268268

269269
/// Transforms a string with color format into one with proper
270-
/// escape characters for color display.
270+
/// escape characters for color display. This function is also
271+
/// responsible for formatting the string as bold depending on
272+
/// whether or not the user explicitly turned off boldness for
273+
/// onefetch.
271274
///
272275
/// Colors are specified with {0}, {1}... where the number represents
273276
/// the nth element in the colors Vec provided to the function.
274277
/// If there are more colors in the ascii than in the Vec it
275278
/// defaults to white.
276279
/// The usize in the tuple refers to the extra padding needed
277280
/// which comes from the added escape characters.
278-
fn colorize_str(line: &str, colors: Vec<Color>) -> (String, usize) {
281+
fn colorize_str(line: &str, colors: Vec<Color>, bold: bool) -> (String, usize) {
279282
// Extract colors from string coded with {n}
280283
let mut colors_in_str: Vec<Color> = line.split('{').fold(Vec::new(), |mut acc, s| {
281284
if s.len() > 2 {
@@ -301,11 +304,25 @@ fn colorize_str(line: &str, colors: Vec<Color>) -> (String, usize) {
301304
Some(&c) => c,
302305
None => Color::White,
303306
};
304-
acc.push_str(&format!("{}", s.color(c).bold()));
307+
308+
// Make the string bold if boldness flag is set to 'on'
309+
if bold {
310+
acc.push_str(&format!("{}", s.color(c).bold()));
311+
} else {
312+
acc.push_str(&format!("{}", s.color(c)));
313+
}
314+
305315
}
306316
acc
307317
});
308-
(out_str, colors_in_str.len() * 11 )
318+
319+
// Adjust the extra padding for the line. Use 2 less characters if not bold.
320+
let padding_constant = if bold {
321+
11
322+
} else {
323+
9
324+
};
325+
(out_str, colors_in_str.len() * padding_constant )
309326
}
310327

311328
/// Returns the true length of a string after substracting the {n}

0 commit comments

Comments
 (0)