use std::path::Path;
use anyhow::Result;
use diffs_rs::engine::Command;
use plannotator_tui_review::{Side, progress};
use super::Screen;
pub(super) fn saving(data_dir: &Path) -> bool {
let env = std::env::var("PLANNOTATOR_REVIEW_PROGRESS").ok();
let config = std::fs::read_to_string(data_dir.join("config.json")).ok();
progress::enabled(env.as_deref(), config.as_deref())
}
impl Screen {
pub(super) fn viewed_label(&self) -> String {
let total = self.capture.files.len();
let viewed = self.progress.count(self.capture.files.iter().map(|f| f.path.as_str()));
format!("{viewed}/{total} viewed")
}
pub(super) fn sync_viewed(&mut self) {
let viewed: Vec<String> = self
.capture
.files
.iter()
.filter(|file| self.progress.is_viewed(&file.path))
.map(|file| file.path.clone())
.collect();
if let Err(err) = self.diff.set_viewed(&viewed) {
self.status = Some(format!("could not tick viewed files in the file list: {err:#}"));
}
}
pub(super) fn cursor_file_viewed(&self) -> bool {
let path = match self.diff.selection() {
Some(lines) => Some(lines.path),
None => self.diff.cursor().ok().flatten().map(|lines| lines.path),
};
path.is_some_and(|p| self.progress.is_viewed(&p))
}
pub(super) fn file_under_cursor(&mut self) -> Result<Option<String>> {
let mut path = match self.diff.selection() {
Some(lines) => Some(lines.path),
None => self.diff.cursor()?.map(|lines| lines.path),
};
for _ in 0..3 {
if path.is_some() {
break;
}
self.diff.run(Command::Down)?;
path = self.diff.cursor()?.map(|lines| lines.path);
}
Ok(path)
}
pub(super) fn toggle_viewed(&mut self) -> Result<()> {
let Some(path) = self.file_under_cursor()? else {
self.status = Some("move the cursor onto a line of the file first".into());
return Ok(());
};
self.toggle_viewed_of(&path);
Ok(())
}
pub(super) fn toggle_viewed_of(&mut self, path: &str) {
let viewed = !self.progress.is_viewed(path);
let saved = self.progress.set(path, viewed);
self.sync_viewed();
let count = self.viewed_label();
self.status = Some(match (saved, viewed) {
(Err(err), _) => format!("{path}: for this session only, the mark was not saved: {err}"),
(Ok(()), true) if !self.progress.is_saved(path) => {
format!("\u{2713} {path} viewed for this session \u{b7} {count}")
}
(Ok(()), true) => format!("\u{2713} {path} viewed \u{b7} {count}"),
(Ok(()), false) => format!("{path} not viewed \u{b7} {count}"),
});
}
pub(super) fn jump_unviewed(&mut self, forward: bool) -> Result<()> {
let paths = self.diff.paths().to_vec();
if paths.is_empty() {
return Ok(());
}
let here = self.file_under_cursor()?.and_then(|p| paths.iter().position(|q| *q == p));
let count = paths.len();
let start = here.unwrap_or(if forward { count - 1 } else { 0 });
let target = (1..=count)
.map(|step| if forward { (start + step) % count } else { (start + count - step % count) % count })
.find(|&i| paths.get(i).is_some_and(|p| !self.progress.is_viewed(p)));
let Some(index) = target.filter(|&i| Some(i) != here) else {
let all = paths.iter().all(|p| self.progress.is_viewed(p));
self.status = Some(if all { "every file is viewed" } else { "no other file to view" }.into());
return Ok(());
};
let path = paths.get(index).cloned().unwrap_or_default();
self.show_file(&path, index)?;
let left = paths.iter().filter(|p| !self.progress.is_viewed(p)).count();
self.status = Some(format!("{path} \u{b7} {left} not viewed"));
Ok(())
}
fn show_file(&mut self, path: &str, index: usize) -> Result<()> {
let first = self.capture.file(path).and_then(|file| {
let line = |side| file.lines(side).next().map(|(n, _)| (side, n));
line(Side::New).or_else(|| line(Side::Old))
});
if let Some((side, line)) = first
&& self.diff.reveal(path, side, line).is_ok()
{
return Ok(());
}
for _ in 0..self.diff.paths().len() {
self.diff.run(Command::PreviousFile)?;
}
for _ in 0..index {
self.diff.run(Command::NextFile)?;
}
Ok(())
}
}