diff --git a/bin/commands/issue/suite.rs b/bin/commands/issue/suite.rs index 21de1c40..353a79fd 100644 --- a/bin/commands/issue/suite.rs +++ b/bin/commands/issue/suite.rs @@ -301,7 +301,7 @@ impl App { } } -impl Tui for App { +impl Tui for App { fn init(&mut self, app: &mut Application) -> Result<()> { self.view_list(app, &self.theme.clone())?; @@ -344,7 +344,7 @@ impl Tui for App { } } - fn exit(&self) -> Option { + fn exit(&self) -> Option> { if self.quit { return Some(Exit { value: None }); } diff --git a/bin/commands/patch.rs b/bin/commands/patch.rs index 56c86f96..41967e32 100644 --- a/bin/commands/patch.rs +++ b/bin/commands/patch.rs @@ -8,6 +8,7 @@ mod select; mod suite; use std::ffi::OsString; +use std::process::Command; use anyhow::anyhow; @@ -16,6 +17,8 @@ use radicle_tui::{context, log, Window}; use crate::terminal; use crate::terminal::args::{Args, Error, Help}; +use self::list::PatchCommand; + pub const FPS: u64 = 60; pub const HELP: Help = Help { name: "patch", @@ -88,11 +91,46 @@ pub fn run(options: Options, _ctx: impl terminal::Context) -> anyhow::Result<()> log::enable(context.profile(), "patch", "list")?; - let patch_id = Window::default() - .run(&mut list::App::new(context), 1000 / FPS)? - .unwrap_or_default(); + let mut app = list::App::new(context); + if let Some(command) = Window::default().run(&mut app, 1000 / FPS)? { + match command { + PatchCommand::Show(id) => { + match Command::new("rad") + .arg("patch") + .arg("show") + .arg(id.to_string()) + .spawn() + { + Ok(_) => {} + Err(_) => {} + } + }, + PatchCommand::Edit(id) => { + match Command::new("rad") + .arg("patch") + .arg("edit") + .arg(id.to_string()) + .spawn() + { + Ok(_) => {} + Err(_) => {} + } + } + PatchCommand::Checkout(id) => { + match Command::new("rad") + .arg("patch") + .arg("checkout") + .arg(id.to_string()) + .spawn() + { + Ok(_) => {} + Err(_) => {} + } + } + } + } - eprint!("{patch_id}"); + // eprint!("{patch_id}"); } Operation::Select => { let context = context::Context::new(id)?.with_patches(); diff --git a/bin/commands/patch/list.rs b/bin/commands/patch/list.rs index 8b2841b7..ef7c3061 100644 --- a/bin/commands/patch/list.rs +++ b/bin/commands/patch/list.rs @@ -24,6 +24,13 @@ use tui::{Exit, PageStack, Tui}; use page::ListView; +#[derive(Clone, Debug, Eq, PartialEq)] +pub enum PatchCommand { + Show(PatchId), + Edit(PatchId), + Checkout(PatchId), +} + #[derive(Debug, Eq, PartialEq, Clone, Hash)] pub enum ListCid { Header, @@ -44,7 +51,7 @@ pub enum Cid { pub enum Message { #[default] Tick, - Quit(Option), + Quit(Option), Batch(Vec), } @@ -53,7 +60,7 @@ pub struct App { pages: PageStack, theme: Theme, quit: bool, - result: Option, + result: Option, } /// Creates a new application using a tui-realm-application, mounts all @@ -113,7 +120,7 @@ impl App { } } -impl Tui for App { +impl Tui for App { fn init(&mut self, app: &mut Application) -> Result<()> { self.view_list(app, &self.theme.clone())?; @@ -152,10 +159,10 @@ impl Tui for App { } } - fn exit(&self) -> Option { + fn exit(&self) -> Option> { if self.quit { return Some(Exit { - value: self.result.map(|id| format!("{id}")), + value: self.result.clone(), }); } None diff --git a/bin/commands/patch/list/event.rs b/bin/commands/patch/list/event.rs index bf304622..9d309c12 100644 --- a/bin/commands/patch/list/event.rs +++ b/bin/commands/patch/list/event.rs @@ -1,6 +1,6 @@ -use tuirealm::command::{Cmd, Direction as MoveDirection}; +use tuirealm::command::{Cmd, CmdResult, Direction as MoveDirection}; use tuirealm::event::{Event, Key, KeyEvent}; -use tuirealm::{MockComponent, NoUserEvent}; +use tuirealm::{MockComponent, NoUserEvent, State, StateValue}; use radicle_tui as tui; @@ -11,7 +11,7 @@ use tui::ui::widget::list::PropertyList; use tui::ui::widget::Widget; use super::super::common; -use super::Message; +use super::{Message, PatchCommand}; /// Since the framework does not know the type of messages that are being /// passed around in the app, the following handlers need to be implemented for @@ -51,6 +51,48 @@ impl tuirealm::Component for Widget { + let result = self.perform(Cmd::Submit); + match result { + CmdResult::Submit(State::One(StateValue::Usize(selected))) => { + let item = self.items().get(selected)?; + Some(Message::Quit(Some(PatchCommand::Show( + item.id().to_owned(), + )))) + } + _ => None, + } + } + Event::Keyboard(KeyEvent { + code: Key::Char('e'), .. + }) => { + let result = self.perform(Cmd::Submit); + match result { + CmdResult::Submit(State::One(StateValue::Usize(selected))) => { + let item = self.items().get(selected)?; + Some(Message::Quit(Some(PatchCommand::Edit( + item.id().to_owned(), + )))) + } + _ => None, + } + } + 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)?; + Some(Message::Quit(Some(PatchCommand::Checkout( + item.id().to_owned(), + )))) + } + _ => None, + } + } _ => None, } } diff --git a/bin/commands/patch/list/page.rs b/bin/commands/patch/list/page.rs index af2b0dc2..1d08d990 100644 --- a/bin/commands/patch/list/page.rs +++ b/bin/commands/patch/list/page.rs @@ -39,6 +39,9 @@ impl ListView { theme, vec![ tui::ui::shortcut(theme, "↑/↓", "navigate"), + tui::ui::shortcut(theme, "enter", "show"), + tui::ui::shortcut(theme, "c", "checkout"), + tui::ui::shortcut(theme, "e", "edit"), tui::ui::shortcut(theme, "q", "quit"), ], ), diff --git a/bin/commands/patch/select.rs b/bin/commands/patch/select.rs index ec736b9d..6e3bab06 100644 --- a/bin/commands/patch/select.rs +++ b/bin/commands/patch/select.rs @@ -113,7 +113,7 @@ impl App { } } -impl Tui for App { +impl Tui for App { fn init(&mut self, app: &mut Application) -> Result<()> { self.view_list(app, &self.theme.clone())?; @@ -152,7 +152,7 @@ impl Tui for App { } } - fn exit(&self) -> Option { + fn exit(&self) -> Option> { if self.quit { return Some(Exit { value: self.result.map(|id| format!("{id}")), diff --git a/bin/commands/patch/suite.rs b/bin/commands/patch/suite.rs index 2c6fa2e5..30389efe 100644 --- a/bin/commands/patch/suite.rs +++ b/bin/commands/patch/suite.rs @@ -232,7 +232,7 @@ impl App { } } -impl Tui for App { +impl Tui for App { fn init(&mut self, app: &mut Application) -> Result<()> { self.view_list(app, &self.theme.clone())?; @@ -275,7 +275,7 @@ impl Tui for App { } } - fn exit(&self) -> Option { + fn exit(&self) -> Option> { if self.quit { return Some(Exit { value: None }); } diff --git a/src/lib.rs b/src/lib.rs index 4a762ea1..c7055af3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -22,7 +22,7 @@ use ui::theme::Theme; /// accordingly and rendered with new state. /// /// Please see `examples/` for further information on how to use it. -pub trait Tui +pub trait Tui where Id: Eq + PartialEq + Clone + Hash, Message: Eq, @@ -38,12 +38,12 @@ where fn view(&mut self, app: &mut Application, frame: &mut Frame); /// Should return `Some` if the application is requested to quit. - fn exit(&self) -> Option; + fn exit(&self) -> Option>; } /// An optional return value. -pub struct Exit { - pub value: Option, +pub struct Exit { + pub value: Option, } /// A tui-window using the cross-platform Terminal helper provided @@ -77,9 +77,13 @@ impl Window { /// - update application state /// - redraw view /// 3. Leave alternative terminal screen - pub fn run(&mut self, tui: &mut T, interval: u64) -> Result> + pub fn run( + &mut self, + tui: &mut T, + interval: u64, + ) -> Result> where - T: Tui, + T: Tui, Id: Eq + PartialEq + Clone + Hash, Message: Eq, {