From f2e52f3815350760c43606bd054a59eace42ad79 Mon Sep 17 00:00:00 2001 From: Wynd Date: Tue, 20 Aug 2024 00:07:19 +0300 Subject: [PATCH] Added a new config for coloring based on amount or weight --- README.md | 8 +++++++- src/cli.rs | 5 ++++- src/heatmap.rs | 7 +++++++ src/main.rs | 35 ++++++++++++++++++++++++++--------- 4 files changed, 44 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index bb690f3..bfb5a2d 100644 --- a/README.md +++ b/README.md @@ -45,13 +45,19 @@ $ git-heatmap -r "/path/to/repo" -b "main" -r "other/repo" -b "" # by default merges are counted so using --no-merges ensures they won't be counted $ git-heatmap --no-merges +# by default it colors every day on a per number of commits basis with --counting by-weight +# it will color the days based on the number of commits in that day vs +# the maximum number of commits in a day that is visible +$ git-heatmap --counting by-weight + # filter by one or multiple authors # without an -a flag all authors will be checked $ git-heatmap -a "username" -a "other" # choose from when to start the checking # if no --since flag is given it will start the search one year from the current date -# if no --until date is given it will check for either 365 days since the start date or until the current day, depending on which one is closer +# if no --until date is given it will check for either 365 days since the start date or +# until the current day, depending on which one is closer $ git-heatmap --since "2013-08-23" # or choose a time span, both --since and --until must use a YYYY-MM-DD format diff --git a/src/cli.rs b/src/cli.rs index 4d79ef1..5b6cfd1 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -3,7 +3,7 @@ use std::path::PathBuf; use chrono::{Duration, Local}; use clap::{arg, Parser, ValueHint}; -use crate::heatmap::HeatmapColors; +use crate::heatmap::{ColorLogic, HeatmapColors}; #[derive(Clone, Debug, Parser, PartialEq, Eq)] #[command(version, about, long_about = None)] @@ -31,6 +31,9 @@ pub struct CliArgs { #[arg(long("no-merges"), default_value_t = false)] pub no_merges: bool, + + #[arg(long("counting"), value_enum, default_value_t = ColorLogic::ByAmount)] + pub counting: ColorLogic, } fn get_since_date() -> String { diff --git a/src/heatmap.rs b/src/heatmap.rs index f177933..ff7af4f 100644 --- a/src/heatmap.rs +++ b/src/heatmap.rs @@ -138,3 +138,10 @@ pub enum HeatmapColors { Green, Red, } + +#[derive(Clone, Debug, PartialEq, Eq, ValueEnum)] +pub enum ColorLogic { + ByAmount, + ByWeight, +} + diff --git a/src/main.rs b/src/main.rs index 4900dbc..dc15b54 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,7 +8,7 @@ use anyhow::{anyhow, Context, Result}; use chrono::{DateTime, Duration, Local, NaiveDate, TimeZone}; use clap::Parser; use gix::{bstr::ByteSlice, traverse::commit::simple::Sorting, ObjectId}; -use heatmap::HeatmapColors; +use heatmap::{ColorLogic, HeatmapColors}; use itertools::Itertools; use mailmap::Mailmap; use rgb::Rgb; @@ -24,6 +24,7 @@ pub const ESCAPE: &str = "\x1B"; pub const RESET: &str = "\x1B[0m"; pub const DAYS: [&str; 7] = ["Mo", "Tu", "We", "Th", "Fr", "Sa", "Su"]; pub static CHAR: OnceLock = OnceLock::new(); +static COLOR_LOGIC: OnceLock = OnceLock::new(); pub static COLOR_MAP: OnceLock> = OnceLock::new(); const GREEN_COLOR_MAP: [Rgb; 5] = [ @@ -64,6 +65,7 @@ fn main() -> Result<()> { // dbg!(&args); CHAR.set(args.char).unwrap(); + COLOR_LOGIC.set(args.counting.clone()).unwrap(); let color_map = match args.color_scheme { HeatmapColors::Green => GREEN_COLOR_MAP, HeatmapColors::Red => RED_COLOR_MAP, @@ -100,14 +102,29 @@ fn get_default_until(since: NaiveDate) -> String { } fn get_color(val: i32, high: i32) -> usize { - let color = val as f32 / high as f32; - match color { - 0.0 => 0, - x if x <= 0.2 => 1, - x if x <= 0.4 => 2, - x if x <= 0.8 => 3, - x if x > 0.8 => 4, - _ => 0, + match COLOR_LOGIC.get() { + Some(logic) => match logic { + ColorLogic::ByAmount => match val { + 0 => 0, + x if x < 2 => 1, + x if x < 4 => 2, + x if x < 6 => 3, + x if x > 6 => 4, + _ => 0, + }, + ColorLogic::ByWeight => { + let color = val as f32 / high as f32; + match color { + 0.0 => 0, + x if x <= 0.2 => 1, + x if x <= 0.4 => 2, + x if x <= 0.8 => 3, + x if x > 0.8 => 4, + _ => 0, + } + } + }, + None => 0, } }