diff --git a/Cargo.toml b/Cargo.toml index 1db5e2b..65cc1bd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,6 +10,7 @@ keywords = ["cli", "prompt", "console"] [dependencies] console = "0.15" +ctrlc = "3.4.5" fuzzy-matcher = "0.3" itertools = "0.13" once_cell = "1" diff --git a/examples/confirm.rs b/examples/confirm.rs index c910e12..cce4a4f 100644 --- a/examples/confirm.rs +++ b/examples/confirm.rs @@ -5,11 +5,11 @@ fn main() { .description("This will do a thing.") .affirmative("Yes!") .negative("No."); - let _ = match confirm.run() { + match confirm.run() { Ok(confirm) => confirm, Err(e) => { if e.kind() == std::io::ErrorKind::Interrupted { - println!("Dialog was cancelled"); + println!("{}", e); false } else { panic!("Error: {}", e); diff --git a/examples/dialog.rs b/examples/dialog.rs index 13d3453..c8085c6 100644 --- a/examples/dialog.rs +++ b/examples/dialog.rs @@ -9,11 +9,11 @@ fn main() { DialogButton::new("Cancel"), ]) .selected_button(1); - let _ = match dialog.run() { + match dialog.run() { Ok(value) => value, Err(e) => { if e.kind() == std::io::ErrorKind::Interrupted { - println!("Dialog was cancelled"); + println!("{}", e); return; } else { panic!("Error: {}", e); diff --git a/examples/input-password.rs b/examples/input-password.rs index 6bb8897..b5657a1 100644 --- a/examples/input-password.rs +++ b/examples/input-password.rs @@ -5,11 +5,11 @@ fn main() { .placeholder("Enter password") .prompt("Password: ") .password(true); - let _ = match input.run() { + match input.run() { Ok(value) => value, Err(e) => { if e.kind() == std::io::ErrorKind::Interrupted { - println!("Input cancelled"); + println!("{}", e); return; } else { panic!("Error: {}", e); diff --git a/examples/input.rs b/examples/input.rs index de00192..0542fa4 100644 --- a/examples/input.rs +++ b/examples/input.rs @@ -26,11 +26,11 @@ fn main() { "Zack Snyder", ]) .validation(notempty_minlen); - let _ = match input.run() { + match input.run() { Ok(value) => value, Err(e) => { if e.kind() == std::io::ErrorKind::Interrupted { - println!("Input cancelled"); + println!("{}", e); return; } else { panic!("Error: {}", e); diff --git a/examples/list.rs b/examples/list.rs index 0fd2bf4..7a33a09 100644 --- a/examples/list.rs +++ b/examples/list.rs @@ -71,7 +71,7 @@ fn main() { Ok(_) => {} Err(e) => { if e.kind() == io::ErrorKind::Interrupted { - println!("Input cancelled"); + println!("{}", e); } else { panic!("Error: {}", e); } diff --git a/examples/multiselect.rs b/examples/multiselect.rs index 23d43eb..76a5790 100644 --- a/examples/multiselect.rs +++ b/examples/multiselect.rs @@ -13,11 +13,11 @@ fn main() { .option(DemandOption::new("Cheese")) .option(DemandOption::new("Vegan Cheese")) .option(DemandOption::new("Nutella")); - let _ = match multiselect.run() { + match multiselect.run() { Ok(toppings) => toppings, Err(e) => { if e.kind() == std::io::ErrorKind::Interrupted { - println!("Input cancelled"); + println!("{}", e); return; } else { panic!("Error: {}", e); diff --git a/examples/multiselect_huge.rs b/examples/multiselect_huge.rs index 197c09a..08941d7 100644 --- a/examples/multiselect_huge.rs +++ b/examples/multiselect_huge.rs @@ -67,11 +67,11 @@ fn main() { .option(DemandOption::new("Starburst")) .option(DemandOption::new("Twizzlers")) .option(DemandOption::new("Milk Duds")); - let _ = match multiselect.run() { + match multiselect.run() { Ok(value) => value, Err(e) => { if e.kind() == std::io::ErrorKind::Interrupted { - println!("Input cancelled"); + println!("{}", e); return; } else { panic!("Error: {}", e); diff --git a/examples/select.rs b/examples/select.rs index 32c9269..779ef0e 100644 --- a/examples/select.rs +++ b/examples/select.rs @@ -47,11 +47,11 @@ fn main() { .option(DemandOption::new("EG").label("Egypt")) .option(DemandOption::new("SA").label("Saudi Arabia")) .option(DemandOption::new("AE").label("United Arab Emirates")); - let _ = match ms.run() { + match ms.run() { Ok(value) => value, Err(e) => { if e.kind() == std::io::ErrorKind::Interrupted { - println!("Input cancelled"); + println!("{}", e); return; } else { panic!("Error: {}", e); diff --git a/examples/themes.rs b/examples/themes.rs index 1ab93a5..113a606 100644 --- a/examples/themes.rs +++ b/examples/themes.rs @@ -2,6 +2,20 @@ use std::env::args; use demand::{Confirm, DemandOption, Input, MultiSelect, Theme}; +fn handle_run(result: Result) -> 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("john.doe@acme.com") .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()); } diff --git a/src/confirm.rs b/src/confirm.rs index 049b9bd..a2bbf3a 100644 --- a/src/confirm.rs +++ b/src/confirm.rs @@ -4,8 +4,8 @@ use std::io::Write; use console::{Key, Term}; use termcolor::{Buffer, WriteColor}; -use crate::theme; use crate::theme::Theme; +use crate::{show_cursor_after_ctrlc, theme}; /// Select multiple options from a list /// @@ -95,9 +95,11 @@ impl<'a> Confirm<'a> { /// This function will block until the user submits the input. If the user cancels the input, /// an error of type `io::ErrorKind::Interrupted` is returned. pub fn run(mut self) -> io::Result { + show_cursor_after_ctrlc(&self.term); let affirmative_char = self.affirmative.to_lowercase().chars().next().unwrap(); let negative_char = self.negative.to_lowercase().chars().next().unwrap(); self.term.clear_line()?; + self.term.hide_cursor()?; loop { self.clear()?; let output = self.render()?; @@ -119,7 +121,7 @@ impl<'a> Confirm<'a> { return self.handle_submit(); } Key::Escape => { - self.clear()?; + self.term.show_cursor()?; return Err(io::Error::new(io::ErrorKind::Interrupted, "user cancelled")); } _ => {} @@ -130,6 +132,7 @@ impl<'a> Confirm<'a> { fn handle_submit(mut self) -> io::Result { self.term.clear_to_end_of_screen()?; self.clear()?; + self.term.show_cursor()?; let output = self.render_success()?; self.term.write_all(output.as_bytes())?; Ok(self.selected) diff --git a/src/dialog.rs b/src/dialog.rs index 09201e1..0ae8a16 100644 --- a/src/dialog.rs +++ b/src/dialog.rs @@ -4,8 +4,8 @@ use std::io::Write; use console::{Key, Term}; use termcolor::{Buffer, WriteColor}; -use crate::theme; use crate::theme::Theme; +use crate::{show_cursor_after_ctrlc, theme}; #[derive(Clone, Debug, Default, PartialEq)] /// A button to select in a dialog @@ -131,6 +131,8 @@ impl<'a> Dialog<'a> { /// This function will block until the user submits the input. If the user cancels the input, /// an error of type `io::ErrorKind::Interrupted` is returned. pub fn run(mut self) -> io::Result { + show_cursor_after_ctrlc(&self.term); + self.term.hide_cursor()?; loop { self.clear()?; let output = self.render()?; @@ -149,7 +151,7 @@ impl<'a> Dialog<'a> { return self.handle_submit(); } Key::Escape => { - self.clear()?; + self.term.show_cursor()?; return Err(io::Error::new(io::ErrorKind::Interrupted, "user cancelled")); } _ => {} @@ -159,6 +161,7 @@ impl<'a> Dialog<'a> { fn handle_submit(mut self) -> io::Result { self.clear()?; + self.term.show_cursor()?; let output = self.render_success()?; self.term.write_all(output.as_bytes())?; let result = if !self.buttons.is_empty() { diff --git a/src/input.rs b/src/input.rs index 86a3d32..192d05d 100644 --- a/src/input.rs +++ b/src/input.rs @@ -6,7 +6,7 @@ use std::{ use console::{measure_text_width, Key, Term}; use termcolor::{Buffer, WriteColor}; -use crate::{theme, Theme}; +use crate::{show_cursor_after_ctrlc, theme, Theme}; /// Single line text input /// @@ -153,6 +153,7 @@ impl<'a> Input<'a> { /// This function will block until the user submits the input. If the user cancels the input, /// an error of type `io::ErrorKind::Interrupted` is returned. pub fn run(mut self) -> io::Result { + show_cursor_after_ctrlc(&self.term); self.term.hide_cursor()?; loop { self.clear()?; @@ -184,7 +185,7 @@ impl<'a> Input<'a> { } Key::Tab => self.handle_tab()?, Key::Escape => { - self.clear()?; + self.term.show_cursor()?; return Err(io::Error::new(io::ErrorKind::Interrupted, "user cancelled")); } _ => {} diff --git a/src/lib.rs b/src/lib.rs index 6b4933e..ce327ed 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,7 @@ //! A prompt library for Rust. Based on [huh? for Go](https://github.com/charmbracelet/huh). pub use confirm::Confirm; +use console::Term; pub use dialog::Dialog; pub use dialog::DialogButton; pub use input::Input; @@ -24,3 +25,12 @@ mod theme; #[cfg(test)] mod test; + +/// Resets the cursor when a user presses `Ctrl+C`. +/// This is useful when the cursor is hidden and the program is interrupted. +fn show_cursor_after_ctrlc(term: &Term) { + let t = term.clone(); + let _ = ctrlc::set_handler(move || { + let _ = t.show_cursor(); + }); +} diff --git a/src/list.rs b/src/list.rs index 0c7995e..2151417 100644 --- a/src/list.rs +++ b/src/list.rs @@ -4,7 +4,7 @@ use console::{Key, Term}; use std::io::Write; use termcolor::{Buffer, WriteColor}; -use crate::{theme, Theme}; +use crate::{show_cursor_after_ctrlc, theme, Theme}; /// Display a list of options /// @@ -125,6 +125,7 @@ impl<'a> List<'a> { /// This function will block until the user submits the input. If the user cancels the input, /// an error of type `io::ErrorKind::Interrupted` is returned. pub fn run(mut self) -> Result<(), io::Error> { + show_cursor_after_ctrlc(&self.term); loop { self.clear()?; let output = self.render()?; @@ -148,7 +149,7 @@ impl<'a> List<'a> { Key::ArrowRight | Key::Char('l') => self.handle_right()?, Key::Char('/') if self.filterable => self.handle_start_filtering(), Key::Escape => { - self.clear()?; + self.term.show_cursor()?; return Err(io::Error::new(io::ErrorKind::Interrupted, "user cancelled")); } Key::Enter => { diff --git a/src/multiselect.rs b/src/multiselect.rs index 1314938..bf5927a 100644 --- a/src/multiselect.rs +++ b/src/multiselect.rs @@ -6,7 +6,7 @@ use console::{Key, Term}; use termcolor::{Buffer, WriteColor}; use crate::theme::Theme; -use crate::{theme, DemandOption}; +use crate::{show_cursor_after_ctrlc, theme, DemandOption}; /// Select multiple options from a list /// @@ -141,6 +141,7 @@ impl<'a, T> MultiSelect<'a, T> { /// This function will block until the user submits the input. If the user cancels the input, /// an error of type `io::ErrorKind::Interrupted` is returned. pub fn run(mut self) -> io::Result> { + show_cursor_after_ctrlc(&self.term); self.max = self.max.min(self.options.len()); self.min = self.min.min(self.max); @@ -170,7 +171,7 @@ impl<'a, T> MultiSelect<'a, T> { Key::Char('/') if self.filterable => self.handle_start_filtering(), Key::Escape => { if self.filter.is_empty() { - self.clear()?; + self.term.show_cursor()?; return Err(io::Error::new( io::ErrorKind::Interrupted, "user cancelled", diff --git a/src/select.rs b/src/select.rs index 7e7ee83..842cd30 100644 --- a/src/select.rs +++ b/src/select.rs @@ -2,7 +2,7 @@ use std::io; use std::io::Write; use crate::theme::Theme; -use crate::{theme, DemandOption}; +use crate::{show_cursor_after_ctrlc, theme, DemandOption}; use console::{Alignment, Key, Term}; use fuzzy_matcher::skim::SkimMatcherV2; use fuzzy_matcher::FuzzyMatcher; @@ -131,6 +131,8 @@ impl<'a, T> Select<'a, T> { /// This function will block until the user submits the input. If the user cancels the input, /// an error of type `io::ErrorKind::Interrupted` is returned. pub fn run(mut self) -> io::Result { + show_cursor_after_ctrlc(&self.term); + loop { self.clear()?; let output = self.render()?; @@ -171,7 +173,7 @@ impl<'a, T> Select<'a, T> { Key::Char('/') if self.filterable => self.handle_start_filtering(), Key::Escape => { if self.filter.is_empty() { - self.clear()?; + self.term.show_cursor()?; return Err(io::Error::new( io::ErrorKind::Interrupted, "user cancelled", diff --git a/src/spinner.rs b/src/spinner.rs index db2782e..a259e92 100644 --- a/src/spinner.rs +++ b/src/spinner.rs @@ -129,6 +129,12 @@ impl<'a> Spinner<'a> { F: FnOnce(&mut SpinnerActionRunner<'spinner>) -> T + Send + 'scope, T: Send + 'scope, { + let t = self.term.clone(); + let _ = ctrlc::set_handler(move || { + t.show_cursor().unwrap(); + std::process::exit(130); + }); + std::thread::scope(|s| { let (sender, receiver) = mpsc::channel(); let handle = s.spawn(move || {