terminal cleanup

This commit is contained in:
filipriec
2025-03-20 22:09:04 +01:00
parent 5d5208705f
commit 6d87707ecf

View File

@@ -2,12 +2,13 @@
use crossterm::event::{self, Event}; use crossterm::event::{self, Event};
use crossterm::{ use crossterm::{
execute, execute,
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen}, terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen},
}; };
use crossterm::cursor::{SetCursorStyle, EnableBlinking}; use crossterm::cursor::{SetCursorStyle, EnableBlinking};
use ratatui::{backend::CrosstermBackend, Terminal}; use ratatui::{backend::CrosstermBackend, Terminal};
use std::io::{self, stdout}; use std::io::{self, stdout};
use tonic::transport::Channel; use tonic::transport::Channel;
use ratatui::backend::Backend;
// Import the correct clients and proto messages from their respective modules // Import the correct clients and proto messages from their respective modules
use common::proto::multieko2::adresar::adresar_client::AdresarClient; use common::proto::multieko2::adresar::adresar_client::AdresarClient;
@@ -22,6 +23,12 @@ pub struct AppTerminal {
table_structure_client: TableStructureServiceClient<Channel>, table_structure_client: TableStructureServiceClient<Channel>,
} }
impl Drop for AppTerminal {
fn drop(&mut self) {
let _ = self.cleanup(); // Best-effort cleanup during drop
}
}
impl AppTerminal { impl AppTerminal {
pub fn set_cursor_style( pub fn set_cursor_style(
&mut self, &mut self,
@@ -66,8 +73,20 @@ impl AppTerminal {
} }
pub fn cleanup(&mut self) -> Result<(), Box<dyn std::error::Error>> { pub fn cleanup(&mut self) -> Result<(), Box<dyn std::error::Error>> {
disable_raw_mode()?; // Disable raw mode first
execute!(self.terminal.backend_mut(), LeaveAlternateScreen)?; let _ = disable_raw_mode(); // Ignore errors as we're cleaning up
// Execute cleanup commands
execute!(
self.terminal.backend_mut(),
crossterm::terminal::LeaveAlternateScreen,
crossterm::cursor::SetCursorStyle::DefaultUserShape,
crossterm::cursor::Show,
)?;
// Ensure all commands are flushed using the Backend trait's flush
self.terminal.backend_mut().flush()?;
Ok(()) Ok(())
} }