From 8159b34ce1eeb07b544f984ab43e35c449ae045f Mon Sep 17 00:00:00 2001 From: o2sh Date: Sat, 19 Nov 2022 14:06:26 +0100 Subject: [PATCH] add cli flag to set maximum number of languages to be shown #863 --- src/cli.rs | 8 +++-- src/info/langs/language.rs | 64 +++++++++++++++++++++++++++++++++++--- src/info/mod.rs | 7 ++++- 3 files changed, 71 insertions(+), 8 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index a99c84174..4cf9fe129 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -85,9 +85,12 @@ pub struct Config { /// Hides the color palette #[arg(long)] pub no_color_palette: bool, - /// NUM of authors to be shown - #[arg(long, short, default_value_t = 3usize, value_name = "NUM")] + /// Maximum NUM of authors to be shown + #[arg(long, default_value_t = 3usize, value_name = "NUM")] pub number_of_authors: usize, + /// Maximum NUM of languages to be shown + #[arg(long, default_value_t = 6usize, value_name = "NUM")] + pub number_of_languages: usize, /// Ignore all files & directories matching EXCLUDE #[arg(long, short, num_args = 1.., value_hint = ValueHint::AnyPath)] pub exclude: Vec, @@ -166,6 +169,7 @@ impl Default for Config { no_merges: Default::default(), no_color_palette: Default::default(), number_of_authors: 3, + number_of_languages: 6, exclude: Default::default(), no_bots: Default::default(), languages: Default::default(), diff --git a/src/info/langs/language.rs b/src/info/langs/language.rs index 4696fdcc9..e32b39c77 100644 --- a/src/info/langs/language.rs +++ b/src/info/langs/language.rs @@ -14,12 +14,18 @@ pub struct LanguageWithPercentage { pub struct LanguagesInfo { pub languages_with_percentage: Vec, - pub true_color: bool, - pub info_color: DynColors, + true_color: bool, + number_of_languages: usize, + info_color: DynColors, } impl LanguagesInfo { - pub fn new(languages: Vec<(Language, f64)>, true_color: bool, info_color: DynColors) -> Self { + pub fn new( + languages: Vec<(Language, f64)>, + true_color: bool, + number_of_languages: usize, + info_color: DynColors, + ) -> Self { let languages_with_percentage = languages .into_iter() .map(|(language, percentage)| LanguageWithPercentage { @@ -30,6 +36,7 @@ impl LanguagesInfo { Self { languages_with_percentage, true_color, + number_of_languages, info_color, } } @@ -65,8 +72,11 @@ impl std::fmt::Display for LanguagesInfo { (language.to_string(), percentage, circle_color) }, ); - if self.languages_with_percentage.len() > 6 { - let mut languages = iter.by_ref().take(6).collect::>(); + if self.languages_with_percentage.len() > self.number_of_languages { + let mut languages = iter + .by_ref() + .take(self.number_of_languages) + .collect::>(); let other_perc = iter.fold(0.0, |acc, x| acc + x.1); languages.push(( "Other".to_string(), @@ -144,6 +154,7 @@ mod test { percentage: 100_f64, }], true_color: false, + number_of_languages: 6, info_color: DynColors::Ansi(AnsiColors::White), }; let expected_languages_info = format!( @@ -158,4 +169,47 @@ mod test { assert_eq!(languages_info.value(), expected_languages_info); } + + #[test] + fn should_display_correct_number_of_languages() { + let languages_info = LanguagesInfo { + languages_with_percentage: vec![ + LanguageWithPercentage { + language: Language::Go, + percentage: 30_f64, + }, + LanguageWithPercentage { + language: Language::Erlang, + percentage: 40_f64, + }, + LanguageWithPercentage { + language: Language::Java, + percentage: 20_f64, + }, + LanguageWithPercentage { + language: Language::Rust, + percentage: 10_f64, + }, + ], + true_color: false, + number_of_languages: 2, + info_color: DynColors::Ansi(AnsiColors::White), + }; + + assert!(languages_info.value().contains( + &"Go (30.0 %)" + .color(DynColors::Ansi(AnsiColors::White)) + .to_string() + )); + assert!(languages_info.value().contains( + &"Erlang (40.0 %)" + .color(DynColors::Ansi(AnsiColors::White)) + .to_string() + )); + assert!(languages_info.value().contains( + &"Other (30.0 %)" + .color(DynColors::Ansi(AnsiColors::White)) + .to_string() + )); + } } diff --git a/src/info/mod.rs b/src/info/mod.rs index c7108e175..1c72047ce 100644 --- a/src/info/mod.rs +++ b/src/info/mod.rs @@ -258,7 +258,12 @@ impl Info { config.email, )?; let created = CreatedInfo::new(config.iso_time, &commits); - let languages = LanguagesInfo::new(languages, true_color, text_colors.info); + let languages = LanguagesInfo::new( + languages, + true_color, + config.number_of_languages, + text_colors.info, + ); let dependencies = DependenciesInfo::new(manifest.as_ref()); let authors = AuthorsInfo::new(text_colors.info, &mut commits); let last_change = LastChangeInfo::new(config.iso_time, &commits);