Skip to content

Rust TUI library inspired by bubbletea. (Not for production)

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT
Notifications You must be signed in to change notification settings

LiamGallagher737/sketch

Repository files navigation

sketch

A Rust TUI library

crates.io docs.rs

Features

  • Fast compile times
  • Built on crossterm for cross-platform compatibility
  • Elm-like MVU architecture

Key Concepts

Model::update

The update method processes events by taking a Msg as input. This could be a keyboard key, mouse input, or a custom message. Based on the message, it:

  • Updates the model to its next state.
  • Optionally returns another message to be processed.

For example:

fn update(mut self, msg: &Msg) -> (Self, Option<Msg>) {
    // Handle `Key` messages.
    if let Some(key) = msg.cast::<Key>() {
        match key.code {
            // If the enter key was pressed increment the counter.
            KeyCode::Enter => self.count += 1,
            // If the 'q' key was pressed return the `Quit` message to exit the app.
            KeyCode::Char('q') => return (self, Some(Msg::new(Quit))),
            _ => {}
        }
    }

    (self, None)
}

Model::view

The view method is responsible for rendering your model into a string. You can use Style to apply formatting:

fn view(&self) -> String {
    COUNTER_STYLE.render(self.count.to_string())
}

Message

A message is any type that implements the Message trait. The Msg type given to Model::update holds a Message. The benifet of this approach is messages can be defined by other crates like widget libraries.

A message can be defined like this:

struct MyMessage {
    something: i32,
}
impl Message for MyMessage {}

Your message can then be sent using the app's Sender which you can get with App::sender:

let app = App::new(model);
let sender = app.sender();
sender.send(MyMessage { something: 5 });
app.run();

For example if you need to make and HTTP request you could spawn a thread to complete the request and then send a message using the sender.

Sketch includes the following messages:

  • Quit: Send to quit the app.
  • Key: Keyboard input.
  • Mouse: Mouse input.
  • Focus: Focus changes.
  • Paste: Clipboard pastes. Only if the paste feature is enabeld.

About

Rust TUI library inspired by bubbletea. (Not for production)

Topics

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT

Stars

Watchers

Forks