mirror of
https://codeberg.org/JasterV/teatui.git
synced 2026-04-27 02:15:41 +00:00
chore: update docs
This commit is contained in:
parent
d439efd2a3
commit
520afe7183
5 changed files with 45 additions and 10 deletions
|
|
@ -1,9 +1,13 @@
|
|||
// Actor responsible of processing side effects sent by the update actor.
|
||||
//! Actor responsible of processing side effects sent by the update actor.
|
||||
use color_eyre::Result;
|
||||
use std::sync::mpsc::Receiver;
|
||||
use std::sync::mpsc::Sender;
|
||||
|
||||
pub fn run<M, Msg, Eff, F>(effects_fn: F, rx: Receiver<(M, Eff)>, tx: Sender<Msg>) -> Result<()>
|
||||
pub(crate) fn run<M, Msg, Eff, F>(
|
||||
effects_fn: F,
|
||||
rx: Receiver<(M, Eff)>,
|
||||
tx: Sender<Msg>,
|
||||
) -> Result<()>
|
||||
where
|
||||
Msg: Send + Sync + 'static,
|
||||
F: Fn(&M, Eff) -> Result<Option<Msg>>,
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ use color_eyre::Result;
|
|||
use crossterm::event;
|
||||
use std::sync::mpsc::Sender;
|
||||
|
||||
pub fn run<M>(tx: Sender<M>) -> Result<()>
|
||||
pub(crate) fn run<M>(tx: Sender<M>) -> Result<()>
|
||||
where
|
||||
M: From<crossterm::event::Event> + Sync + Send + 'static,
|
||||
{
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
//! Elm-like framework based on Ratatui.
|
||||
//! Elm-like framework implemented on top of [Ratatui](https://github.com/ratatui/ratatui).
|
||||
//!
|
||||
//! The state of your application is represented by a single type called the Model.
|
||||
//!
|
||||
|
|
@ -22,15 +22,22 @@
|
|||
//! The users of this framework only need to provide:
|
||||
//!
|
||||
//! - An update function that given a model and a message return an `Update` instance.
|
||||
//!
|
||||
//! - A view function that given a reference to the model, returns a `View`
|
||||
//!
|
||||
//! - An effects function that given a reference to the model and an effect,
|
||||
//! might perform any side effects and optionally return a message to update the state of the application
|
||||
//!
|
||||
//! ### Examples
|
||||
//!
|
||||
//! You can find a folder with example projects in the [examples](https://github.com/JasterV/ratatui-elm/tree/main/examples) folder.
|
||||
use color_eyre::Report;
|
||||
use color_eyre::Result;
|
||||
use std::{
|
||||
sync::mpsc::{Sender, channel},
|
||||
thread,
|
||||
};
|
||||
|
||||
pub use update::Update;
|
||||
pub use view::View;
|
||||
|
||||
|
|
@ -39,6 +46,18 @@ mod events;
|
|||
mod update;
|
||||
mod view;
|
||||
|
||||
/// Starts the runtime which manages all the internal
|
||||
/// processes and message passing.
|
||||
///
|
||||
/// The user needs to provide:
|
||||
///
|
||||
/// - The initial model
|
||||
///
|
||||
/// - An `update` function, responsible for updating the model based on messages.
|
||||
///
|
||||
/// - A `view` function, responsible for constructing the view from the model.
|
||||
///
|
||||
/// - An `effects` function responsible for handling side effects.
|
||||
pub fn start<M, Msg, Eff, UF, VF, EF>(
|
||||
model: M,
|
||||
update_fn: UF,
|
||||
|
|
|
|||
|
|
@ -2,13 +2,20 @@
|
|||
use color_eyre::{Report, Result};
|
||||
use std::sync::mpsc::{Receiver, Sender};
|
||||
|
||||
/// Tells the runtime what to do with the previous message.
|
||||
///
|
||||
/// If `Update::Exit` is returned, the program will exit.
|
||||
///
|
||||
/// If `Update::Next(M)` is returned, the view will be rendered with the new model.
|
||||
///
|
||||
/// If `Update::NextWithEffect` is returned, the view will be rendered with the new model and a side effect will be executed.
|
||||
pub enum Update<M, E> {
|
||||
Exit,
|
||||
Next(M),
|
||||
NextWithEffect(M, E),
|
||||
}
|
||||
|
||||
pub fn run<M, Msg, Eff, F>(
|
||||
pub(crate) fn run<M, Msg, Eff, F>(
|
||||
mut model: M,
|
||||
update_fn: F,
|
||||
rx: Receiver<Msg>,
|
||||
|
|
@ -33,13 +40,15 @@ where
|
|||
Update::NextWithEffect(new_model, effect) => (new_model, Some(effect)),
|
||||
};
|
||||
|
||||
// Send the new model to the view
|
||||
view_tx.send(new_model.clone())?;
|
||||
|
||||
// After the view is notified of the new model,
|
||||
// execute side effects if any
|
||||
if let Some(effect) = effect {
|
||||
effects_tx.send((new_model.clone(), effect))?;
|
||||
}
|
||||
|
||||
// Send the updated version of the model
|
||||
view_tx.send(new_model.clone())?;
|
||||
|
||||
model = new_model;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,10 +1,13 @@
|
|||
// Actor responsible of rendering the model into the terminal.
|
||||
//! Actor responsible of rendering the model into the terminal.
|
||||
use color_eyre::Result;
|
||||
use ratatui::DefaultTerminal;
|
||||
use ratatui::widgets::Widget;
|
||||
use ratatui::widgets::WidgetRef;
|
||||
use std::sync::mpsc::Receiver;
|
||||
|
||||
/// A thin wrapper around a `ratatui` WidgetRef.
|
||||
/// It is guaranteed that it will always be possible
|
||||
/// to construct it from a Widget.
|
||||
pub struct View(Box<dyn WidgetRef>);
|
||||
|
||||
impl View {
|
||||
|
|
@ -19,7 +22,7 @@ impl Widget for View {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn run<M, F>(
|
||||
pub(crate) fn run<M, F>(
|
||||
mut model: M,
|
||||
mut terminal: DefaultTerminal,
|
||||
view_fn: F,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue