Comments (10)
Sort of tangential to this, if I CTRL-C
out of a selection, then my cursor is not restored.
Is that something I'd have to handle myself with some of the approaches named above?
from dialoguer.
Why was this closed?
from dialoguer.
This could be done through a custom (global?) callback function that users of the library could set before instantiating any dialog components.
from dialoguer.
Same problem. Would like the program not to exit when ctrl-c is hit during input.
from dialoguer.
So, after investigating a little, this is not really an issue with the library. It actually catches the ^C and returns an "Interrupted" error. However, before this can happen, the default SIGINT handler exits the program.
In order to work around this, one has to "disable" the signal handler.
I could not find a "good" way to do this. Below's my awkward solution. Notice the need to first setup the SIGINT handler. ctrlc
does not provide a way to change the handler after setting it, and signal_hook
does not allow setting the signal handler again if you reset to a default (i.e. if you set an empty handler to avoid exiting the program, then reset to the "default" one later, then you cannot set it again if, e.g., you need a new input prompt).
use anyhow::{Context, Result};
use dialoguer::Input;
use signal_hook::{register, unregister, SigId, SIGINT};
use std::io::ErrorKind;
pub fn prompt(p: &str) -> Result<String> {
with_sigint_disabled(|| do_prompt(p))
}
static mut SIGINT_HANDLER: Option<SigId> = None;
fn with_sigint_disabled<F, R>(f: F) -> Result<R>
where
F: Fn() -> Result<R>,
{
disable_sigint();
let r = f();
enable_sigint();
r
}
pub fn enable_sigint() {
unsafe {
if SIGINT_HANDLER.is_none() {
let signal = register(SIGINT, move || std::process::exit(128 + SIGINT)).unwrap();
SIGINT_HANDLER = Some(signal);
}
}
}
fn disable_sigint() {
unsafe {
if let Some(s) = SIGINT_HANDLER {
unregister(s);
SIGINT_HANDLER = None;
}
}
}
fn do_prompt(p: &str) -> Result<String> {
Input::new()
.with_prompt(p)
.allow_empty(true)
.validate_with(|x: &String| {
if x.trim().is_empty() {
Err(format!("{} cannot be empty", p))
} else {
Ok(())
}
})
.interact_text()
.map(|x| x.trim().to_string())
.map_err(|e| {
if e.kind() == ErrorKind::Interrupted {
println!("");
}
e
})
.with_context(|| format!("{} could not be read", p))
}
from dialoguer.
Can you try with https://lib.rs/crates/ctrlc?
from dialoguer.
I tried that one initially. The issue is that this one only lets you set the handler, but not disable it temporarily.
I just dawned on me that instead of disabling the handler, one could use a global flag, which is checked by the handler in order to decide what to do (e.g quit or nothing...).
Anyhow, not an issue with the library I guess. Just unexpected, given that in other languages this works out of the box.
std::string foo;
std::cin >> foo;
from dialoguer.
@felipesere I just had this issue and this is how I restored the terminal:
let term = dialogue::console::Term::stderr();
term.show_cursor()?;
from dialoguer.
Showing the cursor should hopefully be fixed by #149 issue.
from dialoguer.
Closing this issue since it is not about this library.
from dialoguer.
Related Issues (20)
- Feature request: alert message with pressing enter as only option
- Add some example pictures to the readme with links to the corresponding file in examples.
- Tab completion doesn't work on windows. HOT 1
- Backspace behavior at the end of line
- Handling Interrupt Signals (e.g., Ctrl+C) in `Select` and Other Dialoguer Actions HOT 4
- Duplicate line when wrapped HOT 1
- `Password`: Returns `Err` when used in conjunction with input via redirect or pipe HOT 2
- feat: support multi line input HOT 1
- [Issue] No visual indecator on selected items on Windows Terminal HOT 1
- Feature request: custom keyboard interactions
- Add support for asynchronous non-blocking I/O.
- Add header and footer options to (Fuzzy)Select (for making borders/tables)
- error[E0195]: lifetime parameters or bounds on type `Target` do not match the trait declaration HOT 2
- Feature: Third option for confirm prompt HOT 1
- `Input` post completion text does not appear if the user uses the default
- Request: add screenshots of what it can do to the README
- Save and restore a wizard flow HOT 1
- FuzzySelect messes up existing formatting
- Wide items in fuzzy select make the prompt disappear
- typos in CHANGELOG: dialouger -> dialoguer
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from dialoguer.