lib: Move imUI to ui module

This commit is contained in:
Erik Kundt
2024-09-04 14:37:42 +02:00
parent 41d240b221
commit a6838098bc
3 changed files with 33 additions and 35 deletions
+31 -1
View File
@@ -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<S, M, P>(
channel: Channel<M>,
state: S,
app: impl im::App<State = S, Message = M>,
) -> Result<Option<P>>
where
S: State<P, Message = M> + 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::<S, M, P>::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");
}
}
+1
View File
@@ -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;
+1 -34
View File
@@ -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<S, M, P>(
channel: Channel<M>,
state: S,
app: impl App<State = S, Message = M>,
) -> Result<Option<P>>
where
S: State<P, Message = M> + 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::<S, M, P>::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<S, M, P>(
pub async fn main_loop<S, M, P>(
self,
app: impl App<State = S, Message = M>,
mut state_rx: UnboundedReceiver<S>,