Skip to content

Commit

Permalink
Add option for whole-word search
Browse files Browse the repository at this point in the history
  • Loading branch information
bryevdv committed Oct 12, 2023
1 parent 6417711 commit 9abf17c
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 8 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.

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"
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 {
true => Regex::new(format!("\\b{}\\b", self.query).as_str())
.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",
);
ui.checkbox(
&mut self.config.search_state.include_collapsed_entries,
"Include collapsed processors",
Expand Down

0 comments on commit 9abf17c

Please sign in to comment.