Skip to content

Commit

Permalink
fix(tui): reset terminal no matter what (#1175)
Browse files Browse the repository at this point in the history
Currently if you run `rustic snapshots -i` without a repository
configured `rustic` will leave the terminal in raw mode.

This is a simple fix to make sure the terminal is properly reset in all
situations.

close #1166

Co-authored-by: aawsome <37850842+aawsome@users.noreply.github.com>
  • Loading branch information
drdo and aawsome authored Jul 10, 2024
1 parent 599e61a commit cb8c376
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 8 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ aho-corasick = "1.1.2"
chrono = { version = "0.4", default-features = false, features = ["clock", "serde"] }
comfy-table = "7.1.0"
rhai = { version = "1.17", features = ["sync", "serde", "no_optimize", "no_module", "no_custom_syntax", "only_i64"] }
scopeguard = "1.2"
semver = { version = "1", optional = true }
simplelog = "0.12"

Expand Down
12 changes: 4 additions & 8 deletions src/commands/tui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ mod widgets;

use crossterm::event::{KeyEvent, KeyModifiers};
use progress::TuiProgressBars;
use scopeguard::defer;
use snapshots::Snapshots;

use std::io;
Expand Down Expand Up @@ -36,12 +37,10 @@ pub fn run(group_by: SnapshotGroupCriterion) -> Result<()> {
let terminal = init_terminal()?;
let terminal = Arc::new(RwLock::new(terminal));

let original_hook = std::panic::take_hook();

std::panic::set_hook(Box::new(move |panic| {
// restore terminal (even when leaving through ?, early return, or panic)
defer! {
reset_terminal().unwrap();
original_hook(panic);
}));
}

let progress = TuiProgressBars {
terminal: terminal.clone(),
Expand All @@ -54,9 +53,6 @@ pub fn run(group_by: SnapshotGroupCriterion) -> Result<()> {
let app = App { snapshots };
let res = run_app(terminal, app);

// restore terminal
reset_terminal()?;

if let Err(err) = res {
println!("{err:?}");
}
Expand Down

0 comments on commit cb8c376

Please sign in to comment.