Skip to content

Commit 37ab570

Browse files
authored
Merge pull request console-rs#73 from edwardwli/master
2 parents 37b460a + 4261050 commit 37ab570

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

examples/confirm.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
extern crate dialoguer;
2+
3+
use dialoguer::Confirm;
4+
5+
fn main() {
6+
println!("disable default");
7+
if Confirm::new()
8+
.with_prompt("continue?")
9+
.disable_default(true)
10+
.interact()
11+
.unwrap()
12+
{
13+
println!("continuing");
14+
} else {
15+
println!("exiting");
16+
}
17+
18+
println!();
19+
20+
println!("enable default");
21+
if Confirm::new()
22+
.with_prompt("continue?")
23+
.interact()
24+
.unwrap()
25+
{
26+
println!("continuing");
27+
} else {
28+
println!("exiting");
29+
}
30+
}

src/prompts/confirm.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ pub struct Confirm<'a> {
2323
prompt: String,
2424
default: bool,
2525
show_default: bool,
26+
disable_default: bool,
2627
wait_for_newline: bool,
2728
theme: &'a dyn Theme,
2829
}
@@ -60,6 +61,7 @@ impl<'a> Confirm<'a> {
6061
prompt: "".into(),
6162
default: true,
6263
show_default: true,
64+
disable_default: false,
6365
wait_for_newline: false,
6466
theme,
6567
}
@@ -109,6 +111,16 @@ impl<'a> Confirm<'a> {
109111
self
110112
}
111113

114+
/// Select whether the user must explicitly confirm their selection.
115+
///
116+
/// When `false` (default), the user can input a newline to select the default.
117+
///
118+
/// When `true`, the user must input a letter - a newline will do nothing.
119+
pub fn disable_default(&mut self, val: bool) -> &mut Confirm<'a> {
120+
self.disable_default = val;
121+
self
122+
}
123+
112124
/// Enables user interaction and returns the result.
113125
///
114126
/// If the user confirms the result is `true`, `false` if declines or default (configured in [default](#method.default)) if pushes enter.
@@ -156,7 +168,7 @@ impl<'a> Confirm<'a> {
156168
let rv = match &*input_buf.trim_end().to_lowercase() {
157169
"y" | "yes" => true,
158170
"n" | "no" => false,
159-
"" => self.default,
171+
"" if !self.disable_default => self.default,
160172
_ => {
161173
// On invalid input re-render the user prompt.
162174
render.confirm_prompt(&self.prompt, default)?;
@@ -178,7 +190,7 @@ impl<'a> Confirm<'a> {
178190
let rv = match input {
179191
'y' | 'Y' => true,
180192
'n' | 'N' => false,
181-
'\n' | '\r' => self.default,
193+
'\n' | '\r' if !self.disable_default => self.default,
182194
_ => {
183195
continue;
184196
}

0 commit comments

Comments
 (0)