use super::App;
use crate::git::load;
#[derive(Clone, Copy, PartialEq)]
pub enum Scope {
Local,
All,
Pushed,
}
pub const SCOPES: [Scope; 3] = [Scope::Local, Scope::All, Scope::Pushed];
pub const DEFAULT_SCOPE: usize = 1;
impl Scope {
pub fn label(self) -> &'static str {
match self {
Scope::Local => "local",
Scope::All => "all",
Scope::Pushed => "pushed",
}
}
}
impl App {
pub(super) fn set_repo(&mut self, repo: String) {
self.repo = repo;
self.load_branches();
}
pub(super) fn ensure_unpushed(&mut self) {
if self.unpushed_for != self.repo {
self.unpushed = load::load_unpushed(&self.repo);
self.unpushed_for = self.repo.clone();
}
}
pub(super) fn load_commits(&mut self) {
let args: Vec<&str> = self.log_args.iter().map(String::as_str).collect();
self.commits = load::load_commits(&self.repo, &args, self.limit);
self.rescope_commits();
}
pub(super) fn rescope_commits(&mut self) {
let scope = SCOPES[self.csel.scope];
let visible = (0..self.commits.len())
.filter(|&i| match scope {
Scope::All => true,
Scope::Local => self.unpushed.contains(&self.commits[i].hash),
Scope::Pushed => !self.unpushed.contains(&self.commits[i].hash),
})
.collect();
self.csel.show(visible);
}
pub(super) fn enter_commit(&mut self) {
let Some(i) = self.csel.idx() else {
self.files.clear();
self.fsel.show(Vec::new());
return;
};
let paths: Vec<&str> = self.paths.iter().map(String::as_str).collect();
self.files = load::load_files(&self.repo, &self.commits[i].hash, &paths);
self.fsel.show_all(self.files.len());
}
pub(super) fn commit_hash(&self) -> Option<&str> {
self.csel.idx().map(|i| self.commits[i].hash.as_str())
}
pub(super) fn file_path(&self) -> Option<&str> {
self.fsel.idx().map(|i| self.files[i].path.as_str())
}
}