From a480324a2b3c0b7566c78e1fe4f923cadf392fbb Mon Sep 17 00:00:00 2001 From: iff Date: Wed, 9 Aug 2023 21:04:59 +0200 Subject: [PATCH] fix: add a timeout to skip intentional canceled task --- src/suggestions.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/suggestions.rs b/src/suggestions.rs index f4d9d51..625dce3 100644 --- a/src/suggestions.rs +++ b/src/suggestions.rs @@ -1,4 +1,5 @@ -use std::process::Stdio; +use std::process::{Stdio, exit}; +use std::time::{Instant, Duration}; use regex_lite::Regex; @@ -194,6 +195,8 @@ pub fn confirm_suggestion(shell: &str, command: &str, highlighted: &str) -> Resu let _p = p.to_owned() + " "; if command.starts_with(&_p) { let command = command.replacen(p, "", 1); + + let now = Instant::now(); let process = std::process::Command::new(p) .arg(shell) .arg("-c") @@ -208,6 +211,9 @@ pub fn confirm_suggestion(shell: &str, command: &str, highlighted: &str) -> Resu if process.success() { return Ok(()); } else { + if now.elapsed() > Duration::from_secs(3) { + exit(1); + } let process = std::process::Command::new(p) .arg(shell) .arg("-c") @@ -220,6 +226,7 @@ pub fn confirm_suggestion(shell: &str, command: &str, highlighted: &str) -> Resu } } + let now = Instant::now(); let process = std::process::Command::new(shell) .arg("-c") .arg(command) @@ -233,6 +240,9 @@ pub fn confirm_suggestion(shell: &str, command: &str, highlighted: &str) -> Resu if process.success() { Ok(()) } else { + if now.elapsed() > Duration::from_secs(3) { + exit(1); + } let process = std::process::Command::new(shell) .arg("-c") .arg(command)