summaryrefslogtreecommitdiffstats
path: root/src/flagged.rs
diff options
context:
space:
mode:
authorqkzk <qu3nt1n@gmail.com>2022-12-21 14:44:56 +0100
committerqkzk <qu3nt1n@gmail.com>2022-12-21 14:44:56 +0100
commitde32db0320283fd3c34ebb9752c517333e6f978e (patch)
tree638c6fae9918da4522d541a744ddc55c15131cad /src/flagged.rs
parentc02d7a5fd8eaaf183c7a4afee6d2503deb146812 (diff)
struct for flagged files
Diffstat (limited to 'src/flagged.rs')
-rw-r--r--src/flagged.rs51
1 files changed, 51 insertions, 0 deletions
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<PathBuf>,
+ 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);