Skip to content

Commit

Permalink
Add option for whole-word search (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
bryevdv authored Oct 20, 2023
1 parent 9d587fb commit 665d9ec
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 12 deletions.
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.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ rand = { version = "0.8" }
# transitive depedency, required for rand to support wasm
getrandom = { version = "0.2", features = ["js"] }

regex = "1.10.0"


# client
url = { version = "2", optional = true }
Expand Down
41 changes: 35 additions & 6 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::{escape, Regex};
use serde::{Deserialize, Serialize};

use crate::data::{
Expand Down Expand Up @@ -110,10 +111,13 @@ struct SearchState {
// Search parameters
query: String,
last_query: String,
include_collapsed_entries: bool,
last_include_collapsed_entries: bool,
search_field: FieldID,
last_search_field: FieldID,
whole_word: bool,
last_whole_word: bool,
last_word_regex: Option<Regex>,
include_collapsed_entries: bool,
last_include_collapsed_entries: bool,
last_view_interval: Option<Interval>,

// Cache of matching items
Expand Down Expand Up @@ -1111,10 +1115,13 @@ impl SearchState {

query: "".to_owned(),
last_query: "".to_owned(),
include_collapsed_entries: false,
last_include_collapsed_entries: false,
search_field: title_id,
last_search_field: title_id,
whole_word: false,
last_whole_word: false,
last_word_regex: None,
include_collapsed_entries: false,
last_include_collapsed_entries: false,
last_view_interval: None,

result_set: BTreeSet::new(),
Expand Down Expand Up @@ -1144,6 +1151,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 @@ -1160,12 +1173,24 @@ impl SearchState {
}

if invalidate {
if self.whole_word {
let regex_string = format!("\\b{}\\b", escape(&self.query));
self.last_word_regex = Some(Regex::new(&regex_string).unwrap());
}

self.clear();
}
}

fn is_string_match(&self, s: &str) -> bool {
s.contains(&self.query)
if self.whole_word {
let Some(regex) = &self.last_word_regex else {
unreachable!();
};
regex.is_match(s)
} else {
s.contains(&self.query)
}
}

fn is_field_match(&self, field: &Field) -> bool {
Expand All @@ -1180,7 +1205,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 +1608,10 @@ impl Window {
}
});
});
ui.checkbox(
&mut self.config.search_state.whole_word,
"Match whole words only",
);
ui.checkbox(
&mut self.config.search_state.include_collapsed_entries,
"Include collapsed processors",
Expand Down

0 comments on commit 665d9ec

Please sign in to comment.