Skip to content

Commit

Permalink
feat: Restore the cursor on panic
Browse files Browse the repository at this point in the history
  • Loading branch information
orhun committed Sep 10, 2023
1 parent 51f3a75 commit a0d832b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ fn main() -> Result<()> {
}
}
// Exit the user interface.
tui.exit()?;
Tui::<CrosstermBackend<io::Stderr>>::reset()?;
// Print the exit message if any.
if let Some(message) = app.state.exit_message {
writeln!(io::stdout(), "{message}")?;
Expand Down
16 changes: 5 additions & 11 deletions src/term/tui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::term::event::EventHandler;
use anyhow::{Context, Result};
use crossterm::event::{DisableMouseCapture, EnableMouseCapture};
use crossterm::terminal::{self, EnterAlternateScreen, LeaveAlternateScreen};
use ratatui::backend::Backend;
use ratatui::backend::{Backend, CrosstermBackend};
use ratatui::Terminal;
use std::io;
use std::panic;
Expand Down Expand Up @@ -69,7 +69,7 @@ impl<B: Backend> Tui<B> {
pub fn toggle_pause(&mut self) -> Result<()> {
self.paused = !self.paused;
if self.paused {
self.exit()?;
Self::reset()?;
} else {
self.init()?;
}
Expand Down Expand Up @@ -100,23 +100,17 @@ impl<B: Backend> Tui<B> {
Ok(())
}

/// Exits the terminal interface.
/// Reset the terminal interface.
///
/// It disables the raw mode and reverts back the terminal properties.
pub fn exit(&mut self) -> Result<()> {
Self::reset()?;
self.terminal.show_cursor()?;
Ok(())
}

/// Resets the terminal interface.
fn reset() -> Result<()> {
pub fn reset() -> Result<()> {
terminal::disable_raw_mode()?;
crossterm::execute!(
io::stderr(),
LeaveAlternateScreen,
DisableMouseCapture
)?;
Terminal::new(CrosstermBackend::new(io::stderr()))?.show_cursor()?;
Ok(())
}
}
Expand Down

0 comments on commit a0d832b

Please sign in to comment.