From d15900434099f60c647922551f539df5ee4276a2 Mon Sep 17 00:00:00 2001 From: xeruf <27jf@pm.me> Date: Mon, 25 Nov 2024 14:23:05 +0100 Subject: [PATCH] feat: also match user filter to hashtag and position better in prompt --- src/hashtag.rs | 5 ++++- src/main.rs | 5 +++-- src/tasks.rs | 25 +++++++++++++++---------- 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/src/hashtag.rs b/src/hashtag.rs index 8f1f200..a69ec9a 100644 --- a/src/hashtag.rs +++ b/src/hashtag.rs @@ -17,9 +17,12 @@ pub struct Hashtag { } impl Hashtag { - pub fn matches(&self, token: &str) -> bool { + pub fn contains(&self, token: &str) -> bool { self.lowercased.contains(&token.to_ascii_lowercase()) } + pub fn matches(&self, token: &str) -> bool { + token.contains(&self.lowercased) + } } impl Display for Hashtag { diff --git a/src/main.rs b/src/main.rs index b95a762..3566520 100644 --- a/src/main.rs +++ b/src/main.rs @@ -282,8 +282,9 @@ async fn main() -> Result<()> { println!(); let tasks = relays.get(&selected_relay).unwrap(); let prompt = format!( - "{} {}{}{}", + "{}{} {}{}{}", selected_relay.as_ref().map_or(LOCAL_RELAY_NAME.to_string(), |url| url.to_string()).dimmed(), + tasks.pubkey_str().map_or(String::new(), |s| format!(" @{s}")), tasks.get_task_path(tasks.get_position()).bold(), tasks.get_prompt_suffix().italic(), "❯ ".dimmed() @@ -728,7 +729,7 @@ async fn main() -> Result<()> { tasks.get_filtered(pos, |t| { transform(&t.event.content).contains(&remaining) || t.list_hashtags().any( - |tag| tag.matches(&remaining)) + |tag| tag.contains(&remaining)) }); if filtered.len() == 1 { tasks.move_to(filtered.into_iter().next()); diff --git a/src/tasks.rs b/src/tasks.rs index 7b7db73..64e78ac 100644 --- a/src/tasks.rs +++ b/src/tasks.rs @@ -399,18 +399,22 @@ impl TasksRelay { .and_then(|t| t.parent_id()) } + pub(crate) fn pubkey_str(&self) -> Option { + match self.pubkey { + None => { Some("ALL".to_string()) } + Some(key) => + if key != self.sender.pubkey() { + Some(self.users.get_username(&key)) + } else { + None + }, + } + } + // TODO test with context elements /// Visual representation of current context pub(crate) fn get_prompt_suffix(&self) -> String { 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.users.get_username(&key)) - }, - } for tag in self.tags.iter() { prompt.push_str(&format!(" #{}", tag)); } @@ -509,7 +513,8 @@ impl TasksRelay { fn filter(&self, task: &Task) -> bool { self.state.matches(task) && - (!task.is_task() || self.pubkey.is_none_or(|p| p == task.get_owner())) && + (!task.is_task() || self.pubkey.is_none_or(|p| p == task.get_owner() || + task.list_hashtags().any(|t| t.matches(&self.users.get_username(&p))))) && self.priority.is_none_or(|prio| { task.priority().unwrap_or(DEFAULT_PRIO) >= prio }) && @@ -816,7 +821,7 @@ impl TasksRelay { pub(crate) fn remove_tag(&mut self, tag: &str) { self.view.clear(); let len = self.tags.len(); - self.tags.retain(|t| !t.matches(tag)); + self.tags.retain(|t| !t.contains(tag)); if self.tags.len() < len { info!("Removed tag filters containing {tag}"); } else {