diff --git a/bin/commands/patch/select.rs b/bin/commands/patch/select.rs index 95088b1c..19b45870 100644 --- a/bin/commands/patch/select.rs +++ b/bin/commands/patch/select.rs @@ -66,8 +66,12 @@ pub enum Subject { #[derive(Clone, Debug, Eq, PartialEq, Serialize)] pub enum PatchOperation { Show, - Edit, + Update, Checkout, + Review, + Delete, + Edit, + Comment, } impl Display for PatchOperation { @@ -76,12 +80,24 @@ impl Display for PatchOperation { PatchOperation::Show => { write!(f, "show") } - PatchOperation::Edit => { - write!(f, "edit") + PatchOperation::Update => { + write!(f, "update") } PatchOperation::Checkout => { write!(f, "checkout") } + PatchOperation::Review => { + write!(f, "review") + } + PatchOperation::Delete => { + write!(f, "delete") + } + PatchOperation::Edit => { + write!(f, "edit") + } + PatchOperation::Comment => { + write!(f, "comment") + } } } } @@ -94,6 +110,12 @@ pub struct Output { id: PatchId, } +impl Output { + pub fn new(operation: Option, id: PatchId) -> Self { + Self { operation, id } + } +} + impl Display for Output { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match &self.operation { diff --git a/bin/commands/patch/select/event.rs b/bin/commands/patch/select/event.rs index ce018ce8..a96f8dd5 100644 --- a/bin/commands/patch/select/event.rs +++ b/bin/commands/patch/select/event.rs @@ -32,6 +32,17 @@ impl tuirealm::Component for Widget { impl tuirealm::Component for Widget { fn on(&mut self, event: Event) -> Option { + let mut submit = || -> Option { + let result = self.perform(Cmd::Submit); + match result { + CmdResult::Submit(State::One(StateValue::Usize(selected))) => { + let item = self.items().get(selected)?; + Some(item.id().to_owned()) + } + _ => None, + } + }; + match event { Event::Keyboard(KeyEvent { code: Key::Up, .. }) | Event::Keyboard(KeyEvent { @@ -53,20 +64,10 @@ impl tuirealm::Component for Widget { } Event::Keyboard(KeyEvent { code: Key::Enter, .. - }) => { - let result = self.perform(Cmd::Submit); - match result { - CmdResult::Submit(State::One(StateValue::Usize(selected))) => { - let item = self.items().get(selected)?; - let output = Output { - operation: None, - id: PatchId::from(item.id().to_owned()), - }; - Some(Message::Quit(Some(output))) - } - _ => None, - } - } + }) => submit().map(|id| { + let output = Output::new(None, PatchId::from(id)); + Message::Quit(Some(output)) + }), _ => None, } } @@ -74,6 +75,17 @@ impl tuirealm::Component for Widget { impl tuirealm::Component for Widget { fn on(&mut self, event: Event) -> Option { + let mut submit = || -> Option { + let result = self.perform(Cmd::Submit); + match result { + CmdResult::Submit(State::One(StateValue::Usize(selected))) => { + let item = self.items().get(selected)?; + Some(item.id().to_owned()) + } + _ => None, + } + }; + match event { Event::Keyboard(KeyEvent { code: Key::Up, .. }) | Event::Keyboard(KeyEvent { @@ -95,54 +107,45 @@ impl tuirealm::Component for Widget { } Event::Keyboard(KeyEvent { code: Key::Enter, .. - }) => { - let result = self.perform(Cmd::Submit); - match result { - CmdResult::Submit(State::One(StateValue::Usize(selected))) => { - let item = self.items().get(selected)?; - let output = Output { - operation: Some(PatchOperation::Show), - id: PatchId::from(item.id().to_owned()), - }; - Some(Message::Quit(Some(output))) - } - _ => None, - } - } + }) => submit().map(|id| { + let output = Output::new(Some(PatchOperation::Show), PatchId::from(id)); + Message::Quit(Some(output)) + }), Event::Keyboard(KeyEvent { - code: Key::Char('e'), + code: Key::Char('u'), .. - }) => { - let result = self.perform(Cmd::Submit); - match result { - CmdResult::Submit(State::One(StateValue::Usize(selected))) => { - let item = self.items().get(selected)?; - let output = Output { - operation: Some(PatchOperation::Edit), - id: PatchId::from(item.id().to_owned()), - }; - Some(Message::Quit(Some(output))) - } - _ => None, - } - } + }) => submit().map(|id| { + let output = Output::new(Some(PatchOperation::Update), PatchId::from(id)); + Message::Quit(Some(output)) + }), Event::Keyboard(KeyEvent { code: Key::Char('c'), .. - }) => { - let result = self.perform(Cmd::Submit); - match result { - CmdResult::Submit(State::One(StateValue::Usize(selected))) => { - let item = self.items().get(selected)?; - let output = Output { - operation: Some(PatchOperation::Checkout), - id: PatchId::from(item.id().to_owned()), - }; - Some(Message::Quit(Some(output))) - } - _ => None, - } - } + }) => submit().map(|id| { + let output = Output::new(Some(PatchOperation::Checkout), PatchId::from(id)); + Message::Quit(Some(output)) + }), + Event::Keyboard(KeyEvent { + code: Key::Char('d'), + .. + }) => submit().map(|id| { + let output = Output::new(Some(PatchOperation::Delete), PatchId::from(id)); + Message::Quit(Some(output)) + }), + Event::Keyboard(KeyEvent { + code: Key::Char('e'), + .. + }) => submit().map(|id| { + let output = Output::new(Some(PatchOperation::Edit), PatchId::from(id)); + Message::Quit(Some(output)) + }), + Event::Keyboard(KeyEvent { + code: Key::Char('m'), + .. + }) => submit().map(|id| { + let output = Output::new(Some(PatchOperation::Comment), PatchId::from(id)); + Message::Quit(Some(output)) + }), _ => None, } } diff --git a/bin/commands/patch/select/ui.rs b/bin/commands/patch/select/ui.rs index ba549733..0c718c0d 100644 --- a/bin/commands/patch/select/ui.rs +++ b/bin/commands/patch/select/ui.rs @@ -92,8 +92,12 @@ impl OperationSelect { vec![ tui::ui::shortcut(&self.theme, "↑/↓", "navigate"), tui::ui::shortcut(&self.theme, "enter", "show"), + tui::ui::shortcut(&self.theme, "u", "update"), tui::ui::shortcut(&self.theme, "c", "checkout"), + tui::ui::shortcut(&self.theme, "r", "review"), + tui::ui::shortcut(&self.theme, "d", "delete"), tui::ui::shortcut(&self.theme, "e", "edit"), + tui::ui::shortcut(&self.theme, "m", "comment"), tui::ui::shortcut(&self.theme, "q", "quit"), ], ),