From 468f6a4eba022c9578aa63b0902bbaf9bb4918c9 Mon Sep 17 00:00:00 2001 From: Wynd Date: Sat, 24 Aug 2024 16:41:51 +0300 Subject: [PATCH] Added month splitting option --- README.md | 3 +++ src/cli.rs | 3 +++ src/heatmap.rs | 29 +++++++++++++++++++++++++++-- src/main.rs | 12 ++++-------- 4 files changed, 37 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 71ebf94..c44e033 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,9 @@ $ git-heatmap --root-dir "/path" # by default merges are counted so using --no-merges ensures they won't be counted $ git-heatmap --no-merges +# splits the heatmap per month instead of one big chunk +$ git-heatmap --split-months + # by default it colors every day based on which one the maximum number of commits in a visible day # shading all the others accordingly, with by-amount it will instead color each day based on # the amount of commits in that day diff --git a/src/cli.rs b/src/cli.rs index 5a85db7..575afd6 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -32,6 +32,9 @@ pub struct CliArgs { #[arg(long("until"))] pub until: Option, + #[arg(long("split-months"), help("Split months"), default_value_t = false)] + pub split_months: bool, + #[arg(long("no-merges"), default_value_t = false)] pub no_merges: bool, diff --git a/src/heatmap.rs b/src/heatmap.rs index ff7af4f..0bb6dc2 100644 --- a/src/heatmap.rs +++ b/src/heatmap.rs @@ -17,7 +17,13 @@ pub struct Heatmap { } impl Heatmap { - pub fn new(since: NaiveDate, until: NaiveDate, repos: usize, commits: Vec) -> Self { + pub fn new( + since: NaiveDate, + until: NaiveDate, + repos: usize, + commits: Vec, + split_months: bool, + ) -> Self { let mut heatmap = Self { data: [vec![], vec![], vec![], vec![], vec![], vec![], vec![]], since, @@ -46,6 +52,19 @@ impl Heatmap { } while current_day <= until { + if split_months { + let last_day_of_month = + heatmap.last_day_of_month(current_day.year_ce().1 as i32, current_day.month()); + + // If current day is the last of the month we add 2 weeks worth of empty space so + // months are more visible + if last_day_of_month == current_day { + for i in 0..14 { + heatmap.data[(i as usize) % 7].push(-1); + } + } + } + let month_name = current_day.format("%b").to_string(); if current_day == since { @@ -75,6 +94,13 @@ impl Heatmap { heatmap } + fn last_day_of_month(&self, year: i32, month: u32) -> NaiveDate { + NaiveDate::from_ymd_opt(year, month + 1, 1) + .unwrap_or_else(|| NaiveDate::from_ymd(year + 1, 1, 1)) + .pred_opt() + .unwrap_or_default() + } + fn months_row(&self) -> String { let mut row = " ".to_string(); @@ -144,4 +170,3 @@ pub enum ColorLogic { ByAmount, ByWeight, } - diff --git a/src/main.rs b/src/main.rs index a3e4fd1..c11e1a3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -89,15 +89,11 @@ fn main() -> Result<()> { .unwrap_or_else(|| get_default_until(since)); let until = NaiveDate::parse_from_str(&until, "%Y-%m-%d").unwrap(); - // let mut repo_dirs: Vec = vec![]; - // if let Some(root_dir) = &args.root_dir { - // find_git_repos(root_dir, &mut repo_dirs, &args); - // } - // - // dbg!(repo_dirs); + let split_months = args.split_months; + let commits = get_commits(args, since).with_context(|| "Could not fetch commit list")?; - let heatmap = Heatmap::new(since, until, commits.0, commits.1); + let heatmap = Heatmap::new(since, until, commits.0, commits.1, split_months); println!("{heatmap}"); @@ -196,7 +192,7 @@ fn get_commits(args: CliArgs, start_date: NaiveDate) -> Result<(usize, Vec vec![PathBuf::from(".")], }; - let branches = args.branches.unwrap_or_else(|| vec!["@".to_string()]); + let branches = args.branches.unwrap_or_else(|| vec!["".to_string()]); if repos.len() > 1 && repos.len() != branches.len() { return Err(anyhow!(