From 07b768d0bef49516057e63ee589621d14fcd4828 Mon Sep 17 00:00:00 2001 From: kjuulh Date: Fri, 10 May 2024 23:23:47 +0200 Subject: [PATCH] feat: implement filter Signed-off-by: kjuulh --- .../src/components/graph_explorer.rs | 20 +++++++++++ .../src/components/movement_graph.rs | 35 ++++++++++++------- 2 files changed, 42 insertions(+), 13 deletions(-) diff --git a/crates/hyperlog-tui/src/components/graph_explorer.rs b/crates/hyperlog-tui/src/components/graph_explorer.rs index 6076647..93c9449 100644 --- a/crates/hyperlog-tui/src/components/graph_explorer.rs +++ b/crates/hyperlog-tui/src/components/graph_explorer.rs @@ -12,6 +12,23 @@ use super::{ render_graph::summarize::SummarizeRenderGraph, }; +#[derive(Clone)] +pub enum FilterBy { + NotDone, + None, +} + +impl Default for FilterBy { + fn default() -> Self { + Self::NotDone + } +} + +#[derive(Default, Clone)] +pub struct DisplayOptions { + pub filter_by: FilterBy, +} + pub struct GraphExplorer<'a> { state: SharedState, @@ -24,6 +41,8 @@ pub struct GraphExplorerState<'a> { current_path: Option<&'a str>, current_position: Vec, + display_options: DisplayOptions, + graph: Option, } @@ -36,6 +55,7 @@ impl<'a> GraphExplorer<'a> { current_path: None, current_position: Vec::new(), graph: None, + display_options: DisplayOptions::default(), }, } } diff --git a/crates/hyperlog-tui/src/components/movement_graph.rs b/crates/hyperlog-tui/src/components/movement_graph.rs index 5e04358..bb13d09 100644 --- a/crates/hyperlog-tui/src/components/movement_graph.rs +++ b/crates/hyperlog-tui/src/components/movement_graph.rs @@ -1,8 +1,8 @@ -use std::ops::Deref; - use hyperlog_core::log::{GraphItem, ItemState}; use itertools::Itertools; +use super::graph_explorer::{DisplayOptions, FilterBy}; + #[derive(PartialEq, Eq, Debug, Clone)] pub enum GraphItemType { Section, @@ -109,28 +109,31 @@ impl MovementGraph { None => Vec::new(), } } -} -impl From> for MovementGraph { - fn from(value: Box) -> Self { - value.deref().clone().into() - } -} - -impl From for MovementGraph { - fn from(value: GraphItem) -> Self { + pub fn new(graph_item: GraphItem, display_options: &DisplayOptions) -> MovementGraph { let mut graph = MovementGraph::default(); - match value { + match graph_item { GraphItem::User(sections) | GraphItem::Section(sections) => { let graph_items = sections .iter() .sorted_by(|(a, _), (b, _)| Ord::cmp(a, b)) + .filter(|(_, item)| { + if let GraphItem::Item { state, .. } = item { + if matches!(display_options.filter_by, FilterBy::NotDone) + && matches!(state, ItemState::Done) + { + return false; + } + } + + true + }) .enumerate() .map(|(i, (key, value))| MovementGraphItem { index: i, name: key.clone(), - values: value.clone().into(), + values: Self::new(value.clone(), display_options), item_type: match value { GraphItem::User(_) => GraphItemType::Section, GraphItem::Section(_) => GraphItemType::Section, @@ -150,6 +153,12 @@ impl From for MovementGraph { } } +impl From for MovementGraph { + fn from(value: GraphItem) -> Self { + MovementGraph::new(value, &DisplayOptions::default()) + } +} + #[cfg(test)] mod test { use std::collections::BTreeMap;