CLI option to list commits + made the caching commits vec global

master
Wynd 2025-10-04 22:32:55 +03:00
parent f04b6b2b2e
commit 66e7a76956
3 changed files with 47 additions and 25 deletions

View File

@ -66,9 +66,12 @@ pub struct CliArgs {
#[arg(long("list-days"), default_value_t = false)]
pub list_days: bool,
#[arg(long("list-commits"), default_value_t = false)]
pub list_commits: bool,
}
fn get_since_date() -> String {
let date = Local::now() - Duration::days(364);
date.format("%Y-%m-%d").to_string()
}
}

View File

@ -50,11 +50,11 @@ pub const RED_COLOR_MAP: [Rgb; 5] = [
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Commit {
id: ObjectId,
title: String,
author: Author,
repo: String,
time: DateTime<Local>,
pub id: ObjectId,
pub title: String,
pub author: Author,
pub repo: String,
pub time: DateTime<Local>,
}
impl Commit {
@ -81,8 +81,8 @@ impl Commit {
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub struct Author {
name: String,
email: String,
pub name: String,
pub email: String,
}
pub fn args() -> CliArgs {
@ -122,7 +122,7 @@ pub fn get_commits(
(repos, branches)
}
None => {
let mut repos = match args.repos {
let repos = match args.repos {
Some(r) => r,
None => vec![PathBuf::from(".")],
};
@ -152,6 +152,7 @@ pub fn get_commits(
let mut repos_count: usize = 0;
let mut branches_count: usize = 0;
let mut cached_commits: Vec<Commit> = vec![];
for (i, repo_path) in repos.iter().enumerate() {
let repo = ThreadSafeRepository::open(repo_path).unwrap();
@ -183,7 +184,6 @@ pub fn get_commits(
});
let repo = repo.to_thread_local();
let mut cached_commits: Vec<Commit> = vec![];
let branch_commits = branch_commits
.into_iter()

View File

@ -1,6 +1,10 @@
use anyhow::{Context, Result};
use chrono::NaiveDate;
use libgitheatmap::heatmap::Heatmap;
use libgitheatmap::{RESET, heatmap::Heatmap, rgb::Rgb};
const REPO_COLOR: Rgb = Rgb(0, 255, 0);
const AUTHOR_COLOR: Rgb = Rgb(200, 200, 0);
const TIME_COLOR: Rgb = Rgb(0, 200, 200);
fn main() -> Result<()> {
let args = libgitheatmap::args();
@ -18,24 +22,39 @@ fn main() -> Result<()> {
let format = args.format;
let list_repos = args.list_repos;
let list_days = args.list_days;
let list_commits = args.list_commits;
let commits = libgitheatmap::get_commits(args, since, until)
let mut commits = libgitheatmap::get_commits(args, since, until)
.with_context(|| "Could not fetch commit list")?;
let heatmap = Heatmap::new(
since,
until,
commits.0,
commits.1,
commits.2,
split_months,
months_per_row,
format,
list_repos,
list_days,
);
if list_commits {
// Newer entries at the bottom, older entries at top
commits.2.reverse();
for commit in commits.2 {
let repo = format!("{}{}", REPO_COLOR.to_ansi(), commit.repo);
let author = format!("{}{}", AUTHOR_COLOR.to_ansi(), commit.author.name);
let time = format!("{}{}", TIME_COLOR.to_ansi(), commit.time);
let message = commit.title;
println!("{heatmap}");
println!("{repo} {author} {time}\n{RESET}{message}\n",);
}
}
else {
let heatmap = Heatmap::new(
since,
until,
commits.0,
commits.1,
commits.2,
split_months,
months_per_row,
format,
list_repos,
list_days,
);
println!("{heatmap}");
}
Ok(())
}