forked from janek/mostr
feat: enable setting persistent bookmarks
This commit is contained in:
parent
1297be43bc
commit
945e29b5ed
14
src/main.rs
14
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('|') =>
|
||||
|
|
21
src/tasks.rs
21
src/tasks.rs
|
@ -379,7 +379,10 @@ impl Tasks {
|
|||
|
||||
pub(crate) fn filtered_tasks<'a>(&'a self, position: Option<&'a EventId>) -> impl Iterator<Item=&Task> + '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<Event> {
|
||||
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<EventId>) {
|
||||
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);
|
||||
|
|
Loading…
Reference in New Issue