Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option for whole-word search #32

Merged
merged 11 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ client = ["dep:reqwest", "dep:url"]
server = ["dep:actix-cors", "dep:actix-web"]

[dependencies]
regex = "1.10.0"
elliottslaughter marked this conversation as resolved.
Show resolved Hide resolved
percentage = "0.1.0"
egui = "0.22.0"
egui_extras = "0.22.0"
Expand Down
24 changes: 22 additions & 2 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use egui::{
};
use egui_extras::{Column, TableBuilder};
use percentage::{Percentage, PercentageInteger};
use regex::Regex;
use serde::{Deserialize, Serialize};

use crate::data::{
Expand Down Expand Up @@ -111,6 +112,8 @@ struct SearchState {
query: String,
last_query: String,
include_collapsed_entries: bool,
whole_word: bool,
last_whole_word: bool,
last_include_collapsed_entries: bool,
search_field: FieldID,
last_search_field: FieldID,
Expand Down Expand Up @@ -1112,6 +1115,8 @@ impl SearchState {
query: "".to_owned(),
last_query: "".to_owned(),
include_collapsed_entries: false,
whole_word: false,
last_whole_word: false,
last_include_collapsed_entries: false,
search_field: title_id,
last_search_field: title_id,
Expand Down Expand Up @@ -1144,6 +1149,12 @@ impl SearchState {
self.last_search_field = self.search_field;
}

// Invalidate when the whole word setting changes.
if self.whole_word != self.last_whole_word {
invalidate = true;
self.last_whole_word = self.whole_word;
}

// Invalidate when EXCLUDING collapsed entries. (I.e., because the
// searched set shrinks. Growing is ok because search is monotonic.)
if self.include_collapsed_entries != self.last_include_collapsed_entries
Expand All @@ -1165,7 +1176,12 @@ impl SearchState {
}

fn is_string_match(&self, s: &str) -> bool {
s.contains(&self.query)
match self.whole_word {
elliottslaughter marked this conversation as resolved.
Show resolved Hide resolved
true => Regex::new(format!("\\b{}\\b", self.query).as_str())
elliottslaughter marked this conversation as resolved.
Show resolved Hide resolved
.unwrap()
.is_match(s),
false => s.contains(&self.query),
}
}

fn is_field_match(&self, field: &Field) -> bool {
Expand All @@ -1180,7 +1196,7 @@ impl SearchState {
fn is_match(&self, item: &ItemMeta) -> bool {
let field = self.search_field;
if field == self.title_field {
item.title.contains(&self.query)
self.is_string_match(&item.title)
} else if let Some((_, value)) = item.fields.iter().find(|(x, _)| *x == field) {
self.is_field_match(value)
} else {
Expand Down Expand Up @@ -1583,6 +1599,10 @@ impl Window {
}
});
});
ui.checkbox(
&mut self.config.search_state.whole_word,
"whole-word matches only",
elliottslaughter marked this conversation as resolved.
Show resolved Hide resolved
);
ui.checkbox(
&mut self.config.search_state.include_collapsed_entries,
"Include collapsed processors",
Expand Down
Loading