Skip to content

Commit

Permalink
Merge pull request #180 from nix-community/ctrlc
Browse files Browse the repository at this point in the history
review: reset the terminal when interrupted
  • Loading branch information
figsoda authored Nov 17, 2024
2 parents 8e741a6 + 48ae93f commit 1ba60e2
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 3 deletions.
29 changes: 29 additions & 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 @@ -14,6 +14,7 @@ categories = ["command-line-utilities", "development-tools::testing"]
[dependencies]
bstr = "1.11.0"
color-eyre = "0.6.3"
ctrlc = "3.4.5"
dialoguer = "0.11.0"
eyre = "0.6.12"
monostate = "0.1.13"
Expand Down
22 changes: 19 additions & 3 deletions src/cmd/review.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
use std::{
ffi::OsStr,
fs::{read_dir, remove_file, rename, File},
io::{stderr, BufRead, Write},
io::{self, stderr, BufRead, Write},
path::Path,
process::exit,
thread::sleep,
time::Duration,
};

use dialoguer::{theme::ColorfulTheme, Select};
use dialoguer::{console::Term, theme::ColorfulTheme, Select};
use eyre::{eyre, Result};
use owo_colors::OwoColorize;
use similar::{ChangeTag, TextDiff};
Expand All @@ -19,6 +22,12 @@ use crate::{

pub fn review(opts: Opts, cfg: Option<Config>) -> Result<()> {
let output = nix_eval(opts, cfg)?;
let _ = ctrlc::set_handler(|| {
let mut term = Term::stderr();
let _ = term.show_cursor();
let _ = writeln!(term, "interrupted");
exit(0);
});

for line in output.stderr.lines() {
let line = line?;
Expand Down Expand Up @@ -115,7 +124,14 @@ fn ask(name: &OsStr, old: &Path, new: &Path) -> Result<()> {
.item("skip".blue())
.default(0)
.with_prompt(format!("Review {}", name.to_string_lossy()))
.interact()?;
.interact()
.inspect_err(|dialoguer::Error::IO(e)| {
if e.kind() == io::ErrorKind::Interrupted {
// make sure ctrlc handler has reset the terminal
sleep(Duration::from_millis(16));
exit(0);
}
})?;

match choice {
0 => rename(new, old).map_err(Into::into),
Expand Down

0 comments on commit 1ba60e2

Please sign in to comment.