add language bar
* add language bar
* fix text-colors
* fix text-colors
* fix naming
* address pr review comments
* Collect language bar to `String`
Collects the language bar to a string to reduce String mutations.
@@ -305,29 +305,59 @@ impl Info {
fn get_language_field(&self, title: &str) -> String {
let mut language_field = String::from("");
-
+ let language_bar_length = 26;
let pad = title.len() + 2;
-
- let languages: Vec<(String, f64)> = {
- let mut iter = self.languages.iter().map(|x| (format!("{}", x.0), x.1));
+ let color_palette = vec![
+ Color::Red,
+ Color::Green,
+ Color::Yellow,
+ Color::Blue,
+ Color::Magenta,
+ Color::Cyan,
+ ];
+
+ let languages: Vec<(String, f64, Color)> = {
+ let mut iter = self.languages.iter().enumerate().map(|(i, x)| {
+ let color = if self.config.true_color {
+ x.0.get_colors(true)[0]
+ } else {
+ color_palette[i % color_palette.len()]
+ };
+ (format!("{}", x.0), x.1, color)
+ });
if self.languages.len() > 6 {
let mut languages = iter.by_ref().take(6).collect::<Vec<_>>();
let other_sum = iter.fold(0.0, |acc, x| acc + x.1);
- languages.push(("Other".to_owned(), other_sum));
+ languages.push(("Other".to_owned(), other_sum, Color::White));
languages
} else {
iter.collect()
}
};
- for (cnt, language) in languages.iter().enumerate() {
+ let language_bar: String = languages
+ .iter()
+ .map(|x| {
+ let bar_width = std::cmp::max(
+ (x.1 / 100. * language_bar_length as f64).round() as usize,
+ 1,
+ );
+ format!("{:<width$}", "".on_color(x.2), width = bar_width)
+ })
+ .collect();
+
+ language_field.push_str(&language_bar);
+
+ for (i, language) in languages.iter().enumerate() {
let formatted_number = format!("{:.*}", 1, language.1);
- let language_str =
+ let language_with_perc =
format!("{} ({} %)", language.0, formatted_number).color(self.text_colors.info);
- if cnt != 0 && cnt % 2 == 0 {
+ let language_chip = "\u{25CF}".color(language.2);
+ let language_str = format!("{} {} ", language_chip, language_with_perc);
+ if i % 2 == 0 {
language_field.push_str(&format!("\n{:<width$}{}", "", language_str, width = pad));
} else {
- language_field.push_str(&format!("{}", language_str));
+ language_field.push_str(&language_str.to_string());
}
}
language_field
