From de32db0320283fd3c34ebb9752c517333e6f978e Mon Sep 17 00:00:00 2001 From: qkzk Date: Wed, 21 Dec 2022 14:44:56 +0100 Subject: struct for flagged files --- src/flagged.rs | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 src/flagged.rs (limited to 'src/flagged.rs') diff --git a/src/flagged.rs b/src/flagged.rs new file mode 100644 index 0000000..d8e55aa --- /dev/null +++ b/src/flagged.rs @@ -0,0 +1,51 @@ +use std::path::{Path, PathBuf}; + +use crate::impl_selectable_content; + +#[derive(Clone, Debug, Default)] +pub struct Flagged { + pub content: Vec, + pub index: usize, +} + +impl Flagged { + pub fn push(&mut self, path: PathBuf) { + if self.content.contains(&path) { + return; + } + self.content.push(path); + self.content.sort() + } + + pub fn remove(&mut self, path: PathBuf) { + if let Some(index) = self.content.iter().position(|x| *x == path) { + self.content.remove(index); + } + } + + pub fn toggle(&mut self, path: &Path) { + if let Some(index) = self.content.iter().position(|x| *x == path) { + self.content.remove(index); + } else { + self.push(path.to_path_buf()); + }; + } + + pub fn clear(&mut self) { + self.content.clear() + } + + pub fn contains(&self, path: &PathBuf) -> bool { + self.content.contains(path) + } + + pub fn filtered(&self, current_path: &Path) -> Vec<&Path> { + self.content + .iter() + .filter(|p| p.starts_with(current_path)) + .map(|p| p.as_path()) + .collect() + } +} + +impl_selectable_content!(PathBuf, Flagged); -- cgit v1.2.3