ratatui/examples/curses_demo.rs

78 lines
2.2 KiB
Rust
Raw Normal View History

mod demo;
2019-01-24 18:45:59 +00:00
#[allow(dead_code)]
mod util;
use crate::demo::{ui, App};
use argh::FromArgs;
use easycurses;
use std::{
error::Error,
io,
time::{Duration, Instant},
};
use tui::{backend::CursesBackend, Terminal};
/// Curses demo
#[derive(Debug, FromArgs)]
struct Cli {
/// time in ms between two ticks.
#[argh(option, default = "250")]
tick_rate: u64,
/// whether unicode symbols are used to improve the overall look of the app
#[argh(option, default = "true")]
enhanced_graphics: bool,
2019-01-24 18:45:59 +00:00
}
fn main() -> Result<(), Box<dyn Error>> {
let cli: Cli = argh::from_env();
2019-01-24 18:45:59 +00:00
let mut backend = CursesBackend::new().ok_or(io::Error::new(io::ErrorKind::Other, ""))?;
let curses = backend.get_curses_mut();
curses.set_echo(false);
curses.set_input_timeout(easycurses::TimeoutMode::WaitUpTo(50));
curses.set_input_mode(easycurses::InputMode::RawCharacter);
curses.set_keypad_enabled(true);
let mut terminal = Terminal::new(backend)?;
terminal.hide_cursor()?;
2019-01-24 18:45:59 +00:00
let mut app = App::new("Curses demo", cli.enhanced_graphics);
2019-01-24 18:45:59 +00:00
let mut last_tick = Instant::now();
let tick_rate = Duration::from_millis(cli.tick_rate);
2019-01-24 18:45:59 +00:00
loop {
feat: add stateful widgets Most widgets can be drawn directly based on the input parameters. However, some features may require some kind of associated state to be implemented. For example, the `List` widget can highlight the item currently selected. This can be translated in an offset, which is the number of elements to skip in order to have the selected item within the viewport currently allocated to this widget. The widget can therefore only provide the following behavior: whenever the selected item is out of the viewport scroll to a predefined position (make the selected item the last viewable item or the one in the middle). Nonetheless, if the widget has access to the last computed offset then it can implement a natural scrolling experience where the last offset is reused until the selected item is out of the viewport. To allow such behavior within the widgets, this commit introduces the following changes: - Add a `StatefulWidget` trait with an associated `State` type. Widgets that can take advantage of having a "memory" between two draw calls needs to implement this trait. - Add a `render_stateful_widget` method on `Frame` where the associated state is given as a parameter. The chosen approach is thus to let the developers manage their widgets' states themselves as they are already responsible for the lifecycle of the wigets (given that the crate exposes an immediate mode api). The following changes were also introduced: - `Widget::render` has been deleted. Developers should use `Frame::render_widget` instead. - `Widget::background` has been deleted. Developers should use `Buffer::set_background` instead. - `SelectableList` has been deleted. Developers can directly use `List` where `SelectableList` features have been back-ported.
2019-12-15 20:38:18 +00:00
terminal.draw(|mut f| ui::draw(&mut f, &mut app))?;
match terminal.backend_mut().get_curses_mut().get_input() {
2019-01-24 18:45:59 +00:00
Some(input) => {
match input {
easycurses::Input::Character(c) => {
app.on_key(c);
}
2019-01-24 18:45:59 +00:00
easycurses::Input::KeyUp => {
app.on_up();
2019-01-24 18:45:59 +00:00
}
easycurses::Input::KeyDown => {
app.on_down();
2019-01-24 18:45:59 +00:00
}
easycurses::Input::KeyLeft => {
app.on_left();
2019-01-24 18:45:59 +00:00
}
easycurses::Input::KeyRight => {
app.on_right();
2019-01-24 18:45:59 +00:00
}
_ => {}
};
}
_ => {}
};
terminal.backend_mut().get_curses_mut().flush_input();
if last_tick.elapsed() > tick_rate {
app.on_tick();
last_tick = Instant::now();
}
if app.should_quit {
break;
2019-01-24 18:45:59 +00:00
}
}
Ok(())
2019-01-24 18:45:59 +00:00
}