From 8e1eaf4a030f35de14de8f6ca689a54899e7bf3e Mon Sep 17 00:00:00 2001 From: Wynd Date: Sat, 12 Oct 2024 11:59:13 +0300 Subject: [PATCH] New numbers formatting for the heatmap --- Cargo.lock | 2 +- Cargo.toml | 4 ++-- README.md | 5 +++++ rust-toolchain.toml | 2 +- src/cli.rs | 5 ++++- src/heatmap.rs | 48 +++++++++++++++++++++++++++++++++------------ src/main.rs | 3 ++- 7 files changed, 51 insertions(+), 18 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f105d33..884e420 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -426,7 +426,7 @@ dependencies = [ [[package]] name = "git-heatmap" -version = "1.0.3" +version = "1.0.4" dependencies = [ "anyhow", "chrono", diff --git a/Cargo.toml b/Cargo.toml index 11a9de6..c28d144 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ cargo-features = ["codegen-backend"] [package] name = "git-heatmap" -version = "1.0.3" +version = "1.0.4" edition = "2021" authors = ["Wynd "] description = "A simple and customizable heatmap for git repos" @@ -31,4 +31,4 @@ incremental = true opt-level = 3 strip = true lto = true -codegen-units = 1 +codegen-units = 1 \ No newline at end of file diff --git a/README.md b/README.md index a02df9f..3dbb4e7 100644 --- a/README.md +++ b/README.md @@ -65,6 +65,11 @@ $ git-heatmap --split-months # the amount of commits in that day $ git-heatmap --counting by-amount +# by default the format uses characters to represent days and colors them based on how many commits +# that day has, using the numbers format replaces those characters with the actual number of commits +# maxed at 99 +$ git-heatmap --format numbers + # filter by one or multiple authors (respecting the .mailmap settings) # without an -a flag all authors will be checked $ git-heatmap -a "username" -a "other" diff --git a/rust-toolchain.toml b/rust-toolchain.toml index f27b9a0..5d56faf 100644 --- a/rust-toolchain.toml +++ b/rust-toolchain.toml @@ -1,2 +1,2 @@ [toolchain] -channel= "nightly" \ No newline at end of file +channel = "nightly" diff --git a/src/cli.rs b/src/cli.rs index 4b20d0e..dd8e600 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::{ColorLogic, HeatmapColors}; +use crate::heatmap::{ColorLogic, Format, HeatmapColors}; #[derive(Clone, Debug, Parser, PartialEq, Eq)] #[command(version, about, author, long_about = None)] @@ -43,6 +43,9 @@ pub struct CliArgs { #[arg(long("counting"), value_enum, default_value_t = ColorLogic::ByWeight)] pub counting: ColorLogic, + + #[arg(short, long("format"), value_enum, default_value_t = Format::Chars)] + pub format: Format, } fn get_since_date() -> String { diff --git a/src/heatmap.rs b/src/heatmap.rs index 23ee9c9..2f23fd6 100644 --- a/src/heatmap.rs +++ b/src/heatmap.rs @@ -14,6 +14,8 @@ pub struct Heatmap { months: Vec<(usize, String)>, highest_count: i32, repos: usize, + + format: Format, } impl Heatmap { @@ -23,6 +25,7 @@ impl Heatmap { repos: usize, commits: Vec, split_months: bool, + format: Format, ) -> Self { let mut heatmap = Self { data: [vec![], vec![], vec![], vec![], vec![], vec![], vec![]], @@ -32,6 +35,7 @@ impl Heatmap { months: vec![], highest_count: 0, repos, + format, }; let mut grouped_commits = BTreeMap::new(); @@ -95,9 +99,15 @@ impl Heatmap { let mut row = " ".to_string(); let mut last_index = 0; + let mul = match self.format { + Format::Chars => 2, + Format::Numbers => 3, + }; for (index, month) in &self.months { - let range_size = (index * 2).saturating_sub(last_index * 2).saturating_sub(3); + let range_size = (index * mul) + .saturating_sub(last_index * mul) + .saturating_sub(3); for _i in 0..range_size { row.push(' '); } @@ -116,11 +126,12 @@ impl Display for Heatmap { let commits = self.commits.len().to_string(); let authors = self.commits.iter().unique_by(|c| &c.author.name).count(); - write!(f, "{} - {}\n", start_date, end_date).unwrap(); - write!(f, "{} repo(s)\n", self.repos).unwrap(); - write!(f, "{} author(s)\n", authors).unwrap(); - write!(f, "{} commit(s)\n\n", commits).unwrap(); - write!(f, "{}\n", self.months_row()).unwrap(); + writeln!(f, "{} - {}", start_date, end_date).unwrap(); + writeln!(f, "{} repo(s)", self.repos).unwrap(); + writeln!(f, "{} author(s)", authors).unwrap(); + writeln!(f, "{} commit(s)\n", commits).unwrap(); + + writeln!(f, "{}", self.months_row()).unwrap(); for (day, row) in DAYS.iter().zip(&self.data) { write!(f, "{day} ").unwrap(); @@ -128,22 +139,29 @@ impl Display for Heatmap { match val { x if *x >= 0 => { let color = &get_color_map()[get_color(*val, self.highest_count)]; - write!(f, "{color}{}{RESET} ", get_char()).unwrap(); - } - x if *x < 0 => { - write!(f, "{RESET} ").unwrap(); + match self.format { + Format::Chars => write!(f, "{color}{}{RESET} ", get_char()).unwrap(), + Format::Numbers => { + let val = val.min(&99); + write!(f, "{color}{:0>2}{RESET} ", val).unwrap(); + } + } } + x if *x < 0 => match self.format { + Format::Chars => write!(f, "{RESET} ").unwrap(), + Format::Numbers => write!(f, "{RESET} ").unwrap(), + }, _ => {} } } - write!(f, "\n").unwrap(); + writeln!(f).unwrap(); } write!(f, "\nLess ").unwrap(); for color in get_color_map() { write!(f, "{color}{}{RESET} ", get_char()).unwrap(); } - write!(f, " More\n").unwrap(); + writeln!(f, " More").unwrap(); Ok(()) } @@ -160,3 +178,9 @@ pub enum ColorLogic { ByAmount, ByWeight, } + +#[derive(Clone, Debug, PartialEq, Eq, ValueEnum)] +pub enum Format { + Chars, + Numbers, +} diff --git a/src/main.rs b/src/main.rs index a14fffd..20f1286 100644 --- a/src/main.rs +++ b/src/main.rs @@ -84,10 +84,11 @@ fn main() -> Result<()> { let until = NaiveDate::parse_from_str(&until, "%Y-%m-%d").unwrap(); let split_months = args.split_months; + let format = args.format.clone(); let commits = get_commits(args, since, until).with_context(|| "Could not fetch commit list")?; - let heatmap = Heatmap::new(since, until, commits.0, commits.1, split_months); + let heatmap = Heatmap::new(since, until, commits.0, commits.1, split_months, format); println!("{heatmap}");