Skip to content

Commit 419a32b

Browse files
committed
Merge remote-tracking branch 'o2sh/master'
2 parents 2b5ddf4 + e15894c commit 419a32b

File tree

4 files changed

+126
-50
lines changed

4 files changed

+126
-50
lines changed

Diff for: .travis.yml

-4
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,3 @@ notifications:
1717
branches:
1818
only:
1919
- master
20-
cache:
21-
cargo: true
22-
directories:
23-
- target

Diff for: src/ascii_art.rs

+68-7
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ use colored::{Color, Colorize};
33
pub struct AsciiArt<'a> {
44
content: Box<dyn 'a + Iterator<Item = &'a str>>,
55
colors: Vec<Color>,
6+
bold: bool,
67
start: usize,
78
end: usize,
89
}
910
impl<'a> AsciiArt<'a> {
10-
pub fn new(input: &'a str, colors: Vec<Color>) -> AsciiArt<'a> {
11+
pub fn new(input: &'a str, colors: Vec<Color>, bold: bool) -> AsciiArt<'a> {
1112
let mut lines: Vec<_> = input.lines().skip_while(|line| line.is_empty()).collect();
1213
while let Some(line) = lines.last() {
1314
if Tokens(line).is_empty() {
@@ -30,6 +31,7 @@ impl<'a> AsciiArt<'a> {
3031
AsciiArt {
3132
content: Box::new(lines.into_iter()),
3233
colors: colors,
34+
bold: bold,
3335
start: start,
3436
end: end,
3537
}
@@ -47,7 +49,7 @@ impl<'a> Iterator for AsciiArt<'a> {
4749
fn next(&mut self) -> Option<String> {
4850
self.content
4951
.next()
50-
.map(|line| Tokens(line).render(&self.colors, self.start, self.end))
52+
.map(|line| Tokens(line).render(&self.colors, self.start, self.end, self.bold))
5153
}
5254
}
5355

@@ -152,7 +154,7 @@ impl<'a> Tokens<'a> {
152154
})
153155
}
154156
/// render a truncated line of tokens.
155-
fn render(self, colors: &Vec<Color>, start: usize, end: usize) -> String {
157+
fn render(self, colors: &Vec<Color>, start: usize, end: usize, bold: bool) -> String {
156158
assert!(start <= end);
157159
let mut width = end - start;
158160
let mut colored_segment = String::new();
@@ -166,7 +168,7 @@ impl<'a> Tokens<'a> {
166168
colored_segment.push(chr);
167169
}
168170
Token::Color(col) => {
169-
add_colored_segment(&mut whole_string, &colored_segment, color);
171+
add_colored_segment(&mut whole_string, &colored_segment, color, bold);
170172
colored_segment = String::new();
171173
color = colors.get(col as usize).unwrap_or(&Color::White);
172174
}
@@ -177,7 +179,7 @@ impl<'a> Tokens<'a> {
177179
};
178180
});
179181

180-
add_colored_segment(&mut whole_string, &colored_segment, color);
182+
add_colored_segment(&mut whole_string, &colored_segment, color, bold);
181183
(0..width).for_each(|_| whole_string.push(' '));
182184
whole_string
183185
}
@@ -195,8 +197,12 @@ fn succeed_when<I>(predicate: impl FnOnce(I) -> bool) -> impl FnOnce(I) -> Optio
195197
}
196198
}
197199

198-
fn add_colored_segment(base: &mut String, segment: &String, color: &Color) {
199-
base.push_str(&format!("{}", segment.color(*color).bold()))
200+
fn add_colored_segment(base: &mut String, segment: &String, color: &Color, bold: bool) {
201+
let mut colored_segment = segment.color(*color);
202+
if bold {
203+
colored_segment = colored_segment.bold();
204+
}
205+
base.push_str(&format!("{}", colored_segment));
200206
}
201207

202208
// Basic combinators
@@ -254,6 +260,61 @@ mod test {
254260
assert_eq!(Tokens(" a;lksjf;a").leading_spaces(), 5);
255261
assert_eq!(Tokens(" {1} {5} {9} a").leading_spaces(), 6);
256262
}
263+
264+
#[test]
265+
fn render() {
266+
let colors_shim = Vec::new();
267+
268+
assert_eq!(
269+
Tokens("").render(&colors_shim, 0, 0, true),
270+
"\u{1b}[1;37m\u{1b}[0m"
271+
);
272+
273+
assert_eq!(
274+
Tokens(" ").render(&colors_shim, 0, 0, true),
275+
"\u{1b}[1;37m\u{1b}[0m"
276+
);
277+
278+
assert_eq!(
279+
Tokens(" ").render(&colors_shim, 0, 5, true),
280+
"\u{1b}[1;37m \u{1b}[0m"
281+
);
282+
283+
assert_eq!(
284+
Tokens(" ").render(&colors_shim, 1, 5, true),
285+
"\u{1b}[1;37m \u{1b}[0m"
286+
);
287+
288+
assert_eq!(
289+
Tokens(" ").render(&colors_shim, 3, 5, true),
290+
"\u{1b}[1;37m \u{1b}[0m"
291+
);
292+
293+
assert_eq!(
294+
Tokens(" ").render(&colors_shim, 0, 4, true),
295+
"\u{1b}[1;37m \u{1b}[0m"
296+
);
297+
298+
assert_eq!(
299+
Tokens(" ").render(&colors_shim, 0, 3, true),
300+
"\u{1b}[1;37m \u{1b}[0m"
301+
);
302+
303+
assert_eq!(
304+
Tokens(" {1} {5} {9} a").render(&colors_shim, 4, 10, true),
305+
"\u{1b}[1;37m\u{1b}[0m\u{1b}[1;37m\u{1b}[0m\u{1b}[1;37m \u{1b}[0m\u{1b}[1;37m a\u{1b}[0m "
306+
);
307+
308+
// Tests for bold disabled
309+
assert_eq!(
310+
Tokens(" ").render(&colors_shim, 0, 0, false),
311+
"\u{1b}[37m\u{1b}[0m"
312+
);
313+
assert_eq!(
314+
Tokens(" ").render(&colors_shim, 0, 5, false),
315+
"\u{1b}[37m \u{1b}[0m"
316+
);
317+
}
257318

258319
#[test]
259320
fn truncate() {

Diff for: src/info.rs

+31-19
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::fs;
44
use std::process::Command;
55
use std::str::FromStr;
66

7-
use colored::{Color, Colorize};
7+
use colored::{Color, Colorize, ColoredString};
88
use git2::Repository;
99
use license::License;
1010

@@ -32,6 +32,7 @@ pub struct Info {
3232
custom_logo: Language,
3333
custom_colors: Vec<String>,
3434
disable_fields: InfoFieldOn,
35+
bold_enabled: bool,
3536
}
3637

3738
impl std::fmt::Display for Info {
@@ -55,19 +56,19 @@ impl std::fmt::Display for Info {
5556
write_buf(&mut buf, "", &separator, color)?;
5657
}
5758
if !self.disable_fields.project {
58-
write_buf(&mut buf, "Project: ", &self.project_name, color)?;
59+
write_buf(&mut buf, &self.get_formatted_info_label("Project: ", color), &self.project_name)?;
5960
}
6061

6162
if !self.disable_fields.head {
62-
write_buf(&mut buf, "HEAD: ", &self.current_commit, color)?;
63+
write_buf(&mut buf, &self.get_formatted_info_label("HEAD: ", color), &self.current_commit)?;
6364
}
6465

6566
if !self.disable_fields.version {
66-
write_buf(&mut buf, "Version: ", &self.version, color)?;
67+
write_buf(&mut buf, &self.get_formatted_info_label("Version: ", color), &self.version)?;
6768
}
6869

6970
if !self.disable_fields.created {
70-
write_buf(&mut buf, "Created: ", &self.creation_date, color)?;
71+
write_buf(&mut buf, &self.get_formatted_info_label("Created: ", color), &self.creation_date)?;
7172
}
7273

7374
if !self.disable_fields.languages && !self.languages.is_empty() {
@@ -83,9 +84,9 @@ impl std::fmt::Display for Info {
8384
s = s + &format!("{} ({} %) ", language.0, formatted_number);
8485
}
8586
}
86-
writeln!(buf, "{}{}", title.color(color).bold(), s)?;
87+
writeln!(buf, "{}{}", &self.get_formatted_info_label(title, color), s)?;
8788
} else {
88-
write_buf(&mut buf, "Language: ", &self.dominant_language, color)?;
89+
write_buf(&mut buf, &self.get_formatted_info_label("Language: ", color), &self.dominant_language)?;
8990
};
9091
}
9192

@@ -99,7 +100,7 @@ impl std::fmt::Display for Info {
99100
writeln!(
100101
buf,
101102
"{}{}% {} {}",
102-
title.color(color).bold(),
103+
&self.get_formatted_info_label(title, color),
103104
self.authors[0].2,
104105
self.authors[0].0,
105106
self.authors[0].1
@@ -108,10 +109,11 @@ impl std::fmt::Display for Info {
108109
let title = " ".repeat(title.len());
109110

110111
for author in self.authors.iter().skip(1) {
112+
111113
writeln!(
112114
buf,
113115
"{}{}% {} {}",
114-
title.color(color).bold(),
116+
&self.get_formatted_info_label(&title, color),
115117
author.2,
116118
author.0,
117119
author.1
@@ -120,27 +122,27 @@ impl std::fmt::Display for Info {
120122
}
121123

122124
if !self.disable_fields.last_change {
123-
write_buf(&mut buf, "Last change: ", &self.last_change, color)?;
125+
write_buf(&mut buf, &self.get_formatted_info_label("Last change: ", color), &self.last_change)?;
124126
}
125127

126128
if !self.disable_fields.repo {
127-
write_buf(&mut buf, "Repo: ", &self.repo, color)?;
129+
write_buf(&mut buf, &self.get_formatted_info_label("Repo: ", color), &self.repo)?;
128130
}
129131

130132
if !self.disable_fields.commits {
131-
write_buf(&mut buf, "Commits: ", &self.commits, color)?;
133+
write_buf(&mut buf, &self.get_formatted_info_label("Commits: ", color), &self.commits)?;
132134
}
133135

134136
if !self.disable_fields.lines_of_code {
135-
write_buf(&mut buf, "Lines of code: ", &self.number_of_lines, color)?;
137+
write_buf(&mut buf, &self.get_formatted_info_label("Lines of code: ", color), &self.number_of_lines)?;
136138
}
137139

138140
if !self.disable_fields.size {
139-
write_buf(&mut buf, "Size: ", &self.repo_size, color)?;
141+
write_buf(&mut buf, &self.get_formatted_info_label("Size: ", color), &self.repo_size)?;
140142
}
141143

142144
if !self.disable_fields.license {
143-
write_buf(&mut buf, "License: ", &self.license, color)?;
145+
write_buf(&mut buf, &self.get_formatted_info_label("License: ", color), &self.license)?;
144146
}
145147

146148
writeln!(
@@ -164,7 +166,7 @@ impl std::fmt::Display for Info {
164166
" ".on_bright_white(),
165167
)?;
166168

167-
let mut logo_lines = AsciiArt::new(self.get_ascii(), self.colors());
169+
let mut logo_lines = AsciiArt::new(self.get_ascii(), self.colors(), self.bold_enabled);
168170
let mut info_lines = buf.lines();
169171

170172
let center_pad = " ";
@@ -199,6 +201,7 @@ impl Info {
199201
logo: Language,
200202
colors: Vec<String>,
201203
disabled: InfoFieldOn,
204+
bold_flag: bool,
202205
) -> Result<Info> {
203206
let authors = Info::get_authors(&dir, 3);
204207
let (git_v, git_user) = Info::get_git_info(&dir);
@@ -232,6 +235,7 @@ impl Info {
232235
custom_logo: logo,
233236
custom_colors: colors,
234237
disable_fields: disabled,
238+
bold_enabled: bold_flag,
235239
})
236240
}
237241

@@ -573,13 +577,21 @@ impl Info {
573577
};
574578
Some(color)
575579
}
580+
581+
/// Returns a formatted info label with the desired color and boldness
582+
fn get_formatted_info_label(&self, label: &str, color: Color) -> ColoredString {
583+
let mut formatted_label = label.color(color);
584+
if self.bold_enabled {
585+
formatted_label = formatted_label.bold();
586+
}
587+
formatted_label
588+
}
576589
}
577590

578591
fn write_buf<T: std::fmt::Display>(
579592
buffer: &mut String,
580-
title: &str,
593+
title: &ColoredString,
581594
content: T,
582-
color: Color,
583595
) -> std::fmt::Result {
584-
writeln!(buffer, "{}{}", title.color(color).bold(), content)
596+
writeln!(buffer, "{}{}", title, content)
585597
}

Diff for: src/main.rs

+27-20
Original file line numberDiff line numberDiff line change
@@ -137,24 +137,29 @@ fn main() -> Result<()> {
137137
.help(&format!(
138138
"Specifies a preferred color set. Unspecified colors will remain as default.
139139
Possible values: [{0}{1}{2}{3}{4}{5}{6}{7}{8}{9}{10}{11}{12}{13}{14}{15}]",
140-
"0".black(),
141-
"1".red(),
142-
"2".green(),
143-
"3".yellow(),
144-
"4".blue(),
145-
"5".magenta(),
146-
"6".cyan(),
147-
"7".white(),
148-
"8".bright_black(),
149-
"9".bright_red(),
150-
"10".bright_green(),
151-
"11".bright_yellow(),
152-
"12".bright_blue(),
153-
"13".bright_magenta(),
154-
"14".bright_cyan(),
155-
"15".bright_white(),
156-
)),
157-
)
140+
"0".black(),
141+
"1".red(),
142+
"2".green(),
143+
"3".yellow(),
144+
"4".blue(),
145+
"5".magenta(),
146+
"6".cyan(),
147+
"7".white(),
148+
"8".bright_black(),
149+
"9".bright_red(),
150+
"10".bright_green(),
151+
"11".bright_yellow(),
152+
"12".bright_blue(),
153+
"13".bright_magenta(),
154+
"14".bright_cyan(),
155+
"15".bright_white(),
156+
)))
157+
.arg(
158+
Arg::with_name("no-bold")
159+
.short("b")
160+
.long("no-bold")
161+
.help("Turns off bold formatting for the logo and all labels")
162+
)
158163
.arg(
159164
Arg::with_name("languages")
160165
.short("l")
@@ -213,7 +218,9 @@ Possible values: [{0}{1}{2}{3}{4}{5}{6}{7}{8}{9}{10}{11}{12}{13}{14}{15}]",
213218
Vec::new()
214219
};
215220

216-
let info = Info::new(&dir, custom_logo, custom_colors, disable_fields)?;
221+
let bold_flag = !matches.is_present("no-bold");
222+
223+
let info = Info::new(&dir, custom_logo, custom_colors, disable_fields, bold_flag)?;
217224

218225
print!("{}", info);
219226
Ok(())
@@ -231,4 +238,4 @@ fn is_git_installed() -> bool {
231238
struct Configuration {
232239
pub repository_name: String,
233240
pub repository_url: String,
234-
}
241+
}

0 commit comments

Comments
 (0)