use std::collections::HashMap;
use anyhow::Result;
use plannotator_tui_review::NoteScope;
use super::filter::Filter;
use super::row::Change;
use super::{attrs, kind, tree};
use crate::review::Screen;
impl Screen {
pub(in crate::review) fn files_for_capture(&mut self) {
let paths: Vec<&str> = self.capture.files.iter().map(|f| f.path.as_str()).collect();
self.files.kinds = paths.iter().map(|p| kind::classify(p)).collect();
self.files.index = HashMap::new();
for (i, path) in paths.iter().enumerate() {
self.files.index.entry((*path).to_owned()).or_default().push(i);
}
self.files.order = tree::order(&paths);
self.files.version += 1;
let owned = paths.iter().map(|p| (*p).to_owned()).collect();
let answer = attrs::start(self.repo.root(), owned, self.wake.clone());
self.files.attributes = Some((self.files.version, answer));
let shown = self.filtered(&self.files.filter.clone());
self.set_shown(shown);
if self.files.current.as_ref().is_none_or(|c| !self.files.shown_set.contains(c)) {
self.files.current = self.files.shown.first().cloned();
}
}
pub(in crate::review) fn take_attributes(&mut self) -> Result<bool> {
let Some((version, receiver)) = &self.files.attributes else { return Ok(false) };
let answer = match receiver.try_recv() {
Ok(answer) => answer,
Err(std::sync::mpsc::TryRecvError::Empty) => return Ok(false),
Err(std::sync::mpsc::TryRecvError::Disconnected) => HashMap::new(),
};
let current = *version == self.files.version;
self.files.attributes = None;
if !current || answer.is_empty() {
return Ok(false);
}
self.files.kinds = self
.capture
.files
.iter()
.map(|f| kind::classify_with(&f.path, answer.get(&f.path).copied()))
.collect();
self.files.version += 1;
if !self.files.filter.all_kinds() {
self.apply_filter(None)?;
}
Ok(true)
}
fn set_shown(&mut self, shown: Vec<String>) {
let files = &self.capture.files;
self.files.shown_totals = shown
.iter()
.filter_map(|p| self.files.index.get(p))
.flatten()
.filter_map(|&i| files.get(i))
.fold((0, 0), |(a, r), f| (a.saturating_add(f.additions), r.saturating_add(f.deletions)));
self.files.shown_set = shown.iter().cloned().collect();
self.files.shown = shown;
self.files.shown_version += 1;
}
pub(super) fn change_of(&self, path: &str) -> Option<Change> {
let indexes = self.files.index.get(path)?;
Change::of(indexes.iter().filter_map(|&i| self.capture.files.get(i)))
}
fn filtered(&self, filter: &Filter) -> Vec<String> {
let paths: Vec<&str> = self.capture.files.iter().map(|f| f.path.as_str()).collect();
let keep = filter.keep(&paths, &self.files.kinds, |p| self.progress.is_viewed(p));
self.files
.order
.iter()
.filter(|&&i| keep.get(i).copied().unwrap_or(false))
.filter(|&&i| {
paths.get(i).and_then(|p| self.files.index.get(*p)).and_then(|v| v.first()) == Some(&i)
})
.filter_map(|&i| paths.get(i).map(|p| (*p).to_owned()))
.collect()
}
pub(in crate::review) fn listed(&self) -> Vec<String> {
self.filtered(&self.files.filter)
}
pub(in crate::review) fn apply_filter(&mut self, show: Option<String>) -> Result<()> {
let shown = self.listed();
let target = show.clone().or_else(|| self.files.current.clone()).filter(|p| shown.contains(p));
if shown == self.files.shown {
if let Some(path) = show.filter(|p| self.files.is_shown(p)) {
self.goto_file(&path)?;
}
return Ok(());
}
self.diff.replace(&self.capture, &shown)?;
self.set_shown(shown);
self.files.current = target.clone().or_else(|| self.files.shown.first().cloned());
self.files.pending = target;
Ok(())
}
pub(in crate::review) fn notes_in_hidden_files(&self) -> usize {
self.store
.pending()
.iter()
.filter(|note| note.scope != NoteScope::General)
.filter(|note| {
self.files.index.contains_key(¬e.file_path) && !self.files.is_shown(¬e.file_path)
})
.count()
}
pub(in crate::review) fn toggle_hide_viewed(&mut self) -> Result<()> {
self.files.filter.hide_viewed = !self.files.filter.hide_viewed;
self.apply_filter(None)?;
let viewed = self.capture.files.iter().filter(|f| self.progress.is_viewed(&f.path)).count();
self.status = Some(if self.files.filter.hide_viewed {
format!("hiding {viewed} viewed file{}", if viewed == 1 { "" } else { "s" })
} else {
"showing viewed files".to_owned()
});
Ok(())
}
pub(in crate::review) fn after_viewed_change(&mut self, path: &str) -> Result<()> {
self.files.viewed_changed();
if !self.files.filter.hide_viewed {
return Ok(());
}
let at = self.files.shown.iter().position(|p| p == path);
let next = at.and_then(|i| {
self.files
.shown
.iter()
.skip(i + 1)
.chain(self.files.shown.iter().take(i))
.find(|p| *p != path)
.cloned()
});
if self.files.cursor.as_deref() == Some(path) {
self.files.cursor.clone_from(&next);
}
self.apply_filter(next)
}
}