2 releases
| 0.0.8 | Nov 11, 2025 |
|---|---|
| 0.0.7 | Dec 13, 2024 |
#19 in #expand
11KB
117 lines
This library expands on [ratatui] by abstracting main methods to different modules, which can be helpful in making modular applications with many view::Views as well as using architectural patterns like MVVM.
How to run
To run application use methods from module [app]:
- Initialize the app with [
app::init()] and provideview::View. - Run the application loop [
app::run()]. - When quitting, call [
app::quit()] to insure proper and clean exit.
Basic example
Add required imports.
use ratatuio::{app, view::View};
use ratatuio::ratatui::{buffer::Buffer, layout::Rect, widgets::Widget};
use std::io;
Create a struct that will store the state of current view.
struct MainPage;
Implement view::View trait and provide the method [view::View::render_view()].
impl View for MainPage{
fn render_view(&self, area: Rect, buf: &mut Buffer){
"Hello World!".render(area, buf);
}
}
And finally initialize and run the app with created MainPage view in the main method.
fn main() -> io::Result<()> {
app::init(MainPage);
app::run()
}
Adding event handler
Above example will run in console however it will be impossible to close. To implement closing the application we first need to handle key press event.
In trait view::View there is an optional method to implement [view::View::handle_events()] with parameter crossterm::event::Event.
We can implement this method and using Rusts' match statement catch when user is pressing key 'q' or 'Q':
use ratatuio::crossterm::event::{Event, KeyEventKind, KeyCode};
impl View for MainPage{
//...
fn handle_events(&mut self, event: &Event) -> io::Result<()> {
match event {
Event::Key(key_event) if key_event.kind == KeyEventKind::Press => {
match key_event.code {
KeyCode::Char('q') | KeyCode::Char('Q') => {
app::quit()
},
_ => {}
}
}
_ => {}
}
Ok(())
}
Inside handle_events we are checking when user is pressing key 'q' or 'Q' after which we are accessing application state app::APPLICATION and editing its 'is_running' value, which will exit out of the program loop on the next iteration.
Dependencies
~8–21MB
~269K SLoC