better cleanup implemented

This commit is contained in:
filipriec
2025-03-20 22:59:00 +01:00
parent 6d87707ecf
commit d3f6296d67

View File

@@ -6,7 +6,7 @@ use crossterm::{
};
use crossterm::cursor::{SetCursorStyle, EnableBlinking};
use ratatui::{backend::CrosstermBackend, Terminal};
use std::io::{self, stdout};
use std::io::{self, stdout, Write};
use tonic::transport::Channel;
use ratatui::backend::Backend;
@@ -73,20 +73,32 @@ impl AppTerminal {
}
pub fn cleanup(&mut self) -> Result<(), Box<dyn std::error::Error>> {
// Disable raw mode first
let _ = disable_raw_mode(); // Ignore errors as we're cleaning up
// Execute cleanup commands
// Get a separate stdout handle for cleanup operations
let mut stdout = stdout();
// Step 1: Show cursor first (most important for user experience)
execute!(stdout, crossterm::cursor::Show)?;
// Step 2: Reset cursor style to default
execute!(stdout, crossterm::cursor::SetCursorStyle::DefaultUserShape)?;
// Step 3: Leave alternate screen mode
execute!(stdout, crossterm::terminal::LeaveAlternateScreen)?;
// Step 4: Disable raw mode
disable_raw_mode()?;
// Step 5: Flush all pending changes to ensure they're applied
stdout.flush()?;
// Step 6: Final reset - clear screen and move cursor to home position
// This ensures terminal is in a known good state
execute!(
self.terminal.backend_mut(),
crossterm::terminal::LeaveAlternateScreen,
crossterm::cursor::SetCursorStyle::DefaultUserShape,
crossterm::cursor::Show,
stdout,
crossterm::terminal::Clear(crossterm::terminal::ClearType::All),
crossterm::cursor::MoveTo(0, 0)
)?;
// Ensure all commands are flushed using the Backend trait's flush
self.terminal.backend_mut().flush()?;
Ok(())
}