mirror of
https://github.com/ratatui-org/ratatui
synced 2024-11-22 12:43:16 +00:00
ed51c4b342
These are simple opinionated methods for creating a terminal that is useful to use in most apps. The new init method creates a crossterm backend writing to stdout, enables raw mode, enters the alternate screen, and sets a panic handler that restores the terminal on panic. A minimal hello world now looks a bit like: ```rust use ratatui::{ crossterm::event::{self, Event}, text::Text, Frame, }; fn main() { let mut terminal = ratatui::init(); loop { terminal .draw(|frame: &mut Frame| frame.render_widget(Text::raw("Hello World!"), frame.area())) .expect("Failed to draw"); if matches!(event::read().expect("failed to read event"), Event::Key(_)) { break; } } ratatui::restore(); } ``` A type alias `DefaultTerminal` is added to represent this terminal type and to simplify any cases where applications need to pass this terminal around. It is equivalent to: `Terminal<CrosstermBackend<Stdout>>` We also added `ratatui::try_init()` and `try_restore()`, for situations where you might want to handle initialization errors yourself instead of letting the panic handler fire and cleanup. Simple Apps should prefer the `init` and `restore` functions over these functions. Corresponding functions to allow passing a `TerminalOptions` with a `Viewport` (e.g. inline, fixed) are also available (`init_with_options`, and `try_init_with_options`). The existing code to create a backend and terminal will remain and is not deprecated by this approach. This just provides a simple one line initialization using the common options. --------- Co-authored-by: Orhun Parmaksız <orhunparmaksiz@gmail.com>
86 lines
2.2 KiB
Rust
86 lines
2.2 KiB
Rust
#![allow(dead_code)]
|
|
use std::{error::Error, io, sync::mpsc, thread, time::Duration};
|
|
|
|
use ratatui::{
|
|
backend::{Backend, TermionBackend},
|
|
termion::{
|
|
event::Key,
|
|
input::{MouseTerminal, TermRead},
|
|
raw::IntoRawMode,
|
|
screen::IntoAlternateScreen,
|
|
},
|
|
Terminal,
|
|
};
|
|
|
|
use crate::{app::App, ui};
|
|
|
|
pub fn run(tick_rate: Duration, enhanced_graphics: bool) -> Result<(), Box<dyn Error>> {
|
|
// setup terminal
|
|
let stdout = io::stdout()
|
|
.into_raw_mode()
|
|
.unwrap()
|
|
.into_alternate_screen()
|
|
.unwrap();
|
|
let stdout = MouseTerminal::from(stdout);
|
|
let backend = TermionBackend::new(stdout);
|
|
let mut terminal = Terminal::new(backend)?;
|
|
|
|
// create app and run it
|
|
let app = App::new("Termion demo", enhanced_graphics);
|
|
run_app(&mut terminal, app, tick_rate)?;
|
|
|
|
Ok(())
|
|
}
|
|
|
|
fn run_app<B: Backend>(
|
|
terminal: &mut Terminal<B>,
|
|
mut app: App,
|
|
tick_rate: Duration,
|
|
) -> Result<(), Box<dyn Error>> {
|
|
let events = events(tick_rate);
|
|
loop {
|
|
terminal.draw(|frame| ui::draw(frame, &mut app))?;
|
|
|
|
match events.recv()? {
|
|
Event::Input(key) => match key {
|
|
Key::Up | Key::Char('k') => app.on_up(),
|
|
Key::Down | Key::Char('j') => app.on_down(),
|
|
Key::Left | Key::Char('h') => app.on_left(),
|
|
Key::Right | Key::Char('l') => app.on_right(),
|
|
Key::Char(c) => app.on_key(c),
|
|
_ => {}
|
|
},
|
|
Event::Tick => app.on_tick(),
|
|
}
|
|
if app.should_quit {
|
|
return Ok(());
|
|
}
|
|
}
|
|
}
|
|
|
|
enum Event {
|
|
Input(Key),
|
|
Tick,
|
|
}
|
|
|
|
fn events(tick_rate: Duration) -> mpsc::Receiver<Event> {
|
|
let (tx, rx) = mpsc::channel();
|
|
let keys_tx = tx.clone();
|
|
thread::spawn(move || {
|
|
let stdin = io::stdin();
|
|
for key in stdin.keys().flatten() {
|
|
if let Err(err) = keys_tx.send(Event::Input(key)) {
|
|
eprintln!("{err}");
|
|
return;
|
|
}
|
|
}
|
|
});
|
|
thread::spawn(move || loop {
|
|
if let Err(err) = tx.send(Event::Tick) {
|
|
eprintln!("{err}");
|
|
break;
|
|
}
|
|
thread::sleep(tick_rate);
|
|
});
|
|
rx
|
|
}
|