-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
186 additions
and
36 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
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
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 |
---|---|---|
|
@@ -2,6 +2,20 @@ use std::env::args; | |
|
||
use demand::{Confirm, DemandOption, Input, MultiSelect, Theme}; | ||
|
||
fn handle_run<T>(result: Result<T, std::io::Error>) -> T { | ||
match result { | ||
Ok(value) => value, | ||
Err(e) => { | ||
if e.kind() == std::io::ErrorKind::Interrupted { | ||
println!("{}", e); | ||
std::process::exit(0); | ||
} else { | ||
panic!("Error: {}", e); | ||
} | ||
} | ||
} | ||
} | ||
|
||
fn main() { | ||
let theme = match args().nth(1).unwrap_or_default().as_str() { | ||
"base16" => Theme::base16(), | ||
|
@@ -16,7 +30,7 @@ fn main() { | |
.description("Please enter your e-mail address.") | ||
.placeholder("[email protected]") | ||
.theme(&theme); | ||
i.run().expect("error running input"); | ||
handle_run(i.run()); | ||
|
||
let ms = MultiSelect::new("Interests") | ||
.description("Select your interests") | ||
|
@@ -31,12 +45,12 @@ fn main() { | |
.option(DemandOption::new("Travel")) | ||
.option(DemandOption::new("Sports")) | ||
.theme(&theme); | ||
ms.run().expect("error running multi select"); | ||
handle_run(ms.run()); | ||
|
||
let c = Confirm::new("Confirm privacy policy") | ||
.description("Do you accept the privacy policy?") | ||
.affirmative("Yes") | ||
.negative("No") | ||
.theme(&theme); | ||
c.run().expect("error running confirm"); | ||
handle_run(c.run()); | ||
} |
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,86 @@ | ||
use console::Term; | ||
use once_cell::sync::Lazy; | ||
use signal_hook::{ | ||
consts::SIGINT, | ||
iterator::{Handle, Signals}, | ||
}; | ||
use std::{ | ||
io::Error, | ||
sync::{ | ||
atomic::{AtomicBool, Ordering}, | ||
Mutex, RwLock, | ||
}, | ||
thread, | ||
time::Duration, | ||
}; | ||
|
||
static MUTEX: Mutex<()> = Mutex::new(()); | ||
static INIT: AtomicBool = AtomicBool::new(false); | ||
static HANDLE: Lazy<RwLock<Option<CtrlcHandle>>> = Lazy::new(|| RwLock::new(None)); | ||
|
||
#[derive(Clone)] | ||
pub struct CtrlcHandle(Handle); | ||
|
||
impl CtrlcHandle { | ||
#[allow(dead_code)] | ||
pub fn close(&self) { | ||
self.0.close(); | ||
} | ||
} | ||
|
||
impl Drop for CtrlcHandle { | ||
fn drop(&mut self) { | ||
// let the signal handler finish | ||
thread::sleep(Duration::from_millis(100)); | ||
self.0.close(); | ||
} | ||
} | ||
|
||
pub fn show_cursor_after_ctrlc(term: &Term) -> Result<CtrlcHandle, Error> { | ||
let t = term.clone(); | ||
match set_ctrlc_handler(move || { | ||
println!("Ctrl-C pressed"); | ||
let _ = t.show_cursor(); | ||
}) { | ||
Ok(Some(handle)) => Ok(handle), | ||
_ => Err(Error::new( | ||
std::io::ErrorKind::Other, | ||
"failed to set Ctrl-C handler", | ||
)), | ||
} | ||
} | ||
|
||
pub fn set_ctrlc_handler<F>(handler: F) -> Result<Option<CtrlcHandle>, Error> | ||
where | ||
F: FnMut() + 'static + Send, | ||
{ | ||
let _mutex = MUTEX.lock(); | ||
if INIT.load(Ordering::Relaxed) { | ||
let handle_guard = HANDLE.read().unwrap(); | ||
return Ok(handle_guard.clone()); | ||
} | ||
INIT.store(true, Ordering::Relaxed); | ||
|
||
let handle = set_ctrlc_handler_internal(handler)?; | ||
{ | ||
let mut handle_guard = HANDLE.write().unwrap(); | ||
*handle_guard = Some(CtrlcHandle(handle.clone())); | ||
} | ||
Ok(Some(CtrlcHandle(handle))) | ||
} | ||
|
||
fn set_ctrlc_handler_internal<F>(mut handler: F) -> Result<Handle, Error> | ||
where | ||
F: FnMut() + 'static + Send, | ||
{ | ||
let mut signals = Signals::new([SIGINT])?; | ||
let handle = signals.handle(); | ||
thread::Builder::new() | ||
.name("ctrl-c".into()) | ||
.spawn(move || { | ||
for _ in signals.forever() { | ||
handler(); | ||
} | ||
})?; | ||
Ok(handle) | ||
} |
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
Oops, something went wrong.