From bc59201fb58983aeed3dcf461b2368e1c90aa470 Mon Sep 17 00:00:00 2001 From: Erik Kundt Date: Wed, 3 Apr 2024 12:16:35 +0200 Subject: [PATCH] patch: Fix and clean up selection state --- bin/commands/patch/flux/select.rs | 9 --- bin/commands/patch/flux/select/ui.rs | 106 ++++++++++++++------------- 2 files changed, 55 insertions(+), 60 deletions(-) diff --git a/bin/commands/patch/flux/select.rs b/bin/commands/patch/flux/select.rs index 32beb661..99ece82d 100644 --- a/bin/commands/patch/flux/select.rs +++ b/bin/commands/patch/flux/select.rs @@ -53,7 +53,6 @@ impl Default for UIState { #[derive(Clone, Debug)] pub struct State { patches: Vec, - selected: Option, mode: Mode, search: store::StateValue, ui: UIState, @@ -73,11 +72,8 @@ impl TryFrom<&Context> for State { } } - let selected = items.first().cloned(); - Ok(Self { patches: items, - selected, mode: context.mode.clone(), search: store::StateValue::new(context.filter.to_string()), ui: UIState::default(), @@ -87,7 +83,6 @@ impl TryFrom<&Context> for State { pub enum Action { Exit { selection: Option }, - Select { item: PatchItem }, PageSize(usize), OpenSearch, UpdateSearch { value: String }, @@ -103,10 +98,6 @@ impl store::State for State { fn handle_action(&mut self, action: Action) -> Option> { match action { Action::Exit { selection } => Some(Exit { value: selection }), - Action::Select { item } => { - self.selected = Some(item); - None - } Action::PageSize(size) => { self.ui.page_size = size; None diff --git a/bin/commands/patch/flux/select/ui.rs b/bin/commands/patch/flux/select/ui.rs index 3190cdb2..3ba625c7 100644 --- a/bin/commands/patch/flux/select/ui.rs +++ b/bin/commands/patch/flux/select/ui.rs @@ -32,7 +32,6 @@ use crate::tui_patch::common::PatchOperation; use super::{Action, State}; pub struct ListPageProps { - selected: Option, mode: Mode, show_search: bool, show_help: bool, @@ -41,7 +40,6 @@ pub struct ListPageProps { impl From<&State> for ListPageProps { fn from(state: &State) -> Self { Self { - selected: state.selected.clone(), mode: state.mode.clone(), show_search: state.ui.show_search, show_help: state.ui.show_help, @@ -108,45 +106,6 @@ impl<'a> Widget for ListPage<'a> { Key::Esc | Key::Ctrl('c') => { let _ = self.action_tx.send(Action::Exit { selection: None }); } - Key::Char('\n') => { - if let Some(selected) = &self.props.selected { - let operation = match self.props.mode { - Mode::Operation => Some(PatchOperation::Show.to_string()), - Mode::Id => None, - }; - let _ = self.action_tx.send(Action::Exit { - selection: Some(Selection { - operation, - ids: vec![selected.id], - args: vec![], - }), - }); - } - } - Key::Char('c') => { - if let Some(selected) = &self.props.selected { - let selection = Selection { - operation: Some(PatchOperation::Checkout.to_string()), - ids: vec![selected.id], - args: vec![], - }; - let _ = self.action_tx.send(Action::Exit { - selection: Some(selection), - }); - } - } - Key::Char('d') => { - if let Some(selected) = &self.props.selected { - let selection = Selection { - operation: Some(PatchOperation::Diff.to_string()), - ids: vec![selected.id], - args: vec![], - }; - let _ = self.action_tx.send(Action::Exit { - selection: Some(selection), - }); - } - } Key::Char('/') => { let _ = self.action_tx.send(Action::OpenSearch); } @@ -214,6 +173,7 @@ impl<'a> Render<()> for ListPage<'a> { } struct PatchesProps { + mode: Mode, patches: Vec, search: StateValue, stats: HashMap, @@ -263,6 +223,7 @@ impl From<&State> for PatchesProps { ]); Self { + mode: state.mode.clone(), patches, search: state.search.clone(), widths: [ @@ -357,18 +318,61 @@ impl Widget for Patches { Key::End => { self.table.end(self.props.patches.len()); } + Key::Char('\n') => { + let operation = match self.props.mode { + Mode::Operation => Some(PatchOperation::Show.to_string()), + Mode::Id => None, + }; + + self.table + .selected() + .and_then(|selected| self.props.patches.get(selected)) + .and_then(|patch| { + self.action_tx + .send(Action::Exit { + selection: Some(Selection { + operation, + ids: vec![patch.id], + args: vec![], + }), + }) + .ok() + }); + } + Key::Char('c') => { + self.table + .selected() + .and_then(|selected| self.props.patches.get(selected)) + .and_then(|patch| { + self.action_tx + .send(Action::Exit { + selection: Some(Selection { + operation: Some(PatchOperation::Checkout.to_string()), + ids: vec![patch.id], + args: vec![], + }), + }) + .ok() + }); + } + Key::Char('d') => { + self.table + .selected() + .and_then(|selected| self.props.patches.get(selected)) + .and_then(|patch| { + self.action_tx + .send(Action::Exit { + selection: Some(Selection { + operation: Some(PatchOperation::Diff.to_string()), + ids: vec![patch.id], + args: vec![], + }), + }) + .ok() + }); + } _ => {} } - self.table - .selected() - .and_then(|selected| self.props.patches.get(selected)) - .and_then(|patch| { - self.action_tx - .send(Action::Select { - item: patch.clone(), - }) - .ok() - }); } }