Option to list amount of commits per day

master
Wynd 2025-04-24 23:08:15 +03:00
parent fe02a4bf0d
commit e608cd9958
4 changed files with 34 additions and 15 deletions

View File

@ -53,5 +53,6 @@ fn heatmap_generation() {
13, 13,
heatmap::Format::Chars, heatmap::Format::Chars,
true, true,
true,
); );
} }

View File

@ -56,9 +56,12 @@ pub struct CliArgs {
#[arg(long("list-repos"), default_value_t = false)] #[arg(long("list-repos"), default_value_t = false)]
pub list_repos: bool, pub list_repos: bool,
#[arg(long("list-days"), default_value_t = false)]
pub list_days: bool,
} }
fn get_since_date() -> String { fn get_since_date() -> String {
let date = Local::now() - Duration::days(365); let date = Local::now() - Duration::days(365);
date.format("%Y-%m-%d").to_string() date.format("%Y-%m-%d").to_string()
} }

View File

@ -13,7 +13,7 @@ pub struct Heatmap {
since: NaiveDate, since: NaiveDate,
until: NaiveDate, until: NaiveDate,
commits: Vec<Commit>, commits: Vec<Commit>,
repo_commits: Vec<(String, Vec<Commit>)>, repo_commits: Vec<(String, u64)>,
highest_count: i32, highest_count: i32,
branches: usize, branches: usize,
repos: usize, repos: usize,
@ -21,6 +21,7 @@ pub struct Heatmap {
format: Format, format: Format,
list_repos: bool, list_repos: bool,
list_days: bool,
} }
#[allow(clippy::too_many_arguments)] #[allow(clippy::too_many_arguments)]
@ -35,12 +36,13 @@ impl Heatmap {
months_per_row: u16, months_per_row: u16,
format: Format, format: Format,
list_repos: bool, list_repos: bool,
list_days: bool,
) -> Self { ) -> Self {
let mut chunks = vec![]; let mut chunks = vec![];
let mut highest_count: i32 = 0; let mut highest_count: i32 = 0;
let mut grouped_commits = BTreeMap::new(); let mut grouped_commits = BTreeMap::new();
let mut repo_commits_map: HashMap<String, Vec<Commit>> = HashMap::new(); let mut repo_commits_map: HashMap<String, u64> = HashMap::new();
let mut repo_commits = Vec::<(String, Vec<Commit>)>::new(); let mut repo_commits = Vec::<(String, u64)>::new();
for commit in &commits { for commit in &commits {
let commit_day = commit.time.date_naive(); let commit_day = commit.time.date_naive();
@ -48,13 +50,8 @@ impl Heatmap {
*record += 1; *record += 1;
if list_repos { if list_repos {
if repo_commits_map.contains_key(&commit.repo) { let commits = repo_commits_map.entry(commit.repo.clone()).or_insert(0);
let list = repo_commits_map.get_mut(&commit.repo).unwrap(); *commits += 1;
list.push(commit.clone());
}
else {
repo_commits_map.insert(commit.repo.clone(), vec![commit.clone()]);
}
} }
} }
@ -62,7 +59,7 @@ impl Heatmap {
repo_commits.extend( repo_commits.extend(
repo_commits_map repo_commits_map
.into_iter() .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)), .map(|e| (e.0, e.1)),
); );
} }
@ -140,6 +137,7 @@ impl Heatmap {
chunks, chunks,
format, format,
list_repos, list_repos,
list_days,
} }
} }
} }
@ -172,21 +170,29 @@ impl Display for Heatmap {
writeln!(f, "{} {}", commits, commits_label).unwrap(); writeln!(f, "{} {}", commits, commits_label).unwrap();
if self.list_repos { if self.list_repos {
for (repo, commits_vec) in &self.repo_commits { for (repo, repo_commits) in &self.repo_commits {
let commits_label = if commits_vec.len() == 1 { let commits_label = if *repo_commits == 1 {
"commit" "commit"
} }
else { else {
"commits" "commits"
}; };
writeln!(f, " {}: {} {}", repo, commits_vec.len(), commits_label).unwrap(); writeln!(f, " {}: {} {}", repo, repo_commits, commits_label).unwrap();
} }
} }
writeln!(f).unwrap(); writeln!(f).unwrap();
let mut per_day_commits: [i32; 7] = [0, 0, 0, 0, 0, 0, 0];
for chunk in &self.chunks { for chunk in &self.chunks {
chunk.display(self, f); chunk.display(self, f);
writeln!(f).unwrap(); writeln!(f).unwrap();
if self.list_days {
per_day_commits
.iter_mut()
.enumerate()
.for_each(|(i, v)| *v += chunk.data[i].iter().sum::<i32>());
}
} }
write!(f, "\nLess ").unwrap(); write!(f, "\nLess ").unwrap();
@ -195,6 +201,13 @@ impl Display for Heatmap {
} }
writeln!(f, " More").unwrap(); 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(()) Ok(())
} }
} }

View File

@ -17,6 +17,7 @@ fn main() -> Result<()> {
let months_per_row = args.months_per_row; let months_per_row = args.months_per_row;
let format = args.format; let format = args.format;
let list_repos = args.list_repos; let list_repos = args.list_repos;
let list_days = args.list_days;
let commits = libgitheatmap::get_commits(args, since, until) let commits = libgitheatmap::get_commits(args, since, until)
.with_context(|| "Could not fetch commit list")?; .with_context(|| "Could not fetch commit list")?;
@ -31,6 +32,7 @@ fn main() -> Result<()> {
months_per_row, months_per_row,
format, format,
list_repos, list_repos,
list_days,
); );
println!("{heatmap}"); println!("{heatmap}");