Skip to main content

Model

Trait Model 

Source
pub trait Model: Send + 'static {
    // Required methods
    fn init(&self) -> Option<Cmd>;
    fn update(&mut self, msg: Message) -> Option<Cmd>;
    fn view(&self) -> String;
}
Expand description

The Model trait for TUI applications.

Implement this trait to define your application’s behavior.

§Example

use bubbletea::{Model, Message, Cmd};

struct Counter { count: i32 }

impl Model for Counter {
    fn init(&self) -> Option<Cmd> { None }

    fn update(&mut self, msg: Message) -> Option<Cmd> {
        if msg.is::<i32>() {
            self.count += msg.downcast::<i32>().unwrap();
        }
        None
    }

    fn view(&self) -> String {
        format!("Count: {}", self.count)
    }
}

Required Methods§

Source

fn init(&self) -> Option<Cmd>

Initialize the model and return an optional startup command.

This is called once when the program starts.

Source

fn update(&mut self, msg: Message) -> Option<Cmd>

Process a message and return a new command.

This is the pure update function at the heart of the Elm Architecture.

Source

fn view(&self) -> String

Render the model as a string for display.

This should be a pure function with no side effects.

Implementors§