From e966c5ad5b4197bda5aae509733573820f20873a Mon Sep 17 00:00:00 2001 From: Andrew Hlynskyi Date: Tue, 2 May 2023 10:34:55 +0300 Subject: [PATCH] fix: use SIGINT instead of stdin for interruption to don't mess up piped commands --- Cargo.lock | 67 +++++++++++++++++++++++++++++++++----------- cli/Cargo.toml | 1 + cli/src/highlight.rs | 2 +- cli/src/main.rs | 4 +-- cli/src/tags.rs | 2 +- cli/src/util.rs | 21 ++++++-------- 6 files changed, 64 insertions(+), 33 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9b74a638..0588a58d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4,9 +4,9 @@ version = 3 [[package]] name = "aho-corasick" -version = "0.7.20" +version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cc936419f96fa211c1b9166887b38e5e40b19958e5b895be7c1f93adec7071ac" +checksum = "67fc08ce920c31afb70f013dcce1bfc3a3195de6a228474e45e1f145b36f8d04" dependencies = [ "memchr", ] @@ -22,9 +22,9 @@ dependencies = [ [[package]] name = "anyhow" -version = "1.0.70" +version = "1.0.71" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7de8ce5e0f9f8d88245311066a578d72b7af3e7088f32783804676302df237e4" +checksum = "9c7d0618f0e0b7e8ff11427422b64564d5fb0be1940354bfe2e0529b18a9d9b8" [[package]] name = "ascii" @@ -57,9 +57,9 @@ checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" [[package]] name = "bumpalo" -version = "3.12.0" +version = "3.12.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0d261e256854913907f67ed06efbc3338dfe6179796deefc1ff763fc1aee5535" +checksum = "9b1ce199063694f33ffb7dd4e0ee620741495c32833cde5aa08f02a0bf96f0c8" [[package]] name = "bytes" @@ -142,6 +142,16 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "ctrlc" +version = "3.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbcf33c2a618cbe41ee43ae6e9f2e48368cd9f9db2896f10167d8d762679f639" +dependencies = [ + "nix", + "windows-sys 0.45.0", +] + [[package]] name = "diff" version = "0.1.13" @@ -372,9 +382,9 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" [[package]] name = "libc" -version = "0.2.141" +version = "0.2.142" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3304a64d199bb964be99741b7a14d26972741915b3649639149b2479bb46f4b5" +checksum = "6a987beff54b60ffa6d51982e1aa1146bc42f19bd26be28b0586f252fccf5317" [[package]] name = "libloading" @@ -388,9 +398,9 @@ dependencies = [ [[package]] name = "linux-raw-sys" -version = "0.3.1" +version = "0.3.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d59d8c75012853d2e872fb56bc8a2e53718e2cafe1a4c823143141c6d90c322f" +checksum = "b64f40e5e03e0d54f03845c8197d0291253cdbedfb1cb46b13c2c117554a9f4c" [[package]] name = "log" @@ -422,6 +432,18 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "27b02d87554356db9e9a873add8782d4ea6e3e58ea071a9adb9a2e8ddb884a8b" +[[package]] +name = "nix" +version = "0.26.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bfdda3d196821d6af13126e40375cdf7da646a96114af134d5f417a9a1dc8e1a" +dependencies = [ + "bitflags", + "cfg-if", + "libc", + "static_assertions", +] + [[package]] name = "objc" version = "0.2.7" @@ -561,13 +583,13 @@ dependencies = [ [[package]] name = "regex" -version = "1.7.3" +version = "1.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8b1f693b24f6ac912f4893ef08244d70b6067480d2f1a46e950c9691e6749d1d" +checksum = "af83e617f331cc6ae2da5443c602dfa5af81e517212d9d611a5b3ba1777b5370" dependencies = [ "aho-corasick", "memchr", - "regex-syntax", + "regex-syntax 0.7.1", ] [[package]] @@ -576,6 +598,12 @@ version = "0.6.29" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f162c6dd7b008981e4d40210aca20b4bd0f9b60ca9271061b07f78537722f2e1" +[[package]] +name = "regex-syntax" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a5996294f19bd3aae0453a862ad728f60e6600695733dd5df01da90c54363a3c" + [[package]] name = "rustc-hash" version = "1.1.0" @@ -584,9 +612,9 @@ checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2" [[package]] name = "rustix" -version = "0.37.11" +version = "0.37.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "85597d61f83914ddeba6a47b3b8ffe7365107221c2e557ed94426489fefb5f77" +checksum = "8bbfc1d1c7c40c01715f47d71444744a81669ca84e8b63e25a55e169b1f86433" dependencies = [ "bitflags", "errno", @@ -655,6 +683,12 @@ version = "2.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "75ce4f9dc4a41b4c3476cc925f1efb11b66df373a8fde5d4b8915fa91b5d995e" +[[package]] +name = "static_assertions" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" + [[package]] name = "strsim" version = "0.8.0" @@ -779,6 +813,7 @@ dependencies = [ "atty", "clap", "ctor", + "ctrlc", "difference", "dirs 3.0.2", "glob", @@ -791,7 +826,7 @@ dependencies = [ "pretty_assertions", "rand", "regex", - "regex-syntax", + "regex-syntax 0.6.29", "rustc-hash", "semver", "serde", diff --git a/cli/Cargo.toml b/cli/Cargo.toml index 28e3d37f..d0085f67 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -24,6 +24,7 @@ ansi_term = "0.12" anyhow = "1.0" atty = "0.2" clap = "2.32" +ctrlc = { version = "3.2.5", features = ["termination"] } difference = "2.0" dirs = "3.0" glob = "0.3.0" diff --git a/cli/src/highlight.rs b/cli/src/highlight.rs index 6cf1580e..e16a705c 100644 --- a/cli/src/highlight.rs +++ b/cli/src/highlight.rs @@ -391,7 +391,7 @@ pub fn html( let stdout = io::stdout(); let mut stdout = stdout.lock(); let time = Instant::now(); - let cancellation_flag = util::cancel_on_stdin(); + let cancellation_flag = util::cancel_on_signal(); let mut highlighter = Highlighter::new(); let events = highlighter.highlight(config, source, Some(&cancellation_flag), |string| { diff --git a/cli/src/main.rs b/cli/src/main.rs index fb2a6327..0a863b1c 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -403,7 +403,7 @@ fn run() -> Result<()> { let edits = matches .values_of("edits") .map_or(Vec::new(), |e| e.collect()); - let cancellation_flag = util::cancel_on_stdin(); + let cancellation_flag = util::cancel_on_signal(); if debug { // For augmenting debug logging in external scanners @@ -530,7 +530,7 @@ fn run() -> Result<()> { println!("{}", highlight::HTML_HEADER); } - let cancellation_flag = util::cancel_on_stdin(); + let cancellation_flag = util::cancel_on_signal(); let mut lang = None; if let Some(scope) = matches.value_of("scope") { diff --git a/cli/src/tags.rs b/cli/src/tags.rs index 457955dd..f9f788ab 100644 --- a/cli/src/tags.rs +++ b/cli/src/tags.rs @@ -23,7 +23,7 @@ pub fn generate_tags( } let mut context = TagsContext::new(); - let cancellation_flag = util::cancel_on_stdin(); + let cancellation_flag = util::cancel_on_signal(); let stdout = io::stdout(); let mut stdout = stdout.lock(); diff --git a/cli/src/util.rs b/cli/src/util.rs index d180cd54..0793e525 100644 --- a/cli/src/util.rs +++ b/cli/src/util.rs @@ -1,8 +1,6 @@ use anyhow::Result; -use std::io; use std::sync::atomic::{AtomicUsize, Ordering}; use std::sync::Arc; -use std::thread; use tree_sitter::{Parser, Tree}; #[cfg(unix)] @@ -22,18 +20,15 @@ svg { width: 100%; } "; -pub fn cancel_on_stdin() -> Arc { +pub fn cancel_on_signal() -> Arc { let result = Arc::new(AtomicUsize::new(0)); - if atty::is(atty::Stream::Stdin) { - thread::spawn({ - let flag = result.clone(); - move || { - let mut line = String::new(); - io::stdin().read_line(&mut line).unwrap(); - flag.store(1, Ordering::Relaxed); - } - }); - } + ctrlc::set_handler({ + let flag = result.clone(); + move || { + flag.store(1, Ordering::Relaxed); + } + }) + .expect("Error setting Ctrl-C handler"); result }