Skip to content

add language bar #585

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

Merged
merged 6 commits into from
Feb 20, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 40 additions & 10 deletions src/info/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on the screenshot, this seems to line up nicely. If this was 29 characters, it might be more symmetrical with the ASCII art, which maxes out at 40 characters wide. (29 + "Languages: ".len()).

Copy link
Owner Author

@o2sh o2sh Feb 20, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the language bar's length were to be fixed, this would indeed make sense. However with the possible extra characters, this value can go up to 35, or 46 with "Languages: ".len().

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 =
format!("{} ({} %) ", language.0, formatted_number).color(self.text_colors.info);
if cnt != 0 && cnt % 2 == 0 {
let language_with_perc =
format!("{} ({} %)", language.0, formatted_number).color(self.text_colors.info);
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
Expand Down