forked from janek/mostr
1
0
Fork 0

feat: fold repeated time tracking events

This commit is contained in:
xeruf 2024-08-01 20:00:45 +03:00
parent 36fe58d3f3
commit a9509fd4f2
3 changed files with 33 additions and 14 deletions

View File

@ -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]` - Close active task and move to parent, with optional state description
- `!TEXT` - Set state for current task from text - `!TEXT` - Set state for current task from text
- `-TEXT` - add text note (comment / description) - `-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: Property Filters:

View File

@ -4,6 +4,7 @@ use std::fmt::Display;
use std::fs; use std::fs;
use std::fs::File; use std::fs::File;
use std::io::{BufRead, BufReader, stdin, stdout, Write}; use std::io::{BufRead, BufReader, stdin, stdout, Write};
use std::ops::Sub;
use std::path::PathBuf; use std::path::PathBuf;
use std::str::FromStr; use std::str::FromStr;
use std::sync::mpsc; use std::sync::mpsc;
@ -32,16 +33,32 @@ struct EventSender {
queue: RefCell<Events>, queue: RefCell<Events>,
} }
impl EventSender { impl EventSender {
fn submit(&self, event_builder: EventBuilder) -> Event { fn submit(&self, event_builder: EventBuilder) -> Result<Event> {
event_builder.to_event(&self.keys) if let Some(event) = self.queue.borrow().first() {
.inspect(|e| self.queue.borrow_mut().push(e.clone())) // Flush if oldest event older than a minute
.unwrap() 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) { fn flush(&self) {
debug!("Flushing {} events from queue", self.queue.borrow().len());
if self.queue.borrow().len() > 0 {
or_print(self.tx.send(self.clear())); or_print(self.tx.send(self.clear()));
} }
}
fn clear(&self) -> Events { fn clear(&self) -> Events {
debug!("Cleared queue {:?}", self.queue.borrow()); trace!("Cleared queue: {:?}", self.queue.borrow());
self.queue.replace(Vec::with_capacity(3)) self.queue.replace(Vec::with_capacity(3))
} }
pub(crate) fn pubkey(&self) -> PublicKey { pub(crate) fn pubkey(&self) -> PublicKey {

View File

@ -381,10 +381,10 @@ impl Tasks {
self.view.clear(); self.view.clear();
self.tags.clear(); // TODO unsure if this is needed, needs alternative way to clear self.tags.clear(); // TODO unsure if this is needed, needs alternative way to clear
if id == self.position { if id == self.position {
debug!("Flushing Tasks because of move in place");
self.flush(); self.flush();
return; return;
} }
self.position = id;
self.submit( self.submit(
EventBuilder::new( EventBuilder::new(
Kind::from(TRACKING_KIND), 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()) { 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.flush();
} }
self.position = id;
} }
// Updates // Updates
@ -437,7 +439,7 @@ impl Tasks {
} }
fn submit(&mut self, builder: EventBuilder) -> EventId { fn submit(&mut self, builder: EventBuilder) -> EventId {
let event = self.sender.submit(builder); let event = self.sender.submit(builder).unwrap();
let id = event.id; let id = event.id;
self.add(event); self.add(event);
id id
@ -474,16 +476,16 @@ impl Tasks {
pub(crate) fn undo(&mut self) { pub(crate) fn undo(&mut self) {
self.sender.clear().into_iter().rev().for_each(|event| { 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) self.remove(&event)
}); });
} }
fn remove(&mut self, event: &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.tasks.remove(&event.id);
self.history.get_mut(&self.sender.pubkey()).map(|t| t.remove(event)); self.history.get_mut(&self.sender.pubkey()).map(|t| t.remove(event));
self.referenced_tasks(event, |t| { t.props.remove(event); }); self.referenced_tasks(event, |t| { t.props.remove(event); });