tuyere 0.1.0

A powerful TUI framework based on The Elm Architecture 🔮
Documentation
//! The Model trait - the core of the Elm Architecture.

use crate::command::Command;
use crate::event::Event;

/// The core trait for Cauldron applications.
///
/// Implement this trait to define your application's behavior.
/// The Model represents your application state and defines how it
/// responds to messages and how it renders itself.
///
/// # Example
///
/// ```rust
/// use cauldron::{Model, Command, Event, Key};
///
/// struct Counter {
///     count: i32,
/// }
///
/// enum Msg {
///     Increment,
///     Decrement,
///     Quit,
/// }
///
/// impl Model for Counter {
///     type Message = Msg;
///
///     fn update(&mut self, msg: Msg) -> Command<Msg> {
///         match msg {
///             Msg::Increment => self.count += 1,
///             Msg::Decrement => self.count -= 1,
///             Msg::Quit => return Command::quit(),
///         }
///         Command::none()
///     }
///
///     fn view(&self) -> String {
///         format!("Count: {}", self.count)
///     }
///
///     fn handle_event(&self, event: Event) -> Option<Msg> {
///         match event {
///             Event::Key(Key::Char('+')) => Some(Msg::Increment),
///             Event::Key(Key::Char('-')) => Some(Msg::Decrement),
///             Event::Key(Key::Char('q')) => Some(Msg::Quit),
///             _ => None,
///         }
///     }
/// }
/// ```
pub trait Model {
    /// The message type that this model responds to.
    type Message;

    /// Update the model based on a message.
    ///
    /// This is where your application logic lives. Return a `Command`
    /// to perform side effects like quitting or spawning async tasks.
    fn update(&mut self, msg: Self::Message) -> Command<Self::Message>;

    /// Render the model to a string.
    ///
    /// This string will be displayed in the terminal. You can use
    /// ANSI escape codes (via `glyphs`) to add colors and styling.
    fn view(&self) -> String;

    /// Initialize the model.
    ///
    /// Called once when the program starts. Return a command to
    /// perform initial setup like loading data.
    fn init(&mut self) -> Command<Self::Message> {
        Command::none()
    }

    /// Handle an event and optionally convert it to a message.
    ///
    /// Override this to handle keyboard, mouse, and other events.
    /// Return `Some(msg)` to send a message, or `None` to ignore the event.
    fn handle_event(&self, _event: Event) -> Option<Self::Message> {
        None
    }

    /// Called when the terminal is resized.
    ///
    /// Override this if your application needs to respond to size changes.
    fn on_resize(&mut self, _width: u16, _height: u16) {}

    /// Called when the application is about to quit.
    ///
    /// Use this for cleanup tasks.
    fn on_quit(&mut self) {}
}