From bb3bb1fd569cb0a455cc9017a952e3b1e6302c59 Mon Sep 17 00:00:00 2001 From: xeruf <27jf@pm.me> Date: Sat, 7 Sep 2024 16:25:44 +0300 Subject: [PATCH] feat: better feedback on bookmarking --- src/main.rs | 12 +++++++----- src/tasks.rs | 23 +++++++++++++++++------ 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/src/main.rs b/src/main.rs index 802c863..cb9d936 100644 --- a/src/main.rs +++ b/src/main.rs @@ -486,15 +486,17 @@ async fn main() -> Result<()> { Some('*') => { match arg { - None => match tasks.get_position_ref() { + None => match tasks.get_position() { None => { info!("Filtering for bookmarked tasks"); tasks.set_view_bookmarks(); } - Some(pos) => { - info!("Toggling bookmark"); - or_warn!(tasks.toggle_bookmark(*pos)); - } + Some(pos) => + match or_warn!(tasks.toggle_bookmark(pos)) { + Some(true) => info!("Bookmarking \"{}\"", tasks.get_task_title(&pos)), + Some(false) => info!("Removing bookmark for \"{}\"", tasks.get_task_title(&pos)), + None => {} + } }, Some(arg) => info!("Setting priority not yet implemented"), } diff --git a/src/tasks.rs b/src/tasks.rs index fa8519f..cec0f84 100644 --- a/src/tasks.rs +++ b/src/tasks.rs @@ -388,6 +388,7 @@ impl Tasks { if sparse && current.is_empty() { vec![] } else { + // TODO highlight bookmarks self.bookmarks.iter() .filter(|id| !position.is_some_and(|p| &p == id) && !ids.contains(id)) .filter_map(|id| self.get_by_id(id)) @@ -395,6 +396,7 @@ impl Tasks { .collect_vec() }; current.append(&mut bookmarks); + current } @@ -523,14 +525,23 @@ 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); } - } + /// Toggle bookmark on the given id. + /// Returns whether it was added (true) or removed (false). + pub(crate) fn toggle_bookmark(&mut self, id: EventId) -> nostr_sdk::Result { + let added = match self.bookmarks.iter().position(|b| b == &id) { + None => { + self.bookmarks.push(id); + true + } + Some(pos) => { + self.bookmarks.remove(pos); + false + } + }; self.sender.submit( EventBuilder::new(Kind::Bookmarks, "mostr pins", - self.bookmarks.iter().map(|id| Tag::event(*id)))) + self.bookmarks.iter().map(|id| Tag::event(*id))))?; + Ok(added) } pub(crate) fn set_filter_author(&mut self, key: PublicKey) -> bool {