use super::confirm::ConfirmModal;
use crate::clean::WorktreeReclaim;
use std::path::{Path, PathBuf};
pub const DEFAULT_CHOICE_LABEL: &str = "(default)";
#[derive(Debug, Default)]
pub struct CleanOverlay {
choices: Vec<Option<String>>,
selected: usize,
target: Option<(String, PathBuf)>,
reclaim: Option<WorktreeReclaim>,
skipped: Vec<String>,
pub confirm: ConfirmModal,
}
impl CleanOverlay {
pub fn new() -> Self {
Self::default()
}
pub fn open(&mut self, profiles: Vec<String>, name: String, path: PathBuf) {
self.choices = std::iter::once(None).chain(profiles.into_iter().map(Some)).collect();
self.selected = 0;
self.target = Some((name, path));
self.reclaim = None;
self.skipped.clear();
self.confirm.reset();
}
pub fn target(&self) -> Option<(&str, &Path)> {
self.target.as_ref().map(|(n, p)| (n.as_str(), p.as_path()))
}
pub fn choice_labels(&self) -> Vec<&str> {
self
.choices
.iter()
.map(|c| c.as_deref().unwrap_or(DEFAULT_CHOICE_LABEL))
.collect()
}
pub fn has_profiles(&self) -> bool {
self.choices.len() > 1
}
pub fn selected_index(&self) -> usize {
self.selected
}
pub fn selected_profile(&self) -> Option<&str> {
self.choices.get(self.selected).and_then(|c| c.as_deref())
}
pub fn select_next(&mut self) -> bool {
if self.choices.len() < 2 {
return false;
}
self.selected = (self.selected + 1) % self.choices.len();
true
}
pub fn select_prev(&mut self) -> bool {
if self.choices.len() < 2 {
return false;
}
self.selected = (self.selected + self.choices.len() - 1) % self.choices.len();
true
}
pub fn set_scan(&mut self, reclaim: WorktreeReclaim, skipped: Vec<String>) {
self.reclaim = Some(reclaim);
self.skipped = skipped;
self.confirm.reset();
}
pub fn reclaim(&self) -> Option<&WorktreeReclaim> {
self.reclaim.as_ref()
}
pub fn skipped(&self) -> &[String] {
&self.skipped
}
pub fn total_bytes(&self) -> u64 {
self.reclaim.as_ref().map(|r| r.total_bytes).unwrap_or(0)
}
pub fn is_empty_scan(&self) -> bool {
self.total_bytes() == 0
}
}