mirror of
https://github.com/ratatui-org/ratatui
synced 2024-11-22 04:33:13 +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"
|
||||
|
||||
[dev-dependencies]
|
||||
anyhow = "1.0.71"
|
||||
argh = "0.1.12"
|
||||
color-eyre = "0.6.2"
|
||||
criterion = { version = "0.5.1", features = ["html_reports"] }
|
||||
|
|
|
@ -18,7 +18,7 @@ use std::{
|
|||
time::Duration,
|
||||
};
|
||||
|
||||
use anyhow::{Context, Result};
|
||||
use color_eyre::{eyre::Context, Result};
|
||||
use ratatui::{
|
||||
backend::CrosstermBackend,
|
||||
crossterm::{
|
||||
|
@ -34,21 +34,20 @@ use ratatui::{
|
|||
/// this is not meant to be prescriptive. It is only meant to demonstrate the basic setup and
|
||||
/// teardown of a terminal application.
|
||||
///
|
||||
/// A more robust application would probably want to handle errors and ensure that the terminal is
|
||||
/// restored to a sane state before exiting. This example does not do that. It also does not handle
|
||||
/// events or update the application state. It just draws a greeting and exits when the user
|
||||
/// presses 'q'.
|
||||
/// This example does not handle events or update the application state. It just draws a greeting
|
||||
/// and exits when the user presses 'q'.
|
||||
fn main() -> Result<()> {
|
||||
let mut terminal = setup_terminal().context("setup failed")?;
|
||||
run(&mut terminal).context("app loop failed")?;
|
||||
restore_terminal(&mut terminal).context("restore terminal failed")?;
|
||||
Ok(())
|
||||
color_eyre::install()?; // augment errors / panics with easy to read messages
|
||||
let mut terminal = init_terminal().context("setup failed")?;
|
||||
let result = run(&mut terminal).context("app loop failed");
|
||||
restore_terminal();
|
||||
result
|
||||
}
|
||||
|
||||
/// 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
|
||||
/// want to handle errors and ensure that the terminal is restored to a sane state before exiting.
|
||||
fn setup_terminal() -> Result<Terminal<CrosstermBackend<Stdout>>> {
|
||||
/// hide the cursor. This example does not handle errors.
|
||||
fn init_terminal() -> Result<Terminal<CrosstermBackend<Stdout>>> {
|
||||
set_panic_hook();
|
||||
let mut stdout = io::stdout();
|
||||
enable_raw_mode().context("failed to enable raw mode")?;
|
||||
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
|
||||
/// the cursor.
|
||||
fn restore_terminal(terminal: &mut Terminal<CrosstermBackend<Stdout>>) -> Result<()> {
|
||||
disable_raw_mode().context("failed to disable raw mode")?;
|
||||
execute!(terminal.backend_mut(), LeaveAlternateScreen)
|
||||
.context("unable to switch to main screen")?;
|
||||
terminal.show_cursor().context("unable to show cursor")
|
||||
fn restore_terminal() {
|
||||
// There's not a lot we can do if these fail, so we just print an error message.
|
||||
if let Err(err) = disable_raw_mode() {
|
||||
eprintln!("Error disabling raw mode: {err}");
|
||||
}
|
||||
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
|
||||
|
@ -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.
|
||||
fn run(terminal: &mut Terminal<CrosstermBackend<Stdout>>) -> Result<()> {
|
||||
loop {
|
||||
terminal.draw(crate::render_app)?;
|
||||
terminal.draw(render_app)?;
|
||||
if should_quit()? {
|
||||
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
|
||||
/// 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
|
||||
/// 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> {
|
||||
if event::poll(Duration::from_millis(250)).context("event poll failed")? {
|
||||
if let Event::Key(key) = event::read().context("event read failed")? {
|
||||
|
|
Loading…
Reference in a new issue