feat: enable setting pubkey as context and auto-filter for own

This commit is contained in:
xeruf 2024-11-20 23:16:57 +01:00
parent b87970d4e2
commit e320523fc0
3 changed files with 41 additions and 23 deletions

View file

@ -45,6 +45,7 @@ Task:
- `time` - time tracked on this task by you - `time` - time tracked on this task by you
Utilities: Utilities:
- `state` - indicator of current progress - `state` - indicator of current progress
- `owner` - author or task assignee
- `rtime` - time tracked on this tasks and its subtree by everyone - `rtime` - time tracked on this tasks and its subtree by everyone
- `progress` - recursive subtask completion in percent - `progress` - recursive subtask completion in percent
- `subtasks` - how many direct subtasks are complete - `subtasks` - how many direct subtasks are complete

View file

@ -453,37 +453,37 @@ async fn main() -> Result<()> {
} }
Some('@') => { Some('@') => {
let success = match arg { match arg {
None => { None => {
let today = Timestamp::now() - 80_000; let today = Timestamp::now() - 80_000;
info!("Filtering for tasks from the last 22 hours"); info!("Filtering for tasks from the last 22 hours");
tasks.set_filter_from(today) if !tasks.set_filter_from(today) {
continue 'repl;
}
} }
Some(arg) => { Some(arg) => {
if arg == "@" { if arg == "@" {
info!("Filtering for own tasks"); info!("Showing everybody's tasks");
tasks.set_filter_author(keys.public_key()) tasks.set_filter_author(None)
} else if let Ok(key) = PublicKey::from_str(arg) { } else if let Ok(key) = PublicKey::from_str(arg) {
let author = tasks.get_username(&key); info!("Showing {}'s tasks", tasks.get_username(&key));
info!("Filtering for tasks by {author}"); tasks.set_filter_author(Some(key))
tasks.set_filter_author(key)
} else if let Some((key, meta)) = tasks.find_user(arg) { } 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()))); info!("Showing {}'s tasks", meta.display_name.as_ref().unwrap_or(meta.name.as_ref().unwrap_or(&key.to_string())));
tasks.set_filter_author(key.clone()) tasks.set_filter_author(Some(key.clone()))
} else { } else {
parse_hour(arg, 1) if parse_hour(arg, 1)
.or_else(|| parse_date(arg).map(|utc| utc.with_timezone(&Local))) .or_else(|| parse_date(arg).map(|utc| utc.with_timezone(&Local)))
.map(|time| { .map(|time| {
info!("Filtering for tasks from {}", format_datetime_relative(time)); info!("Filtering for tasks from {}", format_datetime_relative(time));
tasks.set_filter_from(time.to_timestamp()) tasks.set_filter_from(time.to_timestamp())
}) })
.unwrap_or(false) .is_none_or(|b| !b) {
continue 'repl;
}
} }
} }
}; };
if !success {
continue 'repl;
}
} }
Some('*') => { Some('*') => {

View file

@ -79,6 +79,7 @@ pub(crate) struct TasksRelay {
state: StateFilter, state: StateFilter,
/// Current priority for filtering and new tasks /// Current priority for filtering and new tasks
priority: Option<Prio>, priority: Option<Prio>,
pubkey: Option<PublicKey>,
sender: EventSender, sender: EventSender,
overflow: VecDeque<Event>, overflow: VecDeque<Event>,
@ -173,6 +174,7 @@ impl TasksRelay {
tags_excluded: Default::default(), tags_excluded: Default::default(),
state: Default::default(), state: Default::default(),
priority: None, priority: None,
pubkey: Some(sender.pubkey()),
search_depth: 4, search_depth: 4,
view_depth: 0, view_depth: 0,
@ -382,14 +384,28 @@ impl TasksRelay {
.and_then(|t| t.parent_id()) .and_then(|t| t.parent_id())
} }
// TODO test with context elements
/// Visual representation of current context
pub(crate) fn get_prompt_suffix(&self) -> String { pub(crate) fn get_prompt_suffix(&self) -> String {
self.tags.iter() let mut prompt = String::with_capacity(128);
.map(|t| format!(" #{}", t.content().unwrap())) match self.pubkey {
.chain(self.tags_excluded.iter() None => { prompt.push_str(" @ALL"); }
.map(|t| format!(" -#{}", t.content().unwrap()))) Some(key) =>
.chain(once(self.state.indicator())) if key != self.sender.pubkey() {
.chain(self.priority.map(|p| format!(" *{:02}", p))) prompt.push_str(" ");
.join("") 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<EventId>) -> String { pub(crate) fn get_task_path(&self, id: Option<EventId>) -> String {
@ -478,6 +494,7 @@ impl TasksRelay {
fn filter(&self, task: &Task) -> bool { fn filter(&self, task: &Task) -> bool {
self.state.matches(task) && self.state.matches(task) &&
self.pubkey.is_none_or(|p| p == task.event.pubkey) &&
self.priority.is_none_or(|prio| { self.priority.is_none_or(|prio| {
task.priority().unwrap_or(DEFAULT_PRIO) >= prio task.priority().unwrap_or(DEFAULT_PRIO) >= prio
}) && }) &&
@ -641,8 +658,8 @@ impl TasksRelay {
Ok(added) Ok(added)
} }
pub(crate) fn set_filter_author(&mut self, key: PublicKey) -> bool { pub(crate) fn set_filter_author(&mut self, key: Option<PublicKey>) {
self.set_filter(|t| t.event.pubkey == key) self.pubkey = key
} }
pub(crate) fn set_filter_from(&mut self, time: Timestamp) -> bool { pub(crate) fn set_filter_from(&mut self, time: Timestamp) -> bool {