-
Notifications
You must be signed in to change notification settings - Fork 328
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* pass signals to tasks * [MegaLinter] Apply linters fixes --------- Co-authored-by: jdx <[email protected]>
- Loading branch information
Showing
11 changed files
with
123 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
use console::Term; | ||
use signal_hook::consts::SIGINT; | ||
use signal_hook::iterator::{Handle, Signals}; | ||
use std::process::exit; | ||
use std::sync::atomic::{AtomicBool, Ordering}; | ||
use std::thread; | ||
|
||
#[derive(Debug)] | ||
pub struct HandleGuard(Handle); | ||
|
||
/// ensures cursor is displayed on ctrl-c | ||
pub fn handle_ctrlc() -> eyre::Result<Option<HandleGuard>> { | ||
static HANDLED: AtomicBool = AtomicBool::new(false); | ||
let handled = HANDLED.swap(true, Ordering::Relaxed); | ||
if handled { | ||
return Ok(None); | ||
} | ||
|
||
let mut signals = Signals::new([SIGINT])?; | ||
let handle = HandleGuard(signals.handle()); | ||
thread::spawn(move || { | ||
if signals.into_iter().next().is_some() { | ||
let _ = Term::stderr().show_cursor(); | ||
debug!("Ctrl-C pressed, exiting..."); | ||
exit(1); | ||
} | ||
HANDLED.store(false, Ordering::Relaxed); | ||
}); | ||
Ok(Some(handle)) | ||
} | ||
|
||
impl Drop for HandleGuard { | ||
fn drop(&mut self) { | ||
self.0.close(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,9 @@ | ||
use std::process::exit; | ||
use std::sync::Once; | ||
|
||
use console::{user_attended_stderr, Term}; | ||
|
||
pub use prompt::confirm; | ||
|
||
pub mod ctrlc; | ||
pub mod multi_progress_report; | ||
pub mod progress_report; | ||
pub mod prompt; | ||
pub mod style; | ||
pub mod table; | ||
pub mod tree; | ||
|
||
pub fn handle_ctrlc() { | ||
static ONCE: Once = Once::new(); | ||
ONCE.call_once(|| { | ||
if user_attended_stderr() { | ||
let _ = ctrlc::set_handler(move || { | ||
let _ = Term::stderr().show_cursor(); | ||
debug!("Ctrl-C pressed, exiting..."); | ||
exit(1); | ||
}); | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,18 @@ | ||
use std::io; | ||
use std::sync::Mutex; | ||
|
||
use demand::Confirm; | ||
|
||
use crate::ui; | ||
use crate::ui::ctrlc; | ||
|
||
static MUTEX: Mutex<()> = Mutex::new(()); | ||
|
||
pub fn confirm<S: Into<String>>(message: S) -> io::Result<bool> { | ||
pub fn confirm<S: Into<String>>(message: S) -> eyre::Result<bool> { | ||
let _lock = MUTEX.lock().unwrap(); // Prevent multiple prompts at once | ||
ui::handle_ctrlc(); | ||
let _ = ctrlc::handle_ctrlc()?; | ||
|
||
if !console::user_attended_stderr() { | ||
return Ok(false); | ||
} | ||
Confirm::new(message).run() | ||
let result = Confirm::new(message).run()?; | ||
Ok(result) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
let i = 3 | ||
|
||
process.on('SIGINT', function () { | ||
if (i > 0) { | ||
console.log(`Got SIGINT. Press Control-D to exit. ${i} times left`) | ||
i-- | ||
} else { | ||
process.exit() | ||
} | ||
}) | ||
|
||
// wait for 60 seconds | ||
setTimeout(function () {}, 60000) | ||
console.log('Running. Press Control-C to test.') |