From 3eaccbb61a730e0735ac151266e15fae79353f9d Mon Sep 17 00:00:00 2001 From: mo8it Date: Sat, 17 Aug 2024 16:49:07 +0200 Subject: [PATCH] Restore the terminal after an error in the list --- src/list.rs | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/src/list.rs b/src/list.rs index 74ae0a26..f28230e4 100644 --- a/src/list.rs +++ b/src/list.rs @@ -8,7 +8,7 @@ use ratatui::{ }, Terminal, }; -use std::io::{self, Write}; +use std::io::{self, StdoutLock, Write}; use crate::app_state::AppState; @@ -16,15 +16,8 @@ use self::state::{Filter, UiState}; mod state; -pub fn list(app_state: &mut AppState) -> Result<()> { - let mut stdout = io::stdout().lock(); - stdout - .queue(EnterAlternateScreen)? - .queue(EnableMouseCapture)? - .flush()?; - enable_raw_mode()?; - - let mut terminal = Terminal::new(CrosstermBackend::new(&mut stdout))?; +fn handle_list(app_state: &mut AppState, stdout: &mut StdoutLock) -> Result<()> { + let mut terminal = Terminal::new(CrosstermBackend::new(stdout))?; terminal.clear()?; let mut ui_state = UiState::new(app_state); @@ -88,12 +81,25 @@ pub fn list(app_state: &mut AppState) -> Result<()> { } } - drop(terminal); + Ok(()) +} + +pub fn list(app_state: &mut AppState) -> Result<()> { + let mut stdout = io::stdout().lock(); + stdout + .queue(EnterAlternateScreen)? + .queue(EnableMouseCapture)? + .flush()?; + enable_raw_mode()?; + + let res = handle_list(app_state, &mut stdout); + + // Restore the terminal even if we got an error. stdout .queue(LeaveAlternateScreen)? .queue(DisableMouseCapture)? .flush()?; disable_raw_mode()?; - Ok(()) + res }