Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add '1' and '0' as allowed keys for Confirm #279

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -8,6 +8,7 @@
* Added a `BasicHistory` implementation for `History`
* Added vim mode for `FuzzySelect`
* All prompts implement `Clone`
* `Confirm` now also accepts '1' and '0' as confirmations for international accessibility

### Bug fixes

Expand Down
12 changes: 6 additions & 6 deletions src/prompts/confirm.rs
Expand Up @@ -68,11 +68,11 @@ impl Confirm<'_> {
/// Sets when to react to user input.
///
/// When `false` (default), we check on each user keystroke immediately as
/// it is typed. Valid inputs can be one of 'y', 'n', or a newline to accept
/// it is typed. Valid inputs can be one of 'y', 'n', '1', '0' or a newline to accept
/// the default.
///
/// When `true`, the user must type their choice and hit the Enter key before
/// proceeding. Valid inputs can be "yes", "no", "y", "n", or an empty string
/// proceeding. Valid inputs can be "yes", "no", "y", "n", "1", "0" or an empty string
/// to accept the default.
pub fn wait_for_newline(mut self, wait: bool) -> Self {
self.wait_for_newline = wait;
Expand Down Expand Up @@ -179,10 +179,10 @@ impl Confirm<'_> {
let input = term.read_key()?;

match input {
Key::Char('y') | Key::Char('Y') => {
Key::Char('y') | Key::Char('Y') | Key::Char('1') => {
value = Some(true);
}
Key::Char('n') | Key::Char('N') => {
Key::Char('n') | Key::Char('N') | Key::Char('0') => {
value = Some(false);
}
Key::Enter => {
Expand Down Expand Up @@ -213,8 +213,8 @@ impl Confirm<'_> {
loop {
let input = term.read_key()?;
let value = match input {
Key::Char('y') | Key::Char('Y') => Some(true),
Key::Char('n') | Key::Char('N') => Some(false),
Key::Char('y') | Key::Char('Y') | Key::Char('1') => Some(true),
Key::Char('n') | Key::Char('N') | Key::Char('0') => Some(false),
Key::Enter if self.default.is_some() => Some(self.default.unwrap()),
Key::Escape | Key::Char('q') if allow_quit => None,
_ => {
Expand Down
6 changes: 3 additions & 3 deletions src/theme/colorful.rs
Expand Up @@ -153,20 +153,20 @@ impl Theme for ColorfulTheme {
None => write!(
f,
"{} {}",
self.hint_style.apply_to("(y/n)"),
self.hint_style.apply_to("(y/n or 1/0)"),
&self.prompt_suffix
),
Some(true) => write!(
f,
"{} {} {}",
self.hint_style.apply_to("(y/n)"),
self.hint_style.apply_to("(y/n or 1/0)"),
&self.prompt_suffix,
self.defaults_style.apply_to("yes")
),
Some(false) => write!(
f,
"{} {} {}",
self.hint_style.apply_to("(y/n)"),
self.hint_style.apply_to("(y/n or 1/0)"),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we not have the hints in prompts and leave them as y/n?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you don't want to change the hint, I can make a logic to change it to 1/0 when the non-ascii character is read.
Otherwise, users will probably find it hard to discover alternative keys.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue with showing things after a key is pressed is that in some scenarios, the prompt is considered answered as soon as a key is pressed. So, I am wondering if it's worth showing it at all.

Maybe for those scenarios where it waits, we can change it to show 1/0 only if one of them is pressed?

&self.prompt_suffix,
self.defaults_style.apply_to("no")
),
Expand Down
6 changes: 3 additions & 3 deletions src/theme/mod.rs
Expand Up @@ -38,9 +38,9 @@ pub trait Theme {
write!(f, "{} ", &prompt)?;
}
match default {
None => write!(f, "[y/n] ")?,
Some(true) => write!(f, "[Y/n] ")?,
Some(false) => write!(f, "[y/N] ")?,
None => write!(f, "[y/n or 1/0] ")?,
Some(true) => write!(f, "[Y/n or 1/0] ")?,
Some(false) => write!(f, "[y/N or 1/0] ")?,
}
Ok(())
}
Expand Down