2024-08-13 17:09:46 +00:00
|
|
|
#![allow(dead_code)]
|
2023-05-12 15:58:01 +00:00
|
|
|
use std::{
|
|
|
|
error::Error,
|
|
|
|
time::{Duration, Instant},
|
|
|
|
};
|
2023-06-12 05:07:15 +00:00
|
|
|
|
2024-05-28 20:23:39 +00:00
|
|
|
use ratatui::{
|
2024-05-29 11:42:29 +00:00
|
|
|
backend::TermwizBackend,
|
2024-05-28 20:23:39 +00:00
|
|
|
termwiz::{
|
|
|
|
input::{InputEvent, KeyCode},
|
|
|
|
terminal::Terminal as TermwizTerminal,
|
|
|
|
},
|
2024-08-02 11:18:00 +00:00
|
|
|
Terminal,
|
2024-03-02 09:06:53 +00:00
|
|
|
};
|
2023-05-12 15:58:01 +00:00
|
|
|
|
|
|
|
use crate::{app::App, ui};
|
|
|
|
|
|
|
|
pub fn run(tick_rate: Duration, enhanced_graphics: bool) -> Result<(), Box<dyn Error>> {
|
|
|
|
let backend = TermwizBackend::new()?;
|
|
|
|
let mut terminal = Terminal::new(backend)?;
|
|
|
|
terminal.hide_cursor()?;
|
|
|
|
|
|
|
|
// create app and run it
|
|
|
|
let app = App::new("Termwiz Demo", enhanced_graphics);
|
feat(terminal): Add ratatui::init() and restore() methods (#1289)
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>
2024-08-22 12:16:35 +00:00
|
|
|
let app_result = run_app(&mut terminal, app, tick_rate);
|
2023-05-12 15:58:01 +00:00
|
|
|
|
|
|
|
terminal.show_cursor()?;
|
|
|
|
terminal.flush()?;
|
|
|
|
|
feat(terminal): Add ratatui::init() and restore() methods (#1289)
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>
2024-08-22 12:16:35 +00:00
|
|
|
if let Err(err) = app_result {
|
2023-05-22 03:46:02 +00:00
|
|
|
println!("{err:?}");
|
2023-05-12 15:58:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn run_app(
|
|
|
|
terminal: &mut Terminal<TermwizBackend>,
|
|
|
|
mut app: App,
|
|
|
|
tick_rate: Duration,
|
2023-10-20 19:31:52 +00:00
|
|
|
) -> Result<(), Box<dyn Error>> {
|
2023-05-12 15:58:01 +00:00
|
|
|
let mut last_tick = Instant::now();
|
|
|
|
loop {
|
feat(terminal): Add ratatui::init() and restore() methods (#1289)
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>
2024-08-22 12:16:35 +00:00
|
|
|
terminal.draw(|frame| ui::draw(frame, &mut app))?;
|
2023-05-12 15:58:01 +00:00
|
|
|
|
2023-10-20 19:31:52 +00:00
|
|
|
let timeout = tick_rate.saturating_sub(last_tick.elapsed());
|
|
|
|
if let Some(input) = terminal
|
2023-05-12 15:58:01 +00:00
|
|
|
.backend_mut()
|
|
|
|
.buffered_terminal_mut()
|
|
|
|
.terminal()
|
2023-10-20 19:31:52 +00:00
|
|
|
.poll_input(Some(timeout))?
|
2023-05-12 15:58:01 +00:00
|
|
|
{
|
|
|
|
match input {
|
|
|
|
InputEvent::Key(key_code) => match key_code.key {
|
2023-12-15 03:11:48 +00:00
|
|
|
KeyCode::UpArrow | KeyCode::Char('k') => app.on_up(),
|
|
|
|
KeyCode::DownArrow | KeyCode::Char('j') => app.on_down(),
|
|
|
|
KeyCode::LeftArrow | KeyCode::Char('h') => app.on_left(),
|
|
|
|
KeyCode::RightArrow | KeyCode::Char('l') => app.on_right(),
|
2023-05-12 15:58:01 +00:00
|
|
|
KeyCode::Char(c) => app.on_key(c),
|
|
|
|
_ => {}
|
|
|
|
},
|
|
|
|
InputEvent::Resized { cols, rows } => {
|
|
|
|
terminal
|
|
|
|
.backend_mut()
|
|
|
|
.buffered_terminal_mut()
|
|
|
|
.resize(cols, rows);
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if last_tick.elapsed() >= tick_rate {
|
|
|
|
app.on_tick();
|
|
|
|
last_tick = Instant::now();
|
|
|
|
}
|
|
|
|
if app.should_quit {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|