mirror of
https://github.com/ratatui-org/ratatui
synced 2024-11-22 12:43:16 +00:00
chore(deps): remove anyhow from dev dependencies (#1305)
Co-authored-by: Orhun Parmaksız <orhunparmaksiz@gmail.com>
This commit is contained in:
parent
5f7a7fbe19
commit
69e8ed7db8
2 changed files with 32 additions and 20 deletions
|
@ -45,7 +45,6 @@ unicode-truncate = "1"
|
||||||
unicode-width = "0.1.13"
|
unicode-width = "0.1.13"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
anyhow = "1.0.71"
|
|
||||||
argh = "0.1.12"
|
argh = "0.1.12"
|
||||||
color-eyre = "0.6.2"
|
color-eyre = "0.6.2"
|
||||||
criterion = { version = "0.5.1", features = ["html_reports"] }
|
criterion = { version = "0.5.1", features = ["html_reports"] }
|
||||||
|
|
|
@ -18,7 +18,7 @@ use std::{
|
||||||
time::Duration,
|
time::Duration,
|
||||||
};
|
};
|
||||||
|
|
||||||
use anyhow::{Context, Result};
|
use color_eyre::{eyre::Context, Result};
|
||||||
use ratatui::{
|
use ratatui::{
|
||||||
backend::CrosstermBackend,
|
backend::CrosstermBackend,
|
||||||
crossterm::{
|
crossterm::{
|
||||||
|
@ -34,21 +34,20 @@ use ratatui::{
|
||||||
/// this is not meant to be prescriptive. It is only meant to demonstrate the basic setup and
|
/// this is not meant to be prescriptive. It is only meant to demonstrate the basic setup and
|
||||||
/// teardown of a terminal application.
|
/// teardown of a terminal application.
|
||||||
///
|
///
|
||||||
/// A more robust application would probably want to handle errors and ensure that the terminal is
|
/// This example does not handle events or update the application state. It just draws a greeting
|
||||||
/// restored to a sane state before exiting. This example does not do that. It also does not handle
|
/// and exits when the user presses 'q'.
|
||||||
/// events or update the application state. It just draws a greeting and exits when the user
|
|
||||||
/// presses 'q'.
|
|
||||||
fn main() -> Result<()> {
|
fn main() -> Result<()> {
|
||||||
let mut terminal = setup_terminal().context("setup failed")?;
|
color_eyre::install()?; // augment errors / panics with easy to read messages
|
||||||
run(&mut terminal).context("app loop failed")?;
|
let mut terminal = init_terminal().context("setup failed")?;
|
||||||
restore_terminal(&mut terminal).context("restore terminal failed")?;
|
let result = run(&mut terminal).context("app loop failed");
|
||||||
Ok(())
|
restore_terminal();
|
||||||
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Setup the terminal. This is where you would enable raw mode, enter the alternate screen, and
|
/// Setup the terminal. This is where you would enable raw mode, enter the alternate screen, and
|
||||||
/// hide the cursor. This example does not handle errors. A more robust application would probably
|
/// hide the cursor. This example does not handle errors.
|
||||||
/// want to handle errors and ensure that the terminal is restored to a sane state before exiting.
|
fn init_terminal() -> Result<Terminal<CrosstermBackend<Stdout>>> {
|
||||||
fn setup_terminal() -> Result<Terminal<CrosstermBackend<Stdout>>> {
|
set_panic_hook();
|
||||||
let mut stdout = io::stdout();
|
let mut stdout = io::stdout();
|
||||||
enable_raw_mode().context("failed to enable raw mode")?;
|
enable_raw_mode().context("failed to enable raw mode")?;
|
||||||
execute!(stdout, EnterAlternateScreen).context("unable to enter alternate screen")?;
|
execute!(stdout, EnterAlternateScreen).context("unable to enter alternate screen")?;
|
||||||
|
@ -57,11 +56,23 @@ fn setup_terminal() -> Result<Terminal<CrosstermBackend<Stdout>>> {
|
||||||
|
|
||||||
/// Restore the terminal. This is where you disable raw mode, leave the alternate screen, and show
|
/// Restore the terminal. This is where you disable raw mode, leave the alternate screen, and show
|
||||||
/// the cursor.
|
/// the cursor.
|
||||||
fn restore_terminal(terminal: &mut Terminal<CrosstermBackend<Stdout>>) -> Result<()> {
|
fn restore_terminal() {
|
||||||
disable_raw_mode().context("failed to disable raw mode")?;
|
// There's not a lot we can do if these fail, so we just print an error message.
|
||||||
execute!(terminal.backend_mut(), LeaveAlternateScreen)
|
if let Err(err) = disable_raw_mode() {
|
||||||
.context("unable to switch to main screen")?;
|
eprintln!("Error disabling raw mode: {err}");
|
||||||
terminal.show_cursor().context("unable to show cursor")
|
}
|
||||||
|
if let Err(err) = execute!(io::stdout(), LeaveAlternateScreen) {
|
||||||
|
eprintln!("Error leaving alternate screen: {err}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Replace the default panic hook with one that restores the terminal before panicking.
|
||||||
|
fn set_panic_hook() {
|
||||||
|
let hook = std::panic::take_hook();
|
||||||
|
std::panic::set_hook(Box::new(move |panic_info| {
|
||||||
|
restore_terminal();
|
||||||
|
hook(panic_info);
|
||||||
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Run the application loop. This is where you would handle events and update the application
|
/// Run the application loop. This is where you would handle events and update the application
|
||||||
|
@ -70,7 +81,7 @@ fn restore_terminal(terminal: &mut Terminal<CrosstermBackend<Stdout>>) -> Result
|
||||||
/// on events, or you could have a single application state and update it based on events.
|
/// on events, or you could have a single application state and update it based on events.
|
||||||
fn run(terminal: &mut Terminal<CrosstermBackend<Stdout>>) -> Result<()> {
|
fn run(terminal: &mut Terminal<CrosstermBackend<Stdout>>) -> Result<()> {
|
||||||
loop {
|
loop {
|
||||||
terminal.draw(crate::render_app)?;
|
terminal.draw(render_app)?;
|
||||||
if should_quit()? {
|
if should_quit()? {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -88,7 +99,9 @@ fn render_app(frame: &mut Frame) {
|
||||||
/// Check if the user has pressed 'q'. This is where you would handle events. This example just
|
/// Check if the user has pressed 'q'. This is where you would handle events. This example just
|
||||||
/// checks if the user has pressed 'q' and returns true if they have. It does not handle any other
|
/// checks if the user has pressed 'q' and returns true if they have. It does not handle any other
|
||||||
/// events. There is a 250ms timeout on the event poll so that the application can exit in a timely
|
/// events. There is a 250ms timeout on the event poll so that the application can exit in a timely
|
||||||
/// manner, and to ensure that the terminal is rendered at least once every 250ms.
|
/// manner, and to ensure that the terminal is rendered at least once every 250ms. This allows you
|
||||||
|
/// to do other work in the application loop, such as updating the application state, without
|
||||||
|
/// blocking the event loop for too long.
|
||||||
fn should_quit() -> Result<bool> {
|
fn should_quit() -> Result<bool> {
|
||||||
if event::poll(Duration::from_millis(250)).context("event poll failed")? {
|
if event::poll(Duration::from_millis(250)).context("event poll failed")? {
|
||||||
if let Event::Key(key) = event::read().context("event read failed")? {
|
if let Event::Key(key) = event::read().context("event read failed")? {
|
||||||
|
|
Loading…
Reference in a new issue