Added month splitting option

master
Wynd 2024-08-24 16:41:51 +03:00
parent a7ef30e412
commit 468f6a4eba
4 changed files with 37 additions and 10 deletions

View File

@ -50,6 +50,9 @@ $ git-heatmap --root-dir "/path"
# by default merges are counted so using --no-merges ensures they won't be counted # by default merges are counted so using --no-merges ensures they won't be counted
$ git-heatmap --no-merges $ 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 # 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 # shading all the others accordingly, with by-amount it will instead color each day based on
# the amount of commits in that day # the amount of commits in that day

View File

@ -32,6 +32,9 @@ pub struct CliArgs {
#[arg(long("until"))] #[arg(long("until"))]
pub until: Option<String>, pub until: Option<String>,
#[arg(long("split-months"), help("Split months"), default_value_t = false)]
pub split_months: bool,
#[arg(long("no-merges"), default_value_t = false)] #[arg(long("no-merges"), default_value_t = false)]
pub no_merges: bool, pub no_merges: bool,

View File

@ -17,7 +17,13 @@ pub struct Heatmap {
} }
impl Heatmap { impl Heatmap {
pub fn new(since: NaiveDate, until: NaiveDate, repos: usize, commits: Vec<Commit>) -> Self { pub fn new(
since: NaiveDate,
until: NaiveDate,
repos: usize,
commits: Vec<Commit>,
split_months: bool,
) -> Self {
let mut heatmap = Self { let mut heatmap = Self {
data: [vec![], vec![], vec![], vec![], vec![], vec![], vec![]], data: [vec![], vec![], vec![], vec![], vec![], vec![], vec![]],
since, since,
@ -46,6 +52,19 @@ impl Heatmap {
} }
while current_day <= until { 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(); let month_name = current_day.format("%b").to_string();
if current_day == since { if current_day == since {
@ -75,6 +94,13 @@ impl Heatmap {
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 { fn months_row(&self) -> String {
let mut row = " ".to_string(); let mut row = " ".to_string();
@ -144,4 +170,3 @@ pub enum ColorLogic {
ByAmount, ByAmount,
ByWeight, ByWeight,
} }

View File

@ -89,15 +89,11 @@ fn main() -> Result<()> {
.unwrap_or_else(|| get_default_until(since)); .unwrap_or_else(|| get_default_until(since));
let until = NaiveDate::parse_from_str(&until, "%Y-%m-%d").unwrap(); let until = NaiveDate::parse_from_str(&until, "%Y-%m-%d").unwrap();
// let mut repo_dirs: Vec<PathBuf> = vec![]; let split_months = args.split_months;
// if let Some(root_dir) = &args.root_dir {
// find_git_repos(root_dir, &mut repo_dirs, &args);
// }
//
// dbg!(repo_dirs);
let commits = get_commits(args, since).with_context(|| "Could not fetch commit list")?; 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}"); println!("{heatmap}");
@ -196,7 +192,7 @@ fn get_commits(args: CliArgs, start_date: NaiveDate) -> Result<(usize, Vec<Commi
None => vec![PathBuf::from(".")], None => 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() { if repos.len() > 1 && repos.len() != branches.len() {
return Err(anyhow!( return Err(anyhow!(