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
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

View file

@ -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('*') => {

View file

@ -79,6 +79,7 @@ pub(crate) struct TasksRelay {
state: StateFilter,
/// Current priority for filtering and new tasks
priority: Option<Prio>,
pubkey: Option<PublicKey>,
sender: EventSender,
overflow: VecDeque<Event>,
@ -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<EventId>) -> 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<PublicKey>) {
self.pubkey = key
}
pub(crate) fn set_filter_from(&mut self, time: Timestamp) -> bool {