From a9509fd4f2e62c1bb8ab2b84464a6877abb33488 Mon Sep 17 00:00:00 2001 From: xeruf <27jf@pm.me> Date: Thu, 1 Aug 2024 20:00:45 +0300 Subject: [PATCH] feat: fold repeated time tracking events --- README.md | 2 +- src/main.rs | 29 +++++++++++++++++++++++------ src/tasks.rs | 16 +++++++++------- 3 files changed, 33 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index dccb8e1..df6248c 100644 --- a/README.md +++ b/README.md @@ -103,7 +103,7 @@ Dots can be repeated to move to parent tasks. - `<[TEXT]` - Close active task and move to parent, with optional state description - `!TEXT` - Set state for current task from text - `-TEXT` - add text note (comment / description) -- `@` - undoes last action (moving in place or upwards confirms pending actions) +- `@` - undoes last action (moving in place or upwards or waiting a minute confirms pending actions) Property Filters: diff --git a/src/main.rs b/src/main.rs index 6189684..9858fb4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,6 +4,7 @@ use std::fmt::Display; use std::fs; use std::fs::File; use std::io::{BufRead, BufReader, stdin, stdout, Write}; +use std::ops::Sub; use std::path::PathBuf; use std::str::FromStr; use std::sync::mpsc; @@ -32,16 +33,32 @@ struct EventSender { queue: RefCell, } impl EventSender { - fn submit(&self, event_builder: EventBuilder) -> Event { - event_builder.to_event(&self.keys) - .inspect(|e| self.queue.borrow_mut().push(e.clone())) - .unwrap() + fn submit(&self, event_builder: EventBuilder) -> Result { + if let Some(event) = self.queue.borrow().first() { + // Flush if oldest event older than a minute + if event.created_at < Timestamp::now().sub(60u64) { + debug!("Flushing Event Queue because it is older than a minute"); + self.flush(); + } + } + let mut queue = self.queue.borrow_mut(); + Ok(event_builder.to_event(&self.keys).inspect(|event| { + if event.kind.as_u64() == TRACKING_KIND { + queue.retain(|e| { + e.kind.as_u64() != TRACKING_KIND + }); + } + queue.push(event.clone()); + })?) } fn flush(&self) { - or_print(self.tx.send(self.clear())); + debug!("Flushing {} events from queue", self.queue.borrow().len()); + if self.queue.borrow().len() > 0 { + or_print(self.tx.send(self.clear())); + } } fn clear(&self) -> Events { - debug!("Cleared queue {:?}", self.queue.borrow()); + trace!("Cleared queue: {:?}", self.queue.borrow()); self.queue.replace(Vec::with_capacity(3)) } pub(crate) fn pubkey(&self) -> PublicKey { diff --git a/src/tasks.rs b/src/tasks.rs index 401ba5c..23ac2c0 100644 --- a/src/tasks.rs +++ b/src/tasks.rs @@ -381,10 +381,10 @@ impl Tasks { self.view.clear(); self.tags.clear(); // TODO unsure if this is needed, needs alternative way to clear if id == self.position { + debug!("Flushing Tasks because of move in place"); self.flush(); return; } - self.position = id; self.submit( EventBuilder::new( Kind::from(TRACKING_KIND), @@ -393,8 +393,10 @@ impl Tasks { ) ); if !id.and_then(|id| self.tasks.get(&id)).is_some_and(|t| t.parent_id() == self.position.as_ref()) { + debug!("Flushing Tasks because of move upwards"); self.flush(); } + self.position = id; } // Updates @@ -437,7 +439,7 @@ impl Tasks { } fn submit(&mut self, builder: EventBuilder) -> EventId { - let event = self.sender.submit(builder); + let event = self.sender.submit(builder).unwrap(); let id = event.id; self.add(event); id @@ -474,16 +476,16 @@ impl Tasks { pub(crate) fn undo(&mut self) { self.sender.clear().into_iter().rev().for_each(|event| { - if let Some(pos) = self.position { - if pos == event.id { - self.move_up() - } - } self.remove(&event) }); } fn remove(&mut self, event: &Event) { + if let Some(pos) = self.position { + if pos == event.id { + self.move_up() + } + } self.tasks.remove(&event.id); self.history.get_mut(&self.sender.pubkey()).map(|t| t.remove(event)); self.referenced_tasks(event, |t| { t.props.remove(event); });