use std::collections::HashMap;
use std::path::{Path, PathBuf};
use crate::git;
const LOG_LIMIT: usize = 8;
const DIFF_STAT_LIMIT: usize = 12;
#[derive(Debug, Clone, Default)]
pub struct Preview {
pub commits: Vec<String>,
pub diff_stat: Vec<String>,
}
#[derive(Default)]
pub struct Cache {
data: HashMap<PathBuf, Preview>,
}
impl Cache {
pub fn get_or_compute(&mut self, wt_path: &Path) -> &Preview {
self.data
.entry(wt_path.to_path_buf())
.or_insert_with(|| compute(wt_path))
}
}
fn compute(path: &Path) -> Preview {
let commits = git::capture(
path,
&[
"log",
"-n",
&LOG_LIMIT.to_string(),
"--oneline",
"--decorate",
"--color=never",
],
)
.map(|s| s.lines().map(String::from).collect())
.unwrap_or_default();
let diff_stat = git::capture(path, &["diff", "--stat", "--color=never"])
.map(|s| s.lines().take(DIFF_STAT_LIMIT).map(String::from).collect())
.unwrap_or_default();
Preview { commits, diff_stat }
}