Crate ratatui_calloop
source ·Expand description
Ratatui-calloop is an experimental library for building terminal applications using the Calloop event loop.
This library provides a simple API for building terminal applications using the calloop event
loop. It is built on top of the ratatui library, which provides a high-level API for building
terminal applications. The ratatui-calloop library provides an implementation of the App
trait
that is compatible with the calloop event loop, allowing you to build terminal applications that
respond to user input and update the terminal in real-time.
§Example
The following is a simple example of a terminal application that displays a counter and responds to key events to increment or decrement the counter. The application uses the ratatui-calloop library to handle the event loop and update the terminal.
use calloop::LoopSignal;
use ratatui::{
crossterm::event::{KeyCode, KeyEvent},
layout::Rect,
text::Text,
};
use ratatui_calloop::{App, ApplicationLoop, Result, Terminal};
fn main() -> Result<()> {
let mut app_loop = ApplicationLoop::new()?;
let mut app = DemoApp {
counter: 0,
exit_signal: app_loop.exit_signal(),
};
app_loop.run(&mut app)
}
pub struct DemoApp {
counter: i32,
exit_signal: LoopSignal,
}
impl App for DemoApp {
fn draw(&self, terminal: &mut Terminal) -> std::io::Result<()> {
terminal.draw(|frame| {
let text = Text::raw(format!("Counter: {} <↑/↓> <q: quit>", self.counter));
frame.render_widget(&text, Rect::default());
})?;
Ok(())
}
fn on_key_event(&mut self, event: KeyEvent) {
match event.code {
KeyCode::Up => self.counter += 1,
KeyCode::Down => self.counter -= 1,
KeyCode::Char('q') => self.exit_signal.stop(),
_ => {}
}
}
}
§Features
-
color-eyre
(enabled by default) — Installs color-eyre panic and error hooks during terminal initialization to provide better error messages and backtraces. This feature is optional and can be disabled if you don’t want to use it.
Structs§
- The main event loop for the application.
Enums§
- An error type for this crate.
Traits§
- A trait for the main application struct.
Type Aliases§
- A type alias for the
Result
type used in this crate. - A type alias to simplify the usage of the terminal type from the ratatui library.