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
$ 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

View File

@ -32,6 +32,9 @@ pub struct CliArgs {
#[arg(long("until"))]
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)]
pub no_merges: bool,

View File

@ -17,7 +17,13 @@ pub struct 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 {
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,
}

View File

@ -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<PathBuf> = 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<Commi
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() {
return Err(anyhow!(