From 36fea05b0c7ba402a659470533585aab77fb74b5 Mon Sep 17 00:00:00 2001 From: Robert Date: Sun, 1 Dec 2024 18:05:58 +0000 Subject: [PATCH] feat: simplify initial terminal interactions --- src/main.rs | 9 +++++---- src/terminal.rs | 25 ++----------------------- 2 files changed, 7 insertions(+), 27 deletions(-) diff --git a/src/main.rs b/src/main.rs index 3aaf409..48ab5c6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -47,10 +47,10 @@ async fn main() -> color_eyre::Result<()> { let docker = new_local_docker_connection(&config.docker_path) .await .context(format!("failed to create docker connection, potentially due to misconfiguration (see {CONFIGURATION_DOC_PATH})"))?; - terminal::init_panic_hook(); - let mut terminal = terminal::init().context("failed to initialise terminal")?; + let mut terminal = ratatui::init(); + terminal.clear()?; let mut events = EventLoop::new(); let events_tx = events.get_tx(); @@ -83,7 +83,8 @@ async fn main() -> color_eyre::Result<()> { } Message::Transition(t) => { if t == events::Transition::ToNewTerminal { - terminal = terminal::init().context("failed to initialise terminal")?; + terminal = ratatui::init(); + terminal.clear()?; } else { let _ = &app.transition(t).await; } @@ -99,7 +100,7 @@ async fn main() -> color_eyre::Result<()> { } } - terminal::restore().context("failed to restore terminal")?; + ratatui::restore(); Ok(()) } diff --git a/src/terminal.rs b/src/terminal.rs index cb35cda..8a8b888 100644 --- a/src/terminal.rs +++ b/src/terminal.rs @@ -1,31 +1,10 @@ -use crossterm::{ - terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen}, - ExecutableCommand, -}; -use ratatui::prelude::*; -use std::{ - io::stdout, - panic::{set_hook, take_hook}, -}; - -pub fn init() -> std::io::Result> { - enable_raw_mode()?; - stdout().execute(EnterAlternateScreen)?; - let terminal = Terminal::new(CrosstermBackend::new(stdout()))?; - Ok(terminal) -} - -pub fn restore() -> std::io::Result<()> { - stdout().execute(LeaveAlternateScreen)?; - disable_raw_mode()?; - Ok(()) -} +use std::panic::{set_hook, take_hook}; pub fn init_panic_hook() { let original_hook = take_hook(); set_hook(Box::new(move |panic_info| { // intentionally ignore errors here since we're already in a panic - let _ = restore(); + let _ = ratatui::restore(); original_hook(panic_info); })); }