diff --git a/benches/heatmap.rs b/benches/heatmap.rs index 61a6caf..c669226 100644 --- a/benches/heatmap.rs +++ b/benches/heatmap.rs @@ -53,5 +53,6 @@ fn heatmap_generation() { 13, heatmap::Format::Chars, true, + true, ); } diff --git a/src/cli.rs b/src/cli.rs index 1b7bf3b..2612c59 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -56,9 +56,12 @@ pub struct CliArgs { #[arg(long("list-repos"), default_value_t = false)] pub list_repos: bool, + + #[arg(long("list-days"), default_value_t = false)] + pub list_days: bool, } fn get_since_date() -> String { let date = Local::now() - Duration::days(365); date.format("%Y-%m-%d").to_string() -} \ No newline at end of file +} diff --git a/src/heatmap.rs b/src/heatmap.rs index 0f11234..d4c2b38 100644 --- a/src/heatmap.rs +++ b/src/heatmap.rs @@ -13,7 +13,7 @@ pub struct Heatmap { since: NaiveDate, until: NaiveDate, commits: Vec, - repo_commits: Vec<(String, Vec)>, + repo_commits: Vec<(String, u64)>, highest_count: i32, branches: usize, repos: usize, @@ -21,6 +21,7 @@ pub struct Heatmap { format: Format, list_repos: bool, + list_days: bool, } #[allow(clippy::too_many_arguments)] @@ -35,12 +36,13 @@ impl Heatmap { months_per_row: u16, format: Format, list_repos: bool, + list_days: bool, ) -> Self { let mut chunks = vec![]; let mut highest_count: i32 = 0; let mut grouped_commits = BTreeMap::new(); - let mut repo_commits_map: HashMap> = HashMap::new(); - let mut repo_commits = Vec::<(String, Vec)>::new(); + let mut repo_commits_map: HashMap = HashMap::new(); + let mut repo_commits = Vec::<(String, u64)>::new(); for commit in &commits { let commit_day = commit.time.date_naive(); @@ -48,13 +50,8 @@ impl Heatmap { *record += 1; if list_repos { - if repo_commits_map.contains_key(&commit.repo) { - let list = repo_commits_map.get_mut(&commit.repo).unwrap(); - list.push(commit.clone()); - } - else { - repo_commits_map.insert(commit.repo.clone(), vec![commit.clone()]); - } + let commits = repo_commits_map.entry(commit.repo.clone()).or_insert(0); + *commits += 1; } } @@ -62,7 +59,7 @@ impl Heatmap { repo_commits.extend( repo_commits_map .into_iter() - .sorted_by(|c1, c2| c2.1.len().cmp(&c1.1.len())) + .sorted_by(|c1, c2| c2.1.cmp(&c1.1)) .map(|e| (e.0, e.1)), ); } @@ -140,6 +137,7 @@ impl Heatmap { chunks, format, list_repos, + list_days, } } } @@ -172,21 +170,29 @@ impl Display for Heatmap { writeln!(f, "{} {}", commits, commits_label).unwrap(); if self.list_repos { - for (repo, commits_vec) in &self.repo_commits { - let commits_label = if commits_vec.len() == 1 { + for (repo, repo_commits) in &self.repo_commits { + let commits_label = if *repo_commits == 1 { "commit" } else { "commits" }; - writeln!(f, " {}: {} {}", repo, commits_vec.len(), commits_label).unwrap(); + writeln!(f, " {}: {} {}", repo, repo_commits, commits_label).unwrap(); } } writeln!(f).unwrap(); + let mut per_day_commits: [i32; 7] = [0, 0, 0, 0, 0, 0, 0]; + for chunk in &self.chunks { chunk.display(self, f); writeln!(f).unwrap(); + if self.list_days { + per_day_commits + .iter_mut() + .enumerate() + .for_each(|(i, v)| *v += chunk.data[i].iter().sum::()); + } } write!(f, "\nLess ").unwrap(); @@ -195,6 +201,13 @@ impl Display for Heatmap { } writeln!(f, " More").unwrap(); + if self.list_days { + writeln!(f).unwrap(); + for day in 0..DAYS.len() { + writeln!(f, "{}: {}", DAYS[day], per_day_commits[day]).unwrap(); + } + } + Ok(()) } } diff --git a/src/main.rs b/src/main.rs index cfdbadc..d196b2e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,6 +17,7 @@ fn main() -> Result<()> { let months_per_row = args.months_per_row; let format = args.format; let list_repos = args.list_repos; + let list_days = args.list_days; let commits = libgitheatmap::get_commits(args, since, until) .with_context(|| "Could not fetch commit list")?; @@ -31,6 +32,7 @@ fn main() -> Result<()> { months_per_row, format, list_repos, + list_days, ); println!("{heatmap}");