use plannotator_tui_review::Scope;
use super::rows::{Kind, Row};
use super::source::Job;
use super::{More, Picker, REACH_PAGES};
#[derive(Debug, Default, Clone)]
pub(super) struct Marks(Vec<String>);
fn sha(row: &Row) -> Option<&str> {
match &row.kind {
Kind::Commit(commit) => Some(&commit.sha),
_ => None,
}
}
impl Marks {
pub(super) fn is_empty(&self) -> bool {
self.0.is_empty()
}
pub(super) fn clear(&mut self) {
self.0.clear();
}
pub(super) fn toggle(&mut self, row: &Row) -> bool {
let Some(sha) = sha(row) else { return false };
if let Some(at) = self.0.iter().position(|marked| marked == sha) {
self.0.remove(at);
} else {
self.0.push(sha.to_owned());
}
true
}
pub(super) fn has(&self, row: &Row) -> bool {
sha(row).is_some_and(|sha| self.0.iter().any(|marked| marked == sha))
}
fn set(&mut self, shas: Vec<String>) {
self.0 = shas;
}
fn missing(&self, rows: &[Row]) -> Vec<String> {
self.0
.iter()
.filter(|marked| !rows.iter().any(|row| sha(row) == Some(marked.as_str())))
.cloned()
.collect()
}
pub(super) fn span(&self, rows: &[Row]) -> Option<(usize, usize)> {
let mut marked = rows.iter().enumerate().filter(|(_, row)| self.has(row)).map(|(i, _)| i);
let first = marked.next()?;
Some((first, marked.next_back().unwrap_or(first)))
}
pub(super) fn covers(&self, rows: &[Row], index: usize) -> bool {
self.span(rows).is_some_and(|(first, last)| (first..=last).contains(&index))
}
pub(super) fn range(&self, rows: &[Row]) -> Option<Scope> {
let (first, last) = self.span(rows)?;
if first == last {
return None;
}
let to = rows.get(first).and_then(sha)?.to_owned();
let from = rows.get(last).and_then(sha)?.to_owned();
Some(Scope::Range { from, to })
}
}
impl Picker {
pub(super) fn aim(&mut self, scope: Scope) {
if self.aimed.take().is_some() {
self.marks.clear();
}
self.keep = Some(match scope {
Scope::Range { from, to } => {
self.marks.set(vec![to.clone(), from.clone()]);
self.aimed = Some(Scope::Range { from, to: to.clone() });
Scope::Commit { sha: to }
}
scope => scope,
});
}
pub(super) fn reach_marks(&mut self) {
let missing = self.marks.missing(&self.rows);
if missing.is_empty() {
self.reached = 0;
return;
}
if self.more == More::Ready && self.reached < REACH_PAGES {
self.reached += 1;
self.list_more();
return;
}
self.reached = 0;
if self.aimed.take().is_some() {
self.marks.clear();
} else {
self.marks.0.retain(|marked| !missing.contains(marked));
}
let shorts: Vec<&str> = missing.iter().map(|sha| sha.get(..7).unwrap_or(sha)).collect();
self.status = Some(match shorts.as_slice() {
[one] => format!("commit {one} is not in the list, so it is not marked"),
many => format!("commits {} are not in the list, so they are not marked", many.join(", ")),
});
}
pub(super) fn ask_count(&mut self) {
let Some(scope) = self.marks.range(&self.rows) else { return };
let busy = self.opening.is_some() || self.opened.is_some();
if busy || self.counting.as_ref() == Some(&scope) || self.count_of(&scope).is_some() {
return;
}
self.counting = Some(scope.clone());
if !self.ask(Job::Count { scope }) {
self.counting = None;
}
}
pub(super) fn forget_failed_counts(&mut self) {
self.counts.retain(|(_, count)| count.is_ok());
}
pub(super) fn take_count(&mut self, scope: Scope, count: Result<usize, String>) {
if self.counting.as_ref() == Some(&scope) {
self.counting = None;
}
self.counts.retain(|(known, _)| *known != scope);
self.counts.push((scope, count));
}
pub(super) fn count_of(&self, scope: &Scope) -> Option<&Result<usize, String>> {
self.counts.iter().find(|(known, _)| known == scope).map(|(_, count)| count)
}
}