Skip to content

Commit 6b0da23

Browse files
committed
feat: store the applied version of the filter
1 parent 4b6ad38 commit 6b0da23

File tree

3 files changed

+58
-11
lines changed

3 files changed

+58
-11
lines changed

src/app.rs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ pub struct LogViewerApp {
3232
track_item_align: Option<Align>,
3333
shortcuts: Shortcuts,
3434
should_scroll_to_end_on_load: bool,
35-
// TODO 4: Add UI to set / unset field
35+
// TODO 4: Add UI to set / unset row_idx_field_name
3636
/// When set adds a field with this name and populates it with the row numbers
3737
row_idx_field_name: Option<String>,
3838
/// Allows the user to dim the warning by clicking on it
@@ -492,6 +492,9 @@ impl LogViewerApp {
492492
ui.separator();
493493
self.filtering_ui(ui);
494494
});
495+
ui.horizontal(|ui| {
496+
self.unfilter_ui(ui);
497+
});
495498
}
496499

497500
fn filtering_ui(&mut self, ui: &mut egui::Ui) {
@@ -508,15 +511,8 @@ impl LogViewerApp {
508511
}
509512
}
510513
let mut should_apply_filter = false;
511-
if is_filter_enabled {
512-
if shortcut_button(ui, "Apply", "", &self.shortcuts.apply_filter) {
513-
should_apply_filter = true;
514-
}
515-
if data.is_filtered()
516-
&& shortcut_button(ui, "Unfilter", "Clears Filter", &self.shortcuts.unfilter)
517-
{
518-
data.unfilter();
519-
}
514+
if is_filter_enabled && shortcut_button(ui, "Apply", "", &self.shortcuts.apply_filter) {
515+
should_apply_filter = true;
520516
}
521517

522518
if let Some(filter) = data.filter.as_mut() {
@@ -689,6 +685,18 @@ impl LogViewerApp {
689685
self.should_focus_search = true;
690686
}
691687
}
688+
689+
fn unfilter_ui(&mut self, ui: &mut egui::Ui) {
690+
if let Some(data) = self.data.as_mut() {
691+
if data.is_filtered() {
692+
ui.label(format!("Applied Filter: {}", data.applied_filter_display()));
693+
ui.separator();
694+
if shortcut_button(ui, "Unfilter", "Clears Filter", &self.shortcuts.unfilter) {
695+
data.unfilter();
696+
}
697+
}
698+
}
699+
}
692700
}
693701

694702
#[cfg(not(target_arch = "wasm32"))]

src/app/data.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ pub struct Data {
2323
pub filter: Option<FilterConfig>,
2424
rows: Vec<LogRow>,
2525
filtered_rows: Option<Vec<usize>>,
26+
applied_filter: Option<FilterConfig>,
2627
}
2728

2829
#[derive(serde::Deserialize, serde::Serialize, Default, Debug, PartialEq, Eq, Clone)]
@@ -174,7 +175,7 @@ impl Data {
174175
// Collect other needed info before taking mutable borrow to appease the borrow checker (couldn't find another readable way)
175176
let is_filtered = self.is_filtered();
176177
let filter = if is_filtered {
177-
self.filter.clone()
178+
self.applied_filter.clone()
178179
} else {
179180
None
180181
};
@@ -235,12 +236,14 @@ impl Data {
235236
}
236237

237238
pub fn is_filtered(&self) -> bool {
239+
debug_assert_eq!(self.applied_filter.is_some(), self.filtered_rows.is_some());
238240
self.filtered_rows.is_some()
239241
}
240242

241243
pub fn unfilter(&mut self) {
242244
let previous_real_index_selected = self.selected_row.map(|x| self.get_real_index(x));
243245
self.filtered_rows = None;
246+
self.applied_filter = None;
244247
if let Some(old_selected) = previous_real_index_selected {
245248
self.selected_row = Some(old_selected);
246249
}
@@ -250,6 +253,7 @@ impl Data {
250253
if let Some(filter) = self.filter.as_ref() {
251254
let previous_real_index_selected = self.selected_row.map(|x| self.get_real_index(x));
252255

256+
self.applied_filter = self.filter.clone();
253257
self.filtered_rows = Some(
254258
self.rows
255259
.iter_mut()
@@ -285,6 +289,26 @@ impl Data {
285289
}
286290
}
287291
}
292+
293+
pub fn applied_filter_display(&self) -> String {
294+
let Some(FilterConfig {
295+
search_key,
296+
filter_on,
297+
is_case_sensitive,
298+
comparator,
299+
}) = self.applied_filter.as_ref()
300+
else {
301+
debug_assert!(false, "We really shouldn't end up here");
302+
return "No Filter Applied".to_string();
303+
};
304+
format!(
305+
"Search Key: {search_key} | Filter On: {filter_on} | Case Sensitive: {} | Comparator: {comparator}",
306+
if *is_case_sensitive {
307+
"Yes"
308+
} else {
309+
"No"
310+
})
311+
}
288312
}
289313

290314
/// If the slice of fields and values matches the filter then the indices of the fields that match are returned or None if it does not match

src/app/data/filter.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,18 @@ impl Display for Comparator {
8686
)
8787
}
8888
}
89+
90+
impl Display for FilterOn {
91+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
92+
match self {
93+
FilterOn::Any => write!(f, "Any"),
94+
FilterOn::Field(name) => write!(f, "[Field Named: {name}]"),
95+
}
96+
}
97+
}
98+
99+
impl Display for FieldSpecifier {
100+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
101+
self.name.fmt(f)
102+
}
103+
}

0 commit comments

Comments
 (0)