Heatmap colors based on the highest visible number of commits instead of commit number for that day

master
Wynd 2024-08-16 12:50:24 +03:00
parent de4143ed81
commit 6aae792912
2 changed files with 33 additions and 17 deletions

View File

@ -1,16 +1,17 @@
use std::collections::BTreeMap; use std::collections::BTreeMap;
use chrono::{DateTime, Datelike, Duration, Local, Utc}; use chrono::{DateTime, Datelike, Duration, Local};
use clap::ValueEnum; use clap::ValueEnum;
use crate::{get_char, get_color, get_color_map, Commit, DAYS, RESET}; use crate::{get_char, get_color, get_color_map, Commit, DAYS, RESET};
pub struct Heatmap { pub struct Heatmap {
data: [Vec<u32>; 7], data: [Vec<i32>; 7],
start_date: DateTime<Local>, start_date: DateTime<Local>,
end_date: DateTime<Local>, end_date: DateTime<Local>,
commits: Vec<Commit>, commits: Vec<Commit>,
months: Vec<(usize, String)>, months: Vec<(usize, String)>,
highest_count: i32,
} }
impl Heatmap { impl Heatmap {
@ -25,6 +26,7 @@ impl Heatmap {
end_date, end_date,
commits, commits,
months: vec![], months: vec![],
highest_count: 0,
}; };
let mut grouped_commits = BTreeMap::new(); let mut grouped_commits = BTreeMap::new();
@ -36,11 +38,11 @@ impl Heatmap {
} }
let mut current_day = start_date; let mut current_day = start_date;
let mut day_of_week = (current_day.weekday().num_days_from_monday()) % 7; let mut day_of_week = current_day.weekday().num_days_from_monday() % 7;
if day_of_week != 0 { if day_of_week != 0 {
for i in 0..day_of_week { for i in 0..day_of_week {
heatmap.data[i as usize].push(0); heatmap.data[i as usize].push(-1);
} }
} }
@ -58,10 +60,15 @@ impl Heatmap {
let value = grouped_commits.get(&current_day.date_naive()); let value = grouped_commits.get(&current_day.date_naive());
match value { match value {
Some(val) => heatmap.data[day_of_week as usize].push(*val), Some(val) => {
heatmap.data[day_of_week as usize].push(*val);
if *val > heatmap.highest_count {
heatmap.highest_count = *val;
}
}
None => heatmap.data[day_of_week as usize].push(0), None => heatmap.data[day_of_week as usize].push(0),
} }
// println!("{} {value:?}", current_day.date_naive()); // println!("{} {value:?} {highest_count}", current_day.date_naive());
current_day += Duration::days(1); current_day += Duration::days(1);
day_of_week = current_day.weekday().num_days_from_monday() % 7; day_of_week = current_day.weekday().num_days_from_monday() % 7;
} }
@ -97,8 +104,16 @@ impl Heatmap {
for (day, row) in DAYS.iter().zip(&self.data) { for (day, row) in DAYS.iter().zip(&self.data) {
print!("{day} "); print!("{day} ");
for val in row { for val in row {
let color = &get_color_map()[get_color(*val)]; match val {
print!("{color}{}{RESET} ", get_char()); x if *x >= 0 => {
let color = &get_color_map()[get_color(*val, self.highest_count)];
print!("{color}{}{RESET} ", get_char());
}
x if *x < 0 => {
print!("{RESET} ");
}
_ => {}
}
} }
print!("\n"); print!("\n");
} }

View File

@ -5,7 +5,7 @@
use std::{cmp::Reverse, sync::OnceLock}; use std::{cmp::Reverse, sync::OnceLock};
use anyhow::{Context, Result}; use anyhow::{Context, Result};
use chrono::{Date, DateTime, Datelike, Duration, Local, NaiveDate, Offset, TimeZone, Utc}; use chrono::{DateTime, Duration, Local};
use clap::Parser; use clap::Parser;
use gix::{bstr::ByteSlice, ObjectId, Repository}; use gix::{bstr::ByteSlice, ObjectId, Repository};
use heatmap::HeatmapColors; use heatmap::HeatmapColors;
@ -28,7 +28,7 @@ const GREEN_COLOR_MAP: [Rgb; 5] = [
Rgb(14, 68, 41), Rgb(14, 68, 41),
Rgb(0, 109, 50), Rgb(0, 109, 50),
Rgb(38, 166, 65), Rgb(38, 166, 65),
Rgb(57, 211, 83), Rgb(25, 255, 64),
]; ];
const RED_COLOR_MAP: [Rgb; 5] = [ const RED_COLOR_MAP: [Rgb; 5] = [
@ -76,13 +76,14 @@ fn main() -> Result<()> {
Ok(()) Ok(())
} }
fn get_color(val: u32) -> usize { fn get_color(val: i32, high: i32) -> usize {
match val { let color = val as f32 / high as f32;
0 => 0, match color {
x if x < 2 => 1, 0.0 => 0,
x if x < 4 => 2, x if x <= 0.2 => 1,
x if x < 6 => 3, x if x <= 0.4 => 2,
x if x > 8 => 4, x if x <= 0.8 => 3,
x if x > 0.8 => 4,
_ => 0, _ => 0,
} }
} }