Skip to content

Commit

Permalink
feat: store the applied version of the filter
Browse files Browse the repository at this point in the history
  • Loading branch information
c-git committed Sep 11, 2024
1 parent 4b6ad38 commit 6b0da23
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 11 deletions.
28 changes: 18 additions & 10 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub struct LogViewerApp {
track_item_align: Option<Align>,
shortcuts: Shortcuts,
should_scroll_to_end_on_load: bool,
// TODO 4: Add UI to set / unset field
// TODO 4: Add UI to set / unset row_idx_field_name
/// When set adds a field with this name and populates it with the row numbers
row_idx_field_name: Option<String>,
/// Allows the user to dim the warning by clicking on it
Expand Down Expand Up @@ -492,6 +492,9 @@ impl LogViewerApp {
ui.separator();
self.filtering_ui(ui);
});
ui.horizontal(|ui| {
self.unfilter_ui(ui);
});
}

fn filtering_ui(&mut self, ui: &mut egui::Ui) {
Expand All @@ -508,15 +511,8 @@ impl LogViewerApp {
}
}
let mut should_apply_filter = false;
if is_filter_enabled {
if shortcut_button(ui, "Apply", "", &self.shortcuts.apply_filter) {
should_apply_filter = true;
}
if data.is_filtered()
&& shortcut_button(ui, "Unfilter", "Clears Filter", &self.shortcuts.unfilter)
{
data.unfilter();
}
if is_filter_enabled && shortcut_button(ui, "Apply", "", &self.shortcuts.apply_filter) {
should_apply_filter = true;
}

if let Some(filter) = data.filter.as_mut() {
Expand Down Expand Up @@ -689,6 +685,18 @@ impl LogViewerApp {
self.should_focus_search = true;
}
}

fn unfilter_ui(&mut self, ui: &mut egui::Ui) {
if let Some(data) = self.data.as_mut() {
if data.is_filtered() {
ui.label(format!("Applied Filter: {}", data.applied_filter_display()));
ui.separator();
if shortcut_button(ui, "Unfilter", "Clears Filter", &self.shortcuts.unfilter) {
data.unfilter();
}
}
}
}
}

#[cfg(not(target_arch = "wasm32"))]
Expand Down
26 changes: 25 additions & 1 deletion src/app/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub struct Data {
pub filter: Option<FilterConfig>,
rows: Vec<LogRow>,
filtered_rows: Option<Vec<usize>>,
applied_filter: Option<FilterConfig>,
}

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

pub fn is_filtered(&self) -> bool {
debug_assert_eq!(self.applied_filter.is_some(), self.filtered_rows.is_some());
self.filtered_rows.is_some()
}

pub fn unfilter(&mut self) {
let previous_real_index_selected = self.selected_row.map(|x| self.get_real_index(x));
self.filtered_rows = None;
self.applied_filter = None;
if let Some(old_selected) = previous_real_index_selected {
self.selected_row = Some(old_selected);
}
Expand All @@ -250,6 +253,7 @@ impl Data {
if let Some(filter) = self.filter.as_ref() {
let previous_real_index_selected = self.selected_row.map(|x| self.get_real_index(x));

self.applied_filter = self.filter.clone();
self.filtered_rows = Some(
self.rows
.iter_mut()
Expand Down Expand Up @@ -285,6 +289,26 @@ impl Data {
}
}
}

pub fn applied_filter_display(&self) -> String {
let Some(FilterConfig {
search_key,
filter_on,
is_case_sensitive,
comparator,
}) = self.applied_filter.as_ref()
else {
debug_assert!(false, "We really shouldn't end up here");
return "No Filter Applied".to_string();
};
format!(
"Search Key: {search_key} | Filter On: {filter_on} | Case Sensitive: {} | Comparator: {comparator}",
if *is_case_sensitive {
"Yes"
} else {
"No"
})
}
}

/// 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
Expand Down
15 changes: 15 additions & 0 deletions src/app/data/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,18 @@ impl Display for Comparator {
)
}
}

impl Display for FilterOn {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
FilterOn::Any => write!(f, "Any"),
FilterOn::Field(name) => write!(f, "[Field Named: {name}]"),
}
}
}

impl Display for FieldSpecifier {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.name.fmt(f)
}
}

0 comments on commit 6b0da23

Please sign in to comment.