feat: activate perfect match and filter without dot

This commit is contained in:
xeruf 2024-08-01 21:11:33 +03:00
parent 5b05c53947
commit 03f9e60c6f
2 changed files with 48 additions and 41 deletions

View File

@ -340,7 +340,7 @@ async fn main() {
Some('#') | Some('+') => {
tasks.add_tag(arg.to_string());
}
Some('-') => {
tasks.remove_tag(arg.to_string())
}
@ -360,50 +360,13 @@ async fn main() {
if let Ok(depth) = slice.parse::<i8>() {
tasks.move_to(pos);
tasks.depth = depth;
continue;
}
pos = EventId::parse(slice).ok().or_else(|| {
// TODO rebuild and use for plaintext too
let mut filtered: Vec<EventId> = tasks
.current_tasks()
.into_iter()
.filter(|t| t.event.content.starts_with(slice))
.map(|t| t.event.id)
.collect();
if filtered.is_empty() {
let lowercase = slice.to_ascii_lowercase();
filtered = tasks
.current_tasks()
.into_iter()
.filter(|t| {
t.event.content.to_ascii_lowercase().starts_with(&lowercase)
})
.map(|t| t.event.id)
.collect();
}
match filtered.len() {
0 => {
// No match, new task
Some(tasks.make_task(slice))
}
1 => {
// One match, activate
Some(filtered.first().unwrap().clone())
}
_ => {
// Multiple match, filter
tasks.set_filter(filtered);
None
}
}
});
if pos != None {
tasks.move_to(pos);
} else {
tasks.filter_or_create(slice).map(|id| tasks.move_to(Some(id)));
}
}
_ => {
tasks.make_task(&input);
tasks.filter_or_create(&input);
}
}
}

View File

@ -389,6 +389,50 @@ impl Tasks {
self.sender.flush();
}
/// Finds out what to do with the given string.
/// Returns an EventId when a new Task was created.
pub(crate) fn filter_or_create(&mut self, arg: &str) -> Option<EventId> {
if let Ok(id) = EventId::parse(arg) {
self.move_to(Some(id));
return None;
}
let tasks = self.current_tasks();
let mut filtered: Vec<EventId> = Vec::with_capacity(tasks.len());
let lowercase_arg = arg.to_ascii_lowercase();
let mut filtered_more: Vec<EventId> = Vec::with_capacity(tasks.len());
for task in tasks {
let lowercase = task.event.content.to_ascii_lowercase();
if lowercase == lowercase_arg {
self.move_to(Some(task.event.id));
return None
} else if task.event.content.starts_with(arg) {
filtered.push(task.event.id)
} else if lowercase.starts_with(&lowercase_arg) {
filtered_more.push(task.event.id)
}
}
if filtered.len() == 0 {
filtered = filtered_more
}
match filtered.len() {
0 => {
// No match, new task
self.view.clear();
Some(self.make_task(arg))
}
1 => {
// One match, activate
self.move_to(filtered.into_iter().nth(0));
None
}
_ => {
// Multiple match, filter
self.set_filter(filtered);
None
}
}
}
pub(crate) fn move_to(&mut self, id: Option<EventId>) {
self.view.clear();
if id == self.position {