From e320523fc05f0e13ee8a4ea40682e25fc2048fda Mon Sep 17 00:00:00 2001 From: xeruf <27jf@pm.me> Date: Wed, 20 Nov 2024 23:16:57 +0100 Subject: [PATCH] feat: enable setting pubkey as context and auto-filter for own --- src/kinds.rs | 1 + src/main.rs | 28 ++++++++++++++-------------- src/tasks.rs | 35 ++++++++++++++++++++++++++--------- 3 files changed, 41 insertions(+), 23 deletions(-) diff --git a/src/kinds.rs b/src/kinds.rs index e07b2bd..350b6cc 100644 --- a/src/kinds.rs +++ b/src/kinds.rs @@ -45,6 +45,7 @@ Task: - `time` - time tracked on this task by you Utilities: - `state` - indicator of current progress +- `owner` - author or task assignee - `rtime` - time tracked on this tasks and its subtree by everyone - `progress` - recursive subtask completion in percent - `subtasks` - how many direct subtasks are complete diff --git a/src/main.rs b/src/main.rs index 1da7667..14a54f5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -453,37 +453,37 @@ async fn main() -> Result<()> { } Some('@') => { - let success = match arg { + match arg { None => { let today = Timestamp::now() - 80_000; info!("Filtering for tasks from the last 22 hours"); - tasks.set_filter_from(today) + if !tasks.set_filter_from(today) { + continue 'repl; + } } Some(arg) => { if arg == "@" { - info!("Filtering for own tasks"); - tasks.set_filter_author(keys.public_key()) + info!("Showing everybody's tasks"); + tasks.set_filter_author(None) } else if let Ok(key) = PublicKey::from_str(arg) { - let author = tasks.get_username(&key); - info!("Filtering for tasks by {author}"); - tasks.set_filter_author(key) + info!("Showing {}'s tasks", tasks.get_username(&key)); + tasks.set_filter_author(Some(key)) } else if let Some((key, meta)) = tasks.find_user(arg) { - info!("Filtering for tasks by {}", meta.display_name.as_ref().unwrap_or(meta.name.as_ref().unwrap_or(&key.to_string()))); - tasks.set_filter_author(key.clone()) + info!("Showing {}'s tasks", meta.display_name.as_ref().unwrap_or(meta.name.as_ref().unwrap_or(&key.to_string()))); + tasks.set_filter_author(Some(key.clone())) } else { - parse_hour(arg, 1) + if parse_hour(arg, 1) .or_else(|| parse_date(arg).map(|utc| utc.with_timezone(&Local))) .map(|time| { info!("Filtering for tasks from {}", format_datetime_relative(time)); tasks.set_filter_from(time.to_timestamp()) }) - .unwrap_or(false) + .is_none_or(|b| !b) { + continue 'repl; + } } } }; - if !success { - continue 'repl; - } } Some('*') => { diff --git a/src/tasks.rs b/src/tasks.rs index 6daa0d4..272226b 100644 --- a/src/tasks.rs +++ b/src/tasks.rs @@ -79,6 +79,7 @@ pub(crate) struct TasksRelay { state: StateFilter, /// Current priority for filtering and new tasks priority: Option, + pubkey: Option, sender: EventSender, overflow: VecDeque, @@ -173,6 +174,7 @@ impl TasksRelay { tags_excluded: Default::default(), state: Default::default(), priority: None, + pubkey: Some(sender.pubkey()), search_depth: 4, view_depth: 0, @@ -382,14 +384,28 @@ impl TasksRelay { .and_then(|t| t.parent_id()) } + // TODO test with context elements + /// Visual representation of current context pub(crate) fn get_prompt_suffix(&self) -> String { - self.tags.iter() - .map(|t| format!(" #{}", t.content().unwrap())) - .chain(self.tags_excluded.iter() - .map(|t| format!(" -#{}", t.content().unwrap()))) - .chain(once(self.state.indicator())) - .chain(self.priority.map(|p| format!(" *{:02}", p))) - .join("") + let mut prompt = String::with_capacity(128); + match self.pubkey { + None => { prompt.push_str(" @ALL"); } + Some(key) => + if key != self.sender.pubkey() { + prompt.push_str(" "); + prompt.push_str(&self.get_username(&key)) + }, + } + for tag in self.tags.iter() { + prompt.push_str(&format!(" #{}", tag.content().unwrap())); + } + for tag in self.tags_excluded.iter() { + prompt.push_str(&format!(" -#{}", tag.content().unwrap())); + } + prompt.push_str(&self.state.indicator()); + self.priority.map(|p| + prompt.push_str(&format!(" *{:02}", p))); + prompt } pub(crate) fn get_task_path(&self, id: Option) -> String { @@ -478,6 +494,7 @@ impl TasksRelay { fn filter(&self, task: &Task) -> bool { self.state.matches(task) && + self.pubkey.is_none_or(|p| p == task.event.pubkey) && self.priority.is_none_or(|prio| { task.priority().unwrap_or(DEFAULT_PRIO) >= prio }) && @@ -641,8 +658,8 @@ impl TasksRelay { Ok(added) } - pub(crate) fn set_filter_author(&mut self, key: PublicKey) -> bool { - self.set_filter(|t| t.event.pubkey == key) + pub(crate) fn set_filter_author(&mut self, key: Option) { + self.pubkey = key } pub(crate) fn set_filter_from(&mut self, time: Timestamp) -> bool {