Skip to content

Commit

Permalink
add cli flag to set maximum number of languages to be shown #863
Browse files Browse the repository at this point in the history
  • Loading branch information
o2sh committed Nov 19, 2022
1 parent 4958f48 commit 8159b34
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 8 deletions.
8 changes: 6 additions & 2 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PathBuf>,
Expand Down Expand Up @@ -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(),
Expand Down
64 changes: 59 additions & 5 deletions src/info/langs/language.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,18 @@ pub struct LanguageWithPercentage {

pub struct LanguagesInfo {
pub languages_with_percentage: Vec<LanguageWithPercentage>,
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 {
Expand All @@ -30,6 +36,7 @@ impl LanguagesInfo {
Self {
languages_with_percentage,
true_color,
number_of_languages,
info_color,
}
}
Expand Down Expand Up @@ -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::<Vec<_>>();
if self.languages_with_percentage.len() > self.number_of_languages {
let mut languages = iter
.by_ref()
.take(self.number_of_languages)
.collect::<Vec<_>>();
let other_perc = iter.fold(0.0, |acc, x| acc + x.1);
languages.push((
"Other".to_string(),
Expand Down Expand Up @@ -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!(
Expand All @@ -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()
));
}
}
7 changes: 6 additions & 1 deletion src/info/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 8159b34

Please sign in to comment.