2024-08-16 21:00:37 +03:00
|
|
|
use std::{collections::BTreeMap, fmt::Display};
|
2024-08-15 22:09:51 +03:00
|
|
|
|
2024-08-16 21:00:37 +03:00
|
|
|
use chrono::{Datelike, Duration, NaiveDate};
|
2024-08-15 22:09:51 +03:00
|
|
|
use clap::ValueEnum;
|
|
|
|
|
|
|
|
use crate::{get_char, get_color, get_color_map, Commit, DAYS, RESET};
|
|
|
|
|
|
|
|
pub struct Heatmap {
|
2024-08-16 12:50:24 +03:00
|
|
|
data: [Vec<i32>; 7],
|
2024-08-16 21:00:37 +03:00
|
|
|
since: NaiveDate,
|
|
|
|
until: NaiveDate,
|
2024-08-15 22:09:51 +03:00
|
|
|
commits: Vec<Commit>,
|
|
|
|
months: Vec<(usize, String)>,
|
2024-08-16 12:50:24 +03:00
|
|
|
highest_count: i32,
|
2024-08-15 22:09:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Heatmap {
|
2024-08-16 21:00:37 +03:00
|
|
|
pub fn new(since: NaiveDate, until: NaiveDate, commits: Vec<Commit>) -> Self {
|
2024-08-15 22:09:51 +03:00
|
|
|
let mut heatmap = Self {
|
|
|
|
data: [vec![], vec![], vec![], vec![], vec![], vec![], vec![]],
|
2024-08-16 21:00:37 +03:00
|
|
|
since,
|
|
|
|
until,
|
2024-08-15 22:09:51 +03:00
|
|
|
commits,
|
|
|
|
months: vec![],
|
2024-08-16 12:50:24 +03:00
|
|
|
highest_count: 0,
|
2024-08-15 22:09:51 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
let mut grouped_commits = BTreeMap::new();
|
|
|
|
|
|
|
|
for commit in &heatmap.commits {
|
2024-08-16 02:27:50 +03:00
|
|
|
let commit_day = commit.time.date_naive();
|
2024-08-15 22:09:51 +03:00
|
|
|
let record = grouped_commits.entry(commit_day).or_insert(0);
|
|
|
|
*record += 1;
|
|
|
|
}
|
|
|
|
|
2024-08-16 21:00:37 +03:00
|
|
|
let mut current_day = since;
|
2024-08-16 12:50:24 +03:00
|
|
|
let mut day_of_week = current_day.weekday().num_days_from_monday() % 7;
|
2024-08-15 22:09:51 +03:00
|
|
|
|
|
|
|
if day_of_week != 0 {
|
|
|
|
for i in 0..day_of_week {
|
2024-08-16 12:50:24 +03:00
|
|
|
heatmap.data[i as usize].push(-1);
|
2024-08-15 22:09:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-16 21:00:37 +03:00
|
|
|
while current_day <= until {
|
2024-08-15 22:09:51 +03:00
|
|
|
let month_name = current_day.format("%b").to_string();
|
|
|
|
|
2024-08-16 21:00:37 +03:00
|
|
|
if current_day == since {
|
2024-08-15 22:09:51 +03:00
|
|
|
heatmap.months.push((0, month_name));
|
|
|
|
}
|
|
|
|
else if current_day.day0() == 0 {
|
|
|
|
heatmap
|
|
|
|
.months
|
|
|
|
.push((heatmap.data[day_of_week as usize].len(), month_name));
|
|
|
|
}
|
|
|
|
|
2024-08-16 21:00:37 +03:00
|
|
|
let value = grouped_commits.get(¤t_day);
|
2024-08-15 22:09:51 +03:00
|
|
|
match value {
|
2024-08-16 12:50:24 +03:00
|
|
|
Some(val) => {
|
|
|
|
heatmap.data[day_of_week as usize].push(*val);
|
|
|
|
if *val > heatmap.highest_count {
|
|
|
|
heatmap.highest_count = *val;
|
|
|
|
}
|
|
|
|
}
|
2024-08-15 22:09:51 +03:00
|
|
|
None => heatmap.data[day_of_week as usize].push(0),
|
|
|
|
}
|
2024-08-16 21:00:37 +03:00
|
|
|
// println!("{} {value:?}", current_day);
|
2024-08-15 22:09:51 +03:00
|
|
|
current_day += Duration::days(1);
|
2024-08-16 02:27:50 +03:00
|
|
|
day_of_week = current_day.weekday().num_days_from_monday() % 7;
|
2024-08-15 22:09:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
heatmap
|
|
|
|
}
|
|
|
|
|
|
|
|
fn months_row(&self) -> String {
|
|
|
|
let mut row = " ".to_string();
|
|
|
|
|
|
|
|
let mut last_index = 0;
|
|
|
|
|
|
|
|
for (index, month) in &self.months {
|
|
|
|
let range_size = (index * 2).saturating_sub(last_index * 2).saturating_sub(3);
|
|
|
|
for _i in 0..range_size {
|
|
|
|
row.push(' ');
|
|
|
|
}
|
|
|
|
last_index = *index;
|
|
|
|
row.push_str(month);
|
|
|
|
}
|
|
|
|
|
|
|
|
row
|
|
|
|
}
|
2024-08-16 21:00:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Display for Heatmap {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
let start_date = self.since.format("%Y-%b-%d").to_string();
|
|
|
|
let end_date = self.until.format("%Y-%b-%d").to_string();
|
|
|
|
let commits = self.commits.len().to_string();
|
|
|
|
|
|
|
|
write!(f, "{} - {}\n", start_date, end_date).unwrap();
|
|
|
|
write!(f, "{} commits\n\n", commits).unwrap();
|
|
|
|
write!(f, "{}\n", self.months_row()).unwrap();
|
2024-08-15 22:09:51 +03:00
|
|
|
|
|
|
|
for (day, row) in DAYS.iter().zip(&self.data) {
|
2024-08-16 21:00:37 +03:00
|
|
|
write!(f, "{day} ").unwrap();
|
2024-08-15 22:09:51 +03:00
|
|
|
for val in row {
|
2024-08-16 12:50:24 +03:00
|
|
|
match val {
|
|
|
|
x if *x >= 0 => {
|
|
|
|
let color = &get_color_map()[get_color(*val, self.highest_count)];
|
2024-08-16 21:00:37 +03:00
|
|
|
write!(f, "{color}{}{RESET} ", get_char()).unwrap();
|
2024-08-16 12:50:24 +03:00
|
|
|
}
|
|
|
|
x if *x < 0 => {
|
2024-08-16 21:00:37 +03:00
|
|
|
write!(f, "{RESET} ").unwrap();
|
2024-08-16 12:50:24 +03:00
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
2024-08-15 22:09:51 +03:00
|
|
|
}
|
2024-08-16 21:00:37 +03:00
|
|
|
write!(f, "\n").unwrap();
|
2024-08-15 22:09:51 +03:00
|
|
|
}
|
2024-08-16 21:00:37 +03:00
|
|
|
|
|
|
|
write!(f, "\nLess ").unwrap();
|
2024-08-15 22:09:51 +03:00
|
|
|
for color in get_color_map() {
|
2024-08-16 21:00:37 +03:00
|
|
|
write!(f, "{color}{}{RESET} ", get_char()).unwrap();
|
2024-08-15 22:09:51 +03:00
|
|
|
}
|
2024-08-16 21:00:37 +03:00
|
|
|
write!(f, " More\n").unwrap();
|
|
|
|
|
|
|
|
Ok(())
|
2024-08-15 22:09:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, ValueEnum)]
|
|
|
|
pub enum HeatmapColors {
|
|
|
|
Green,
|
|
|
|
Red,
|
|
|
|
}
|