From 945e29b5edbcc8cd369b806792e7865449807ce0 Mon Sep 17 00:00:00 2001 From: xeruf <27jf@pm.me> Date: Thu, 29 Aug 2024 23:20:31 +0300 Subject: [PATCH] feat: enable setting persistent bookmarks --- src/main.rs | 14 +++++++++++++- src/tasks.rs | 21 ++++++++++++++++++++- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index 29e160e..55e3959 100644 --- a/src/main.rs +++ b/src/main.rs @@ -499,7 +499,19 @@ async fn main() -> Result<()> { } Some('*') => { - info!("Setting priority not yet implemented") + match arg { + None => match tasks.get_position_ref() { + None => { + info!("Filtering for bookmarked tasks"); + tasks.set_filter_bookmarks() + }, + Some(pos) => { + info!("Toggling bookmark"); + or_warn!(tasks.toggle_bookmark(*pos)); + } + }, + Some(arg) => info!("Setting priority not yet implemented"), + } } Some('|') => diff --git a/src/tasks.rs b/src/tasks.rs index 8169da6..2ef8425 100644 --- a/src/tasks.rs +++ b/src/tasks.rs @@ -379,7 +379,10 @@ impl Tasks { pub(crate) fn filtered_tasks<'a>(&'a self, position: Option<&'a EventId>) -> impl Iterator + 'a { let current: HashMap<&EventId, &Task> = self.resolve_tasks(self.children_of(position)).map(|t| (t.get_id(), t)).collect(); - let bookmarks = self.bookmarks.iter().filter(|id| !current.contains_key(id)).filter_map(|id| self.get_by_id(id)).collect_vec(); + let bookmarks = self.bookmarks.iter() + .filter(|id| !position.is_some_and(|p| &p == id) && !current.contains_key(id)) + .filter_map(|id| self.get_by_id(id)) + .collect_vec(); // TODO use ChildIterator current.into_values().chain( bookmarks @@ -522,6 +525,20 @@ impl Tasks { // Movement and Selection + pub(crate) fn toggle_bookmark(&mut self, id: EventId) -> nostr_sdk::Result { + match self.bookmarks.iter().position(|b| b == &id) { + None => self.bookmarks.push(id), + Some(pos) => { self.bookmarks.remove(pos); }, + } + self.sender.submit( + EventBuilder::new(Kind::Bookmarks, "mostr pins", + self.bookmarks.iter().map(|id| Tag::event(*id)))) + } + + pub(crate) fn set_filter_bookmarks(&mut self) { + self.set_filter(self.bookmarks.clone()) + } + pub(crate) fn set_filter(&mut self, view: Vec) { if view.is_empty() { warn!("No match for filter!") @@ -1238,6 +1255,8 @@ mod tasks_test { assert_eq!(tasks.filtered_tasks(Some(&zero)).count(), 0); tasks.submit(EventBuilder::new(Kind::Bookmarks, "", [Tag::event(pin), Tag::event(zero)])); assert_eq!(tasks.filtered_tasks(Some(&zero)).collect_vec(), vec![tasks.get_by_id(&pin).unwrap()]); + tasks.move_to(Some(pin)); + assert!(tasks.visible_tasks().is_empty()); tasks.move_to(None); assert_eq!(tasks.visible_tasks().len(), 3);