Radish alpha
r
Radicle terminal user interface
Radicle
Git (anonymous pull)
Log in to clone via SSH
patch-select: Add support for more operations
Erik Kundt committed 2 years ago
commit b5ccd3f297cd49cd051b387d8a7669a76543ed69
parent 33f9f3584a351892a4008dfe7042590847c5fba5
3 files changed +89 -60
modified 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<PatchOperation>, id: PatchId) -> Self {
+
        Self { operation, id }
+
    }
+
}
+

impl Display for Output {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match &self.operation {
modified bin/commands/patch/select/event.rs
@@ -32,6 +32,17 @@ impl tuirealm::Component<Message, NoUserEvent> for Widget<GlobalListener> {

impl tuirealm::Component<Message, NoUserEvent> for Widget<IdSelect> {
    fn on(&mut self, event: Event<NoUserEvent>) -> Option<Message> {
+
        let mut submit = || -> Option<radicle::cob::patch::PatchId> {
+
            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<Message, NoUserEvent> for Widget<IdSelect> {
            }
            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<Message, NoUserEvent> for Widget<IdSelect> {

impl tuirealm::Component<Message, NoUserEvent> for Widget<OperationSelect> {
    fn on(&mut self, event: Event<NoUserEvent>) -> Option<Message> {
+
        let mut submit = || -> Option<radicle::cob::patch::PatchId> {
+
            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<Message, NoUserEvent> for Widget<OperationSelect> {
            }
            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,
        }
    }
modified 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"),
                ],
            ),