-
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
20 changed files
with
201 additions
and
31 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,102 @@ | ||
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, | ||
}; | ||
|
||
static MUTEX: Mutex<()> = Mutex::new(()); | ||
static INIT: AtomicBool = AtomicBool::new(false); | ||
static HANDLE: Lazy<RwLock<CtrlcHandle>> = Lazy::new(|| RwLock::new(CtrlcHandle(None))); | ||
|
||
#[derive(Clone)] | ||
pub struct CtrlcHandle(Option<Handle>); | ||
|
||
impl CtrlcHandle { | ||
pub fn close(&self) { | ||
if let Some(handle) = &self.0 { | ||
handle.close(); | ||
} | ||
} | ||
} | ||
|
||
/// Show cursor after Ctrl+C is pressed | ||
/// | ||
/// The caller should call the close method of the returned handle to release the resources | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `term` - The terminal to show the cursor | ||
/// | ||
/// # Returns | ||
/// | ||
/// * `CtrlcHandle` - The handle to release the resources | ||
/// | ||
/// # Errors | ||
/// | ||
/// * `Error` - If failed to set the Ctrl+C handler | ||
/// | ||
pub fn show_cursor_after_ctrlc(term: &Term) -> Result<CtrlcHandle, Error> { | ||
let t = term.clone(); | ||
set_ctrlc_handler(move || { | ||
let _ = t.show_cursor(); | ||
}) | ||
} | ||
|
||
/// Set Ctrl+C handler | ||
/// | ||
/// The caller should call the close method of the returned handle to release the resources | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `handler` - The handler to be called when Ctrl+C is pressed | ||
/// | ||
/// # Returns | ||
/// | ||
/// * `Result<Option<CtrlcHandle>, Error>` - The handle to release the resources | ||
/// | ||
/// # Errors | ||
/// * `Error` - If failed to set the Ctrl+C handler | ||
/// | ||
pub fn set_ctrlc_handler<F>(handler: F) -> Result<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 = CtrlcHandle(Some(handle.clone())); | ||
} | ||
Ok(CtrlcHandle(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 _ 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
use std::io::Error; | ||
|
||
use console::Term; | ||
|
||
pub struct CtrlcHandle(Option<()>); | ||
|
||
impl CtrlcHandle { | ||
pub fn close(&self) {} | ||
} | ||
|
||
pub fn show_cursor_after_ctrlc(_term: &Term) -> Result<CtrlcHandle, Error> { | ||
Ok(CtrlcHandle(None)) | ||
} |
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.