milk-tea

Crates.iomilk-tea
lib.rsmilk-tea
version0.3.0
sourcesrc
created_at2024-06-27 02:33:32.908584
updated_at2024-06-28 23:21:36.283306
descriptionA minimal library for creating TUI apps
homepage
repositoryhttps://github.com/finndotbin/milk-tea
max_upload_size
id1285032
size34,721
finndotbin (finndotbin)

documentation

README

🧋 Milk Tea

Milk Tea is a minimal Rust library for creating TUI (terminal user interface) apps. It takes a functional approach to constructing applications inspired by many modern web frameworks.

NOTE: This library has nothing to do with the Go library Bubble Tea. It just happens to also be named around boba tea and just happens to also be an MVU-based TUI library. (I don't know how to Google project names apparently QwQ)

Getting Started

cargo add milk-tea
//! Prints "hello world :3" in magenta and bold to the top left of the screen.

use milk_tea::{
    area::Area,
    draw_call::{DrawCall, DrawCallKind},
    event::{Event, KeyCode, KeyEvent},
    pair::Pair,
    run,
    style::{ContentStyle, Stylize},
    text_size::UnicodeSize,
};

fn main() {
    run(Model::default(), view, update).unwrap();
}

/// Handles drawing to the screen. An `Area` collects any draw calls we push to it to be rendered
/// later.
fn view(_model: &Model, area: &mut Area) {
    area.push_all(vec![
        DrawCall::new(
            Pair::fill(0),
            DrawCallKind::SetStyle(ContentStyle::new().magenta().bold()),
        ),
        DrawCall::new(
            Pair::fill(0),
            DrawCallKind::PrintLine("hello world! :3".limit_size(area.size())),
        ),
    ]);
}

/// Handles events and updates the `Model`.
fn update(event: Event, model: &mut Model) {
    if let Event::Key(KeyEvent {
        code: KeyCode::Esc, ..
    }) = event
    {
        model.0 = true
    }
}

/// Represents the application state.
#[derive(Default, Clone, PartialEq, Eq)]
struct Model(bool);

impl milk_tea::Model for Model {
    fn should_exit(&self) -> bool {
        self.0
    }
}

More examples can be found in the examples directory. Use cargo run --example example_name to run an example.

Commit count: 32

cargo fmt