From a6838098bc89a3864aceb3ae7d26ff03d6961d5a Mon Sep 17 00:00:00 2001 From: Erik Kundt Date: Tue, 27 Aug 2024 16:36:39 +0200 Subject: [PATCH] lib: Move imUI to ui module --- src/lib.rs | 32 +++++++++++++++++++++++++++++++- src/ui.rs | 1 + src/{ => ui}/im.rs | 35 +---------------------------------- 3 files changed, 33 insertions(+), 35 deletions(-) rename src/{ => ui}/im.rs (97%) diff --git a/src/lib.rs b/src/lib.rs index 9cf61ec8..154c0202 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,4 @@ pub mod event; -pub mod im; pub mod store; pub mod task; pub mod terminal; @@ -18,6 +17,7 @@ use store::State; use task::Interrupted; use ui::widget::Widget; use ui::Frontend; +use ui::im; /// An optional return value. #[derive(Clone, Debug)] @@ -176,3 +176,33 @@ where anyhow::bail!("exited because of an unexpected error"); } } + +pub async fn run_im( + channel: Channel, + state: S, + app: impl im::App, +) -> Result> +where + S: State + Clone + Debug + Send + Sync + 'static, + M: 'static, + P: Clone + Debug + Send + Sync + 'static, +{ + let (terminator, mut interrupt_rx) = task::create_termination(); + + let (store, state_rx) = store::Store::::new(); + let frontend = im::Frontend::default(); + + tokio::try_join!( + store.main_loop(state, terminator, channel.rx, interrupt_rx.resubscribe()), + frontend.main_loop(app, state_rx, interrupt_rx.resubscribe()), + )?; + + if let Ok(reason) = interrupt_rx.recv().await { + match reason { + Interrupted::User { payload } => Ok(payload), + Interrupted::OsSignal => anyhow::bail!("exited because of an os sig int"), + } + } else { + anyhow::bail!("exited because of an unexpected error"); + } +} diff --git a/src/ui.rs b/src/ui.rs index d8f9ccc1..1f744ba6 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -3,6 +3,7 @@ pub mod layout; pub mod span; pub mod theme; pub mod widget; +pub mod im; use std::fmt::Debug; use std::time::Duration; diff --git a/src/im.rs b/src/ui/im.rs similarity index 97% rename from src/im.rs rename to src/ui/im.rs index 8698a3e6..5eea53b6 100644 --- a/src/im.rs +++ b/src/ui/im.rs @@ -14,15 +14,12 @@ use ratatui::layout::{Constraint, Rect}; use ratatui::Frame; use crate::event::Event; -use crate::store; use crate::store::State; -use crate::task; use crate::task::Interrupted; use crate::terminal; use crate::ui::theme::Theme; use crate::ui::widget::container::Column; use crate::ui::widget::list::ToRow; -use crate::Channel; use self::widget::Widget; @@ -36,41 +33,11 @@ pub trait App { fn update(&self, ui: &Context, frame: &mut Frame, state: &Self::State) -> Result<()>; } -pub async fn run_app( - channel: Channel, - state: S, - app: impl App, -) -> Result> -where - S: State + Clone + Debug + Send + Sync + 'static, - M: 'static, - P: Clone + Debug + Send + Sync + 'static, -{ - let (terminator, mut interrupt_rx) = task::create_termination(); - - let (store, state_rx) = store::Store::::new(); - let frontend = Frontend::default(); - - tokio::try_join!( - store.main_loop(state, terminator, channel.rx, interrupt_rx.resubscribe()), - frontend.im_main_loop(app, state_rx, interrupt_rx.resubscribe()), - )?; - - if let Ok(reason) = interrupt_rx.recv().await { - match reason { - Interrupted::User { payload } => Ok(payload), - Interrupted::OsSignal => anyhow::bail!("exited because of an os sig int"), - } - } else { - anyhow::bail!("exited because of an unexpected error"); - } -} - #[derive(Default)] pub struct Frontend {} impl Frontend { - pub async fn im_main_loop( + pub async fn main_loop( self, app: impl App, mut state_rx: UnboundedReceiver,