tuyere 0.1.0

A powerful TUI framework based on The Elm Architecture 🔮
Documentation
//! App builder for convenient setup.

use crate::model::Model;
use crate::program::{Program, ProgramOptions};
use std::io;

/// A builder for creating and running Cauldron applications.
///
/// # Example
///
/// ```rust,no_run
/// use cauldron::{App, Model, Command};
///
/// struct MyApp;
///
/// impl Model for MyApp {
///     type Message = ();
///     fn update(&mut self, _: ()) -> Command<()> { Command::quit() }
///     fn view(&self) -> String { "Hello!".into() }
/// }
///
/// App::new(MyApp)
///     .with_mouse()
///     .run()
///     .unwrap();
/// ```
pub struct App<M: Model> {
    model: M,
    options: ProgramOptions,
}

impl<M: Model + 'static> App<M> {
    /// Create a new app with the given model.
    pub fn new(model: M) -> Self {
        Self {
            model,
            options: ProgramOptions::default(),
        }
    }

    /// Enable mouse support.
    #[must_use]
    pub fn with_mouse(mut self) -> Self {
        self.options.mouse = true;
        self
    }

    /// Disable alternate screen (inline mode).
    #[must_use]
    pub fn inline(mut self) -> Self {
        self.options.alternate_screen = false;
        self
    }

    /// Show the cursor.
    #[must_use]
    pub fn show_cursor(mut self) -> Self {
        self.options.hide_cursor = false;
        self
    }

    /// Enable bracketed paste mode.
    #[must_use]
    pub fn with_bracketed_paste(mut self) -> Self {
        self.options.bracketed_paste = true;
        self
    }

    /// Set custom options.
    #[must_use]
    pub fn with_options(mut self, options: ProgramOptions) -> Self {
        self.options = options;
        self
    }

    /// Run the application.
    pub fn run(self) -> io::Result<()> {
        Program::with_options(self.model, self.options).run()
    }
}