| Crates.io | sketch |
| lib.rs | sketch |
| version | 0.1.0 |
| created_at | 2025-01-15 00:45:37.839443+00 |
| updated_at | 2025-01-15 00:45:37.839443+00 |
| description | A Rust TUI library inspired by bubbletea |
| homepage | |
| repository | https://github.com/LiamGallagher737/sketch |
| max_upload_size | |
| id | 1516930 |
| size | 50,802 |
Model::updateThe 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:
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::viewThe 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())
}
MessageA 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.