-
Notifications
You must be signed in to change notification settings - Fork 6
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
166 additions
and
30 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,73 @@ | ||
use console::Term; | ||
use lazy_static::lazy_static; | ||
use signal_hook::{ | ||
consts::SIGINT, | ||
iterator::{Handle, Signals}, | ||
}; | ||
use std::{ | ||
io::Error, | ||
sync::{ | ||
atomic::{AtomicBool, Ordering}, | ||
Mutex, RwLock, | ||
}, | ||
thread, | ||
}; | ||
|
||
static INIT: AtomicBool = AtomicBool::new(false); | ||
static INIT_LOCK: Mutex<()> = Mutex::new(()); | ||
|
||
lazy_static! { | ||
static ref HANDLE: RwLock<Option<Handle>> = RwLock::new(None); | ||
} | ||
|
||
pub fn show_cursor_after_ctrlc(term: &Term) -> Result<Option<Handle>, Error> { | ||
let t = term.clone(); | ||
set_ctrlc_handler(move || { | ||
println!("Received Ctrl-C"); | ||
t.show_cursor().unwrap(); | ||
}) | ||
} | ||
|
||
pub fn close_ctrlc_handle(handle: Option<Handle>) { | ||
if let Some(handle) = handle { | ||
handle.close(); | ||
} | ||
} | ||
|
||
pub fn set_ctrlc_handler<F>(mut handler: F) -> Result<Option<Handle>, Error> | ||
where | ||
F: FnMut() + 'static + Send, | ||
{ | ||
let _guard = INIT_LOCK.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(move || handler())?; | ||
{ | ||
let mut handle_guard = HANDLE.write().unwrap(); | ||
*handle_guard = Some(handle.clone()); | ||
} | ||
Ok(Some(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 sig in signals.forever() { | ||
println!("Received signal {:?}", sig); | ||
handler(); | ||
// we dont' break here and leave it | ||
// to the user to close the handle | ||
} | ||
})?; | ||
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
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.