Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add flag to disable/enable boldness #99

Merged
merged 10 commits into from
Oct 23, 2019
46 changes: 31 additions & 15 deletions src/ascii_art.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ use colored::{Color, Colorize};
pub struct AsciiArt<'a> {
content: Box<dyn 'a + Iterator<Item = &'a str>>,
colors: Vec<Color>,
bold: bool,
start: usize,
end: usize,
}
impl<'a> AsciiArt<'a> {
pub fn new(input: &'a str, colors: Vec<Color>) -> AsciiArt<'a> {
pub fn new(input: &'a str, colors: Vec<Color>, bold: bool) -> AsciiArt<'a> {
let mut lines: Vec<_> = input.lines().skip_while(|line| line.is_empty()).collect();
while let Some(line) = lines.last() {
if Tokens(line).is_empty() {
Expand All @@ -30,6 +31,7 @@ impl<'a> AsciiArt<'a> {
AsciiArt {
content: Box::new(lines.into_iter()),
colors: colors,
bold: bold,
start: start,
end: end,
}
Expand All @@ -47,7 +49,7 @@ impl<'a> Iterator for AsciiArt<'a> {
fn next(&mut self) -> Option<String> {
self.content
.next()
.map(|line| Tokens(line).render(&self.colors, self.start, self.end))
.map(|line| Tokens(line).render(&self.colors, self.start, self.end, self.bold))
}
}

Expand Down Expand Up @@ -152,7 +154,7 @@ impl<'a> Tokens<'a> {
})
}
/// render a truncated line of tokens.
fn render(self, colors: &Vec<Color>, start: usize, end: usize) -> String {
fn render(self, colors: &Vec<Color>, start: usize, end: usize, bold: bool) -> String {
assert!(start <= end);
let mut width = end - start;
let mut colored_segment = String::new();
Expand All @@ -166,7 +168,7 @@ impl<'a> Tokens<'a> {
colored_segment.push(chr);
}
Token::Color(col) => {
add_colored_segment(&mut whole_string, &colored_segment, color);
add_colored_segment(&mut whole_string, &colored_segment, color, bold);
colored_segment = String::new();
color = colors.get(col as usize).unwrap_or(&Color::White);
}
Expand All @@ -177,7 +179,7 @@ impl<'a> Tokens<'a> {
};
});

add_colored_segment(&mut whole_string, &colored_segment, color);
add_colored_segment(&mut whole_string, &colored_segment, color, bold);
(0..width).for_each(|_| whole_string.push(' '));
whole_string
}
Expand All @@ -195,8 +197,12 @@ fn succeed_when<I>(predicate: impl FnOnce(I) -> bool) -> impl FnOnce(I) -> Optio
}
}

fn add_colored_segment(base: &mut String, segment: &String, color: &Color) {
base.push_str(&format!("{}", segment.color(*color).bold()))
fn add_colored_segment(base: &mut String, segment: &String, color: &Color, bold: bool) {
let mut colored_segment = segment.color(*color);
if bold {
colored_segment = colored_segment.bold();
}
base.push_str(&format!("{}", colored_segment));
}

// Basic combinators
Expand Down Expand Up @@ -258,36 +264,46 @@ mod test {
#[test]
fn truncate() {
let colors_shim = Vec::new();
assert_eq!(Tokens("").render(&colors_shim, 0, 0), "\u{1b}[1;37m\u{1b}[0m");
assert_eq!(Tokens("").render(&colors_shim, 0, 0, true), "\u{1b}[1;37m\u{1b}[0m");

assert_eq!(
Tokens(" ").render(&colors_shim, 0, 0),
Tokens(" ").render(&colors_shim, 0, 0, true),
"\u{1b}[1;37m\u{1b}[0m"
);
assert_eq!(
Tokens(" ").render(&colors_shim, 0, 5),
Tokens(" ").render(&colors_shim, 0, 5, true),
"\u{1b}[1;37m \u{1b}[0m"
);
assert_eq!(
Tokens(" ").render(&colors_shim, 1, 5),
Tokens(" ").render(&colors_shim, 1, 5, true),
"\u{1b}[1;37m \u{1b}[0m"
);
assert_eq!(
Tokens(" ").render(&colors_shim, 3, 5),
Tokens(" ").render(&colors_shim, 3, 5, true),
"\u{1b}[1;37m \u{1b}[0m"
);
assert_eq!(
Tokens(" ").render(&colors_shim, 0, 4),
Tokens(" ").render(&colors_shim, 0, 4, true),
"\u{1b}[1;37m \u{1b}[0m"
);
assert_eq!(
Tokens(" ").render(&colors_shim, 0, 3),
Tokens(" ").render(&colors_shim, 0, 3, true),
"\u{1b}[1;37m \u{1b}[0m"
);

assert_eq!(
Tokens(" {1} {5} {9} a").render(&colors_shim, 4, 10),
Tokens(" {1} {5} {9} a").render(&colors_shim, 4, 10, true),
"\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 "
);

// Tests for bold disabled
assert_eq!(
Tokens(" ").render(&colors_shim, 0, 0, false),
"\u{1b}[37m\u{1b}[0m"
);
assert_eq!(
Tokens(" ").render(&colors_shim, 0, 5, false),
"\u{1b}[37m \u{1b}[0m"
);
}
}
85 changes: 69 additions & 16 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ struct Info {
custom_logo: Language,
custom_colors: Vec<String>,
disable_fields: InfoFieldOn,
bold_enabled: bool
}

impl fmt::Display for Info {
Expand All @@ -66,7 +67,7 @@ impl fmt::Display for Info {
writeln!(
buffer,
"{}{}",
"Project: ".color(color).bold(),
self.get_formatted_info_label("Project: ", color),
self.project_name
)?;
}
Expand All @@ -75,7 +76,7 @@ impl fmt::Display for Info {
writeln!(
buffer,
"{}{}",
"HEAD: ".color(color).bold(),
self.get_formatted_info_label("HEAD: ", color),
self.current_commit
)?;
}
Expand All @@ -84,7 +85,7 @@ impl fmt::Display for Info {
writeln!(
buffer,
"{}{}",
"Version: ".color(color).bold(),
self.get_formatted_info_label("Version: ", color),
self.version
)?;
}
Expand All @@ -93,7 +94,7 @@ impl fmt::Display for Info {
writeln!(
buffer,
"{}{}",
"Created: ".color(color).bold(),
self.get_formatted_info_label("Created: ", color),
self.creation_date
)?;
}
Expand All @@ -111,13 +112,18 @@ impl fmt::Display for Info {
s = s + &format!("{} ({} %) ", language.0, formatted_number);
}
}
writeln!(buffer, "{}{}", title.color(color).bold(), s)?;
writeln!(
buffer,
"{}{}",
self.get_formatted_info_label(title, color),
s
)?;
} else {
let title = "Language: ";
writeln!(
buffer,
"{}{}",
title.color(color).bold(),
self.get_formatted_info_label(title, color),
self.dominant_language
)?;
};
Expand All @@ -130,33 +136,52 @@ impl fmt::Display for Info {
"Author: "
};

writeln!(buffer, "{}{}% {} {}", title.color(color).bold(), self.authors[0].2, self.authors[0].0, self.authors[0].1)?;
writeln!(
buffer,
"{}{}% {} {}",
self.get_formatted_info_label(title, color),
self.authors[0].2,
self.authors[0].0,
self.authors[0].1
)?;

let title = " ".repeat(title.len());

for author in self.authors.iter().skip(1) {
writeln!(buffer, "{}{}% {} {}", title.color(color).bold(), author.2, author.0, author.1)?;
writeln!(
buffer,
"{}{}% {} {}",
self.get_formatted_info_label(&title, color),
author.2,
author.0,
author.1
)?;
}
}

if !self.disable_fields.last_change {
writeln!(
buffer,
"{}{}",
"Last change: ".color(color).bold(),
self.get_formatted_info_label("Last change: ", color),
self.last_change
)?;
}

if !self.disable_fields.repo {
writeln!(buffer, "{}{}", "Repo: ".color(color).bold(), self.repo)?;
writeln!(
buffer,
"{}{}",
self.get_formatted_info_label("Repo: ", color),
self.repo
)?;
}

if !self.disable_fields.commits {
writeln!(
buffer,
"{}{}",
"Commits: ".color(color).bold(),
self.get_formatted_info_label("Commits: ", color),
self.commits
)?;
}
Expand All @@ -165,7 +190,7 @@ impl fmt::Display for Info {
writeln!(
buffer,
"{}{}",
"Lines of code: ".color(color).bold(),
self.get_formatted_info_label("Lines of code: ", color),
self.number_of_lines
)?;
}
Expand All @@ -174,7 +199,7 @@ impl fmt::Display for Info {
writeln!(
buffer,
"{}{}",
"Size: ".color(color).bold(),
self.get_formatted_info_label("Size: ", color),
self.repo_size
)?;
}
Expand All @@ -183,7 +208,7 @@ impl fmt::Display for Info {
writeln!(
buffer,
"{}{}",
"License: ".color(color).bold(),
self.get_formatted_info_label("License: ", color),
self.license
)?;
}
Expand All @@ -209,8 +234,7 @@ impl fmt::Display for Info {
" ".on_bright_white(),
)?;


let mut logo_lines = AsciiArt::new(self.get_ascii(), self.colors());
let mut logo_lines = AsciiArt::new(self.get_ascii(), self.colors(), self.bold_enabled);
let mut info_lines = buffer.lines();
let center_pad = " ";
loop {
Expand Down Expand Up @@ -469,6 +493,20 @@ Possible values: [{0}{1}{2}{3}{4}{5}{6}{7}{8}{9}{10}{11}{12}{13}{14}{15}]",
"14".bright_cyan(),
"15".bright_white(),
)))
.arg(Arg::with_name("bold")
.short("b")
.long("bold")
.takes_value(true)
.default_value("on")
.hide_default_value(true)
.possible_values(&["on", "off"])
.hide_possible_values(true)
.help(&format!(
"{}{}{}",
"Specifies whether the logo and all info labels should be bold.",
"\nPossible values: [\"on\", \"off\"]",
"\nDefault value: [\"on\"]"
)))
ccmetz marked this conversation as resolved.
Show resolved Hide resolved
.get_matches();
let dir = String::from(matches.value_of("directory").unwrap());
let custom_logo: Language = Language::from_str(
Expand Down Expand Up @@ -523,6 +561,11 @@ Possible values: [{0}{1}{2}{3}{4}{5}{6}{7}{8}{9}{10}{11}{12}{13}{14}{15}]",
Vec::new()
};

let mut bold_flag = true;
if matches.value_of("bold") == Some("off") {
bold_flag = false;
}
ccmetz marked this conversation as resolved.
Show resolved Hide resolved

let info = Info {
project_name: config.repository_name,
current_commit: current_commit_info,
Expand All @@ -540,6 +583,7 @@ Possible values: [{0}{1}{2}{3}{4}{5}{6}{7}{8}{9}{10}{11}{12}{13}{14}{15}]",
custom_logo,
custom_colors,
disable_fields,
bold_enabled: bold_flag,
};

print!("{}", info);
Expand Down Expand Up @@ -1002,6 +1046,15 @@ impl Info {
}
}

// Returns a formatted info label with the desired color and boldness
ccmetz marked this conversation as resolved.
Show resolved Hide resolved
fn get_formatted_info_label(&self, label: &str, color: Color) -> ColoredString {
let mut formatted_label = label.color(color);
if self.bold_enabled {
formatted_label = formatted_label.bold();
}
formatted_label
}

fn colors(&self) -> Vec<Color> {
let language =
if let Language::Unknown = self.custom_logo {
Expand Down