Skip to content

Commit d15e985

Browse files
committed
feat: add show_hint option to Confirm prompt
1 parent 19af624 commit d15e985

5 files changed

Lines changed: 66 additions & 30 deletions

File tree

examples/no_hint.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
use dialoguer::Confirm;
2+
3+
fn main() {
4+
Confirm::new()
5+
.with_prompt("Press enter to continue")
6+
.show_hint(false)
7+
.report(false)
8+
.default(true)
9+
.interact()
10+
.unwrap();
11+
}

src/prompts/confirm.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ pub struct Confirm<'a> {
3535
show_default: bool,
3636
wait_for_newline: bool,
3737
theme: &'a dyn Theme,
38+
show_hint: bool,
3839
}
3940

4041
impl Default for Confirm<'static> {
@@ -97,6 +98,14 @@ impl Confirm<'_> {
9798
self
9899
}
99100

101+
/// Disables or enables the hint display (e.g. `[y/n]`).
102+
///
103+
/// The default is to show the hint.
104+
pub fn show_hint(mut self, val: bool) -> Self {
105+
self.show_hint = val;
106+
self
107+
}
108+
100109
/// Enables user interaction and returns the result.
101110
///
102111
/// The dialog is rendered on stderr.
@@ -163,7 +172,7 @@ impl Confirm<'_> {
163172
None
164173
};
165174

166-
render.confirm_prompt(&self.prompt, default_if_show)?;
175+
render.confirm_prompt(&self.prompt, default_if_show, self.show_hint)?;
167176

168177
term.hide_cursor()?;
169178
term.flush()?;
@@ -205,7 +214,7 @@ impl Confirm<'_> {
205214
};
206215

207216
term.clear_line()?;
208-
render.confirm_prompt(&self.prompt, value)?;
217+
render.confirm_prompt(&self.prompt, value, self.show_hint)?;
209218
}
210219
} else {
211220
// Default behavior: matches continuously on every keystroke,
@@ -260,6 +269,7 @@ impl<'a> Confirm<'a> {
260269
show_default: true,
261270
wait_for_newline: false,
262271
theme,
272+
show_hint: true,
263273
}
264274
}
265275
}

src/theme/colorful.rs

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ impl Theme for ColorfulTheme {
139139
f: &mut dyn fmt::Write,
140140
prompt: &str,
141141
default: Option<bool>,
142+
show_hint: bool,
142143
) -> fmt::Result {
143144
if !prompt.is_empty() {
144145
write!(
@@ -149,30 +150,33 @@ impl Theme for ColorfulTheme {
149150
)?;
150151
}
151152

152-
match default {
153-
None => write!(
154-
f,
155-
"{} {}",
156-
self.hint_style.apply_to("(y/n)"),
157-
&self.prompt_suffix
158-
),
159-
Some(true) => write!(
160-
f,
161-
"{} {} {}",
162-
self.hint_style.apply_to("(y/n)"),
163-
&self.prompt_suffix,
164-
self.defaults_style.apply_to("yes")
165-
),
166-
Some(false) => write!(
167-
f,
168-
"{} {} {}",
169-
self.hint_style.apply_to("(y/n)"),
170-
&self.prompt_suffix,
171-
self.defaults_style.apply_to("no")
172-
),
153+
if show_hint {
154+
match default {
155+
None => write!(
156+
f,
157+
"{} {}",
158+
self.hint_style.apply_to("(y/n)"),
159+
&self.prompt_suffix
160+
),
161+
Some(true) => write!(
162+
f,
163+
"{} {} {}",
164+
self.hint_style.apply_to("(y/n)"),
165+
&self.prompt_suffix,
166+
self.defaults_style.apply_to("yes")
167+
),
168+
Some(false) => write!(
169+
f,
170+
"{} {} {}",
171+
self.hint_style.apply_to("(y/n)"),
172+
&self.prompt_suffix,
173+
self.defaults_style.apply_to("no")
174+
),
175+
}
176+
} else {
177+
Ok(())
173178
}
174179
}
175-
176180
/// Formats a confirm prompt after selection.
177181
fn format_confirm_prompt_selection(
178182
&self,

src/theme/mod.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,17 @@ pub trait Theme {
3333
f: &mut dyn fmt::Write,
3434
prompt: &str,
3535
default: Option<bool>,
36+
show_hint: bool,
3637
) -> fmt::Result {
3738
if !prompt.is_empty() {
3839
write!(f, "{} ", &prompt)?;
3940
}
40-
match default {
41-
None => write!(f, "[y/n] ")?,
42-
Some(true) => write!(f, "[Y/n] ")?,
43-
Some(false) => write!(f, "[y/N] ")?,
41+
if show_hint {
42+
match default {
43+
None => write!(f, "[y/n] ")?,
44+
Some(true) => write!(f, "[Y/n] ")?,
45+
Some(false) => write!(f, "[y/N] ")?,
46+
}
4447
}
4548
Ok(())
4649
}

src/theme/render.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,16 @@ impl<'a> TermThemeRenderer<'a> {
8787
self.write_formatted_line(|this, buf| this.theme.format_error(buf, err))
8888
}
8989

90-
pub fn confirm_prompt(&mut self, prompt: &str, default: Option<bool>) -> Result<usize> {
91-
self.write_formatted_str(|this, buf| this.theme.format_confirm_prompt(buf, prompt, default))
90+
pub fn confirm_prompt(
91+
&mut self,
92+
prompt: &str,
93+
default: Option<bool>,
94+
show_hint: bool,
95+
) -> Result<usize> {
96+
self.write_formatted_str(|this, buf| {
97+
this.theme
98+
.format_confirm_prompt(buf, prompt, default, show_hint)
99+
})
92100
}
93101

94102
pub fn confirm_prompt_selection(&mut self, prompt: &str, sel: Option<bool>) -> Result {

0 commit comments

Comments
 (0)