Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,21 @@ struct Args {
path: String,
}

/// Guard that ensures term settings are restored upon program exit
struct CleanupGuard;

impl Drop for CleanupGuard {
fn drop(&mut self) {
tui::cleanup();
}
}

#[tokio::main]
async fn main() -> Result<()> {
// Create cleanup guard - will automatically reset term settings
// on program exit, even on panic and error
let _cleanup = CleanupGuard;

// Parse arguments
let args = Args::parse();

Expand Down Expand Up @@ -93,7 +106,6 @@ async fn main() -> Result<()> {
_ = terminal_events(&mut events, status.clone()) => {}
}

ratatui::restore();
Ok(())
}

Expand Down
7 changes: 7 additions & 0 deletions src/tui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ pub use throbbing::Throbbing;

use std::sync::{LazyLock, Mutex};

use crossterm::{cursor, execute};
use ratatui::DefaultTerminal;

pub static TERMINAL: LazyLock<Mutex<DefaultTerminal>> = LazyLock::new(|| ratatui::init().into());

/// Cleanup function to restore terminal state
pub fn cleanup() {
ratatui::restore();
let _ = execute!(std::io::stdout(), cursor::Show);
}