Skip to content

Commit 0130a25

Browse files
remkop22Remo Passpenserblacko2sh
authored
Add CLI option for displaying author emails (#452)
* Add `email` flag to cli args In order to display emails of authors, users can enable `email` cli flag * Display email when cli flag is set Changed type of author to contain `Option<String>`. This field is initialized when the email cli flag is set. If the email is Some() it is formatted together with the username and displayed. * Update src/onefetch/info.rs Co-authored-by: Spenser Black <[email protected]> * Update src/onefetch/repo.rs Co-authored-by: Spenser Black <[email protected]> * Formatting * update help message and var name for --email * rename --email to --show-email Co-authored-by: Remo Pas <[email protected]> Co-authored-by: Spenser Black <[email protected]> Co-authored-by: O2sh <[email protected]>
1 parent 49e46e5 commit 0130a25

File tree

3 files changed

+27
-7
lines changed

3 files changed

+27
-7
lines changed

Diff for: src/onefetch/cli.rs

+9
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ pub struct Cli {
3939
pub art_off: bool,
4040
pub text_colors: Vec<String>,
4141
pub iso_time: bool,
42+
pub show_email: bool,
4243
}
4344

4445
impl Cli {
@@ -259,6 +260,12 @@ impl Cli {
259260
.map_err(|e| e.to_string())
260261
})
261262
)
263+
.arg(
264+
Arg::with_name("show-email")
265+
.short("E")
266+
.long("show-email")
267+
.help("show the email address of each author.")
268+
)
262269
.arg(
263270
Arg::with_name("exclude")
264271
.short("e")
@@ -282,6 +289,7 @@ impl Cli {
282289
let print_languages = matches.is_present("languages");
283290
let print_package_managers = matches.is_present("package-managers");
284291
let iso_time = matches.is_present("isotime");
292+
let show_email = matches.is_present("show-email");
285293

286294
let output =
287295
matches.value_of("output").map(SerializationFormat::from_str).transpose().unwrap();
@@ -392,6 +400,7 @@ impl Cli {
392400
text_colors,
393401
art_off,
394402
iso_time,
403+
show_email,
395404
})
396405
}
397406
}

Diff for: src/onefetch/info.rs

+11-5
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pub struct Info {
2121
creation_date: String,
2222
languages: Vec<(Language, f64)>,
2323
dependencies: String,
24-
authors: Vec<(String, usize, usize)>,
24+
authors: Vec<(String, Option<String>, usize, usize)>,
2525
last_change: String,
2626
repo_url: String,
2727
number_of_commits: String,
@@ -221,7 +221,7 @@ impl Info {
221221
let number_of_branches = internal_repo.get_number_of_branches()?;
222222
let creation_date = internal_repo.get_creation_date(config.iso_time)?;
223223
let number_of_commits = internal_repo.get_number_of_commits();
224-
let authors = internal_repo.get_authors(config.number_of_authors);
224+
let authors = internal_repo.get_authors(config.number_of_authors, config.show_email);
225225
let last_change = internal_repo.get_date_of_last_commit(config.iso_time);
226226
let (repo_size, file_count) = internal_repo.get_repo_size();
227227
let workdir = internal_repo.get_work_dir()?;
@@ -312,15 +312,21 @@ impl Info {
312312

313313
let pad = title.len() + 2;
314314

315-
for (i, (author_name, author_nbr_commits, autor_contribution)) in
315+
for (i, (author_name, author_email_opt, author_nbr_commits, autor_contribution)) in
316316
self.authors.iter().enumerate()
317317
{
318+
let author = if let Some(author_email) = author_email_opt {
319+
format!("{} <{}>", author_name, author_email)
320+
} else {
321+
author_name.to_owned()
322+
};
323+
318324
if i == 0 {
319325
author_field.push_str(&format!(
320326
"{}{} {} {}\n",
321327
autor_contribution.to_string().color(self.text_colors.info),
322328
"%".color(self.text_colors.info),
323-
author_name.to_string().color(self.text_colors.info),
329+
author.to_string().color(self.text_colors.info),
324330
author_nbr_commits.to_string().color(self.text_colors.info),
325331
));
326332
} else {
@@ -329,7 +335,7 @@ impl Info {
329335
"",
330336
autor_contribution.to_string().color(self.text_colors.info),
331337
"%".color(self.text_colors.info),
332-
author_name.to_string().color(self.text_colors.info),
338+
author.to_string().color(self.text_colors.info),
333339
author_nbr_commits.to_string().color(self.text_colors.info),
334340
width = pad
335341
));

Diff for: src/onefetch/repo.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,11 @@ impl<'a> Repo<'a> {
6363
number_of_commits.to_string()
6464
}
6565

66-
pub fn get_authors(&self, n: usize) -> Vec<(String, usize, usize)> {
66+
pub fn get_authors(
67+
&self,
68+
n: usize,
69+
show_email: bool,
70+
) -> Vec<(String, Option<String>, usize, usize)> {
6771
let mut authors = std::collections::HashMap::new();
6872
let mut author_name_by_email = std::collections::HashMap::new();
6973
let mut total_nbr_of_commits = 0;
@@ -83,11 +87,12 @@ impl<'a> Repo<'a> {
8387

8488
authors.truncate(n);
8589

86-
let authors: Vec<(String, usize, usize)> = authors
90+
let authors: Vec<(String, Option<String>, usize, usize)> = authors
8791
.into_iter()
8892
.map(|(author_email, author_nbr_of_commits)| {
8993
(
9094
author_name_by_email.get(&author_email).unwrap().trim_matches('\'').to_string(),
95+
show_email.then(|| author_email),
9196
author_nbr_of_commits,
9297
(author_nbr_of_commits as f32 * 100. / total_nbr_of_commits as f32).round()
9398
as usize,

0 commit comments

Comments
 (0)