Skip to content

Add CLI option for displaying author emails #452

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 7 commits into from
Jun 30, 2021
Merged
Show file tree
Hide file tree
Changes from 5 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
9 changes: 9 additions & 0 deletions src/onefetch/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pub struct Cli {
pub art_off: bool,
pub text_colors: Vec<String>,
pub iso_time: bool,
pub email: bool,
}

impl Cli {
Expand Down Expand Up @@ -259,6 +260,12 @@ impl Cli {
.map_err(|e| e.to_string())
})
)
.arg(
Arg::with_name("email")
.short("E")
.long("email")
.help("Display emails in authors section.")
)
.arg(
Arg::with_name("exclude")
.short("e")
Expand All @@ -282,6 +289,7 @@ impl Cli {
let print_languages = matches.is_present("languages");
let print_package_managers = matches.is_present("package-managers");
let iso_time = matches.is_present("isotime");
let email = matches.is_present("email");

let output =
matches.value_of("output").map(SerializationFormat::from_str).transpose().unwrap();
Expand Down Expand Up @@ -392,6 +400,7 @@ impl Cli {
text_colors,
art_off,
iso_time,
email,
})
}
}
16 changes: 11 additions & 5 deletions src/onefetch/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub struct Info {
creation_date: String,
languages: Vec<(Language, f64)>,
dependencies: String,
authors: Vec<(String, usize, usize)>,
authors: Vec<(String, Option<String>, usize, usize)>,
last_change: String,
repo_url: String,
number_of_commits: String,
Expand Down Expand Up @@ -221,7 +221,7 @@ impl Info {
let number_of_branches = internal_repo.get_number_of_branches()?;
let creation_date = internal_repo.get_creation_date(config.iso_time)?;
let number_of_commits = internal_repo.get_number_of_commits();
let authors = internal_repo.get_authors(config.number_of_authors);
let authors = internal_repo.get_authors(config.number_of_authors, config.email);
let last_change = internal_repo.get_date_of_last_commit(config.iso_time);
let (repo_size, file_count) = internal_repo.get_repo_size();
let workdir = internal_repo.get_work_dir()?;
Expand Down Expand Up @@ -312,15 +312,21 @@ impl Info {

let pad = title.len() + 2;

for (i, (author_name, author_nbr_commits, autor_contribution)) in
for (i, (author_name, author_email_opt, author_nbr_commits, autor_contribution)) in
self.authors.iter().enumerate()
{
let author = if let Some(author_email) = author_email_opt {
format!("{} <{}>", author_name, author_email)
} else {
author_name.to_owned()
};

if i == 0 {
author_field.push_str(&format!(
"{}{} {} {}\n",
autor_contribution.to_string().color(self.text_colors.info),
"%".color(self.text_colors.info),
author_name.to_string().color(self.text_colors.info),
author.to_string().color(self.text_colors.info),
author_nbr_commits.to_string().color(self.text_colors.info),
));
} else {
Expand All @@ -329,7 +335,7 @@ impl Info {
"",
autor_contribution.to_string().color(self.text_colors.info),
"%".color(self.text_colors.info),
author_name.to_string().color(self.text_colors.info),
author.to_string().color(self.text_colors.info),
author_nbr_commits.to_string().color(self.text_colors.info),
width = pad
));
Expand Down
9 changes: 7 additions & 2 deletions src/onefetch/repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@ impl<'a> Repo<'a> {
number_of_commits.to_string()
}

pub fn get_authors(&self, n: usize) -> Vec<(String, usize, usize)> {
pub fn get_authors(
&self,
n: usize,
include_email: bool,
) -> Vec<(String, Option<String>, usize, usize)> {
let mut authors = std::collections::HashMap::new();
let mut author_name_by_email = std::collections::HashMap::new();
let mut total_nbr_of_commits = 0;
Expand All @@ -83,11 +87,12 @@ impl<'a> Repo<'a> {

authors.truncate(n);

let authors: Vec<(String, usize, usize)> = authors
let authors: Vec<(String, Option<String>, usize, usize)> = authors
.into_iter()
.map(|(author_email, author_nbr_of_commits)| {
(
author_name_by_email.get(&author_email).unwrap().trim_matches('\'').to_string(),
include_email.then(|| author_email),
author_nbr_of_commits,
(author_nbr_of_commits as f32 * 100. / total_nbr_of_commits as f32).round()
as usize,
Expand Down