Skip to content

Commit f9c067b

Browse files
authored
Merge pull request #146 from miraclx/say-no-more
feat: support optionally disabling the report after interaction
2 parents db7d7cc + 05cdddb commit f9c067b

File tree

7 files changed

+128
-34
lines changed

7 files changed

+128
-34
lines changed

src/prompts/confirm.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use console::{Key, Term};
2121
/// ```
2222
pub struct Confirm<'a> {
2323
prompt: String,
24+
report: bool,
2425
default: Option<bool>,
2526
show_default: bool,
2627
wait_for_newline: bool,
@@ -47,6 +48,14 @@ impl Confirm<'_> {
4748
self
4849
}
4950

51+
/// Indicates whether or not to report the chosen selection after interaction.
52+
///
53+
/// The default is to report the chosen selection.
54+
pub fn report(&mut self, val: bool) -> &mut Self {
55+
self.report = val;
56+
self
57+
}
58+
5059
#[deprecated(note = "Use with_prompt() instead", since = "0.6.0")]
5160
#[inline]
5261
pub fn with_text(&mut self, text: &str) -> &mut Self {
@@ -226,7 +235,9 @@ impl Confirm<'_> {
226235
}
227236

228237
term.clear_line()?;
229-
render.confirm_prompt_selection(&self.prompt, rv)?;
238+
if self.report {
239+
render.confirm_prompt_selection(&self.prompt, rv)?;
240+
}
230241
term.show_cursor()?;
231242
term.flush()?;
232243

@@ -254,6 +265,7 @@ impl<'a> Confirm<'a> {
254265
pub fn with_theme(theme: &'a dyn Theme) -> Self {
255266
Self {
256267
prompt: "".into(),
268+
report: true,
257269
default: None,
258270
show_default: true,
259271
wait_for_newline: false,

src/prompts/fuzzy_select.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ pub struct FuzzySelect<'a> {
3737
default: usize,
3838
items: Vec<String>,
3939
prompt: String,
40+
report: bool,
4041
clear: bool,
4142
theme: &'a dyn Theme,
4243
}
@@ -92,6 +93,14 @@ impl FuzzySelect<'_> {
9293
self
9394
}
9495

96+
/// Indicates whether to report the selected value after interaction.
97+
///
98+
/// The default is to report the selection.
99+
pub fn report(&mut self, val: bool) -> &mut Self {
100+
self.report = val;
101+
self
102+
}
103+
95104
/// Enables user interaction and returns the result.
96105
///
97106
/// The user can select the items using 'Enter' and the index of selected item will be returned.
@@ -204,7 +213,10 @@ impl FuzzySelect<'_> {
204213
render.clear()?;
205214
}
206215

207-
render.input_prompt_selection(self.prompt.as_str(), &filtered_list[sel].0)?;
216+
if self.report {
217+
render
218+
.input_prompt_selection(self.prompt.as_str(), &filtered_list[sel].0)?;
219+
}
208220

209221
let sel_string = filtered_list[sel].0;
210222
let sel_string_pos_in_items =
@@ -240,6 +252,7 @@ impl<'a> FuzzySelect<'a> {
240252
default: !0,
241253
items: vec![],
242254
prompt: "".into(),
255+
report: true,
243256
clear: true,
244257
theme,
245258
}

src/prompts/input.rs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ use console::{Key, Term};
3939
/// ```
4040
pub struct Input<'a, T> {
4141
prompt: String,
42+
report: bool,
4243
default: Option<T>,
4344
show_default: bool,
4445
initial_text: Option<String>,
@@ -69,6 +70,14 @@ impl<T> Input<'_, T> {
6970
self
7071
}
7172

73+
/// Indicates whether to report the input value after interaction.
74+
///
75+
/// The default is to report the input value.
76+
pub fn report(&mut self, val: bool) -> &mut Self {
77+
self.report = val;
78+
self
79+
}
80+
7281
/// Sets initial text that user can accept or erase.
7382
pub fn with_initial_text<S: Into<String>>(&mut self, val: S) -> &mut Self {
7483
self.initial_text = Some(val.into());
@@ -110,6 +119,7 @@ impl<'a, T> Input<'a, T> {
110119
pub fn with_theme(theme: &'a dyn Theme) -> Self {
111120
Self {
112121
prompt: "".into(),
122+
report: true,
113123
default: None,
114124
show_default: true,
115125
initial_text: None,
@@ -403,7 +413,9 @@ where
403413
}
404414
}
405415

406-
render.input_prompt_selection(&self.prompt, &default.to_string())?;
416+
if self.report {
417+
render.input_prompt_selection(&self.prompt, &default.to_string())?;
418+
}
407419
term.flush()?;
408420
return Ok(default.clone());
409421
} else if !self.permit_empty {
@@ -425,7 +437,9 @@ where
425437
history.write(&value);
426438
}
427439

428-
render.input_prompt_selection(&self.prompt, &input)?;
440+
if self.report {
441+
render.input_prompt_selection(&self.prompt, &input)?;
442+
}
429443
term.flush()?;
430444

431445
return Ok(value);
@@ -492,7 +506,9 @@ where
492506
}
493507
}
494508

495-
render.input_prompt_selection(&self.prompt, &default.to_string())?;
509+
if self.report {
510+
render.input_prompt_selection(&self.prompt, &default.to_string())?;
511+
}
496512
term.flush()?;
497513
return Ok(default.clone());
498514
} else if !self.permit_empty {
@@ -509,7 +525,9 @@ where
509525
}
510526
}
511527

512-
render.input_prompt_selection(&self.prompt, &input)?;
528+
if self.report {
529+
render.input_prompt_selection(&self.prompt, &input)?;
530+
}
513531
term.flush()?;
514532

515533
return Ok(value);

src/prompts/multi_select.rs

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ pub struct MultiSelect<'a> {
2525
defaults: Vec<bool>,
2626
items: Vec<String>,
2727
prompt: Option<String>,
28+
report: bool,
2829
clear: bool,
2930
max_length: Option<usize>,
3031
theme: &'a dyn Theme,
@@ -109,13 +110,21 @@ impl MultiSelect<'_> {
109110

110111
/// Prefaces the menu with a prompt.
111112
///
112-
/// When a prompt is set the system also prints out a confirmation after
113-
/// the selection.
113+
/// By default, when a prompt is set the system also prints out a confirmation after
114+
/// the selection. You can opt-out of this with [`report`](#method.report).
114115
pub fn with_prompt<S: Into<String>>(&mut self, prompt: S) -> &mut Self {
115116
self.prompt = Some(prompt.into());
116117
self
117118
}
118119

120+
/// Indicates whether to report the selected values after interaction.
121+
///
122+
/// The default is to report the selections.
123+
pub fn report(&mut self, val: bool) -> &mut Self {
124+
self.report = val;
125+
self
126+
}
127+
119128
/// Enables user interaction and returns the result.
120129
///
121130
/// The user can select the items with the 'Space' bar and on 'Enter' the indices of selected items will be returned.
@@ -282,19 +291,21 @@ impl MultiSelect<'_> {
282291
}
283292

284293
if let Some(ref prompt) = self.prompt {
285-
let selections: Vec<_> = checked
286-
.iter()
287-
.enumerate()
288-
.filter_map(|(idx, &checked)| {
289-
if checked {
290-
Some(self.items[idx].as_str())
291-
} else {
292-
None
293-
}
294-
})
295-
.collect();
296-
297-
render.multi_select_prompt_selection(prompt, &selections[..])?;
294+
if self.report {
295+
let selections: Vec<_> = checked
296+
.iter()
297+
.enumerate()
298+
.filter_map(|(idx, &checked)| {
299+
if checked {
300+
Some(self.items[idx].as_str())
301+
} else {
302+
None
303+
}
304+
})
305+
.collect();
306+
307+
render.multi_select_prompt_selection(prompt, &selections[..])?;
308+
}
298309
}
299310

300311
term.show_cursor()?;
@@ -330,6 +341,7 @@ impl<'a> MultiSelect<'a> {
330341
defaults: vec![],
331342
clear: true,
332343
prompt: None,
344+
report: true,
333345
max_length: None,
334346
theme,
335347
}

src/prompts/password.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use zeroize::Zeroizing;
2121
/// ```
2222
pub struct Password<'a> {
2323
prompt: String,
24+
report: bool,
2425
theme: &'a dyn Theme,
2526
allow_empty_password: bool,
2627
confirmation_prompt: Option<(String, String)>,
@@ -46,6 +47,14 @@ impl Password<'_> {
4647
self
4748
}
4849

50+
/// Indicates whether to report confirmation after interaction.
51+
///
52+
/// The default is to report.
53+
pub fn report(&mut self, val: bool) -> &mut Self {
54+
self.report = val;
55+
self
56+
}
57+
4958
/// Enables confirmation prompting.
5059
pub fn with_confirmation<A, B>(&mut self, prompt: A, mismatch_err: B) -> &mut Self
5160
where
@@ -85,15 +94,19 @@ impl Password<'_> {
8594

8695
if *password == *pw2 {
8796
render.clear()?;
88-
render.password_prompt_selection(&self.prompt)?;
97+
if self.report {
98+
render.password_prompt_selection(&self.prompt)?;
99+
}
89100
term.flush()?;
90101
return Ok((*password).clone());
91102
}
92103

93104
render.error(err)?;
94105
} else {
95106
render.clear()?;
96-
render.password_prompt_selection(&self.prompt)?;
107+
if self.report {
108+
render.password_prompt_selection(&self.prompt)?;
109+
}
97110
term.flush()?;
98111

99112
return Ok((*password).clone());
@@ -122,6 +135,7 @@ impl<'a> Password<'a> {
122135
pub fn with_theme(theme: &'a dyn Theme) -> Self {
123136
Self {
124137
prompt: "".into(),
138+
report: true,
125139
theme,
126140
allow_empty_password: false,
127141
confirmation_prompt: None,

src/prompts/select.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ pub struct Select<'a> {
3838
default: usize,
3939
items: Vec<String>,
4040
prompt: Option<String>,
41+
report: bool,
4142
clear: bool,
4243
theme: &'a dyn Theme,
4344
max_length: Option<usize>,
@@ -130,8 +131,8 @@ impl Select<'_> {
130131

131132
/// Sets the select prompt.
132133
///
133-
/// When a prompt is set the system also prints out a confirmation after
134-
/// the selection.
134+
/// By default, when a prompt is set the system also prints out a confirmation after
135+
/// the selection. You can opt-out of this with [`report`](#method.report).
135136
///
136137
/// ## Examples
137138
/// ```rust,no_run
@@ -149,6 +150,15 @@ impl Select<'_> {
149150
/// ```
150151
pub fn with_prompt<S: Into<String>>(&mut self, prompt: S) -> &mut Self {
151152
self.prompt = Some(prompt.into());
153+
self.report = true;
154+
self
155+
}
156+
157+
/// Indicates whether to report the selected value after interaction.
158+
///
159+
/// The default is to report the selection.
160+
pub fn report(&mut self, val: bool) -> &mut Self {
161+
self.report = val;
152162
self
153163
}
154164

@@ -314,7 +324,9 @@ impl Select<'_> {
314324
}
315325

316326
if let Some(ref prompt) = self.prompt {
317-
render.select_prompt_selection(prompt, &self.items[sel])?;
327+
if self.report {
328+
render.select_prompt_selection(prompt, &self.items[sel])?;
329+
}
318330
}
319331

320332
term.show_cursor()?;
@@ -360,6 +372,7 @@ impl<'a> Select<'a> {
360372
default: !0,
361373
items: vec![],
362374
prompt: None,
375+
report: false,
363376
clear: true,
364377
max_length: None,
365378
theme,

0 commit comments

Comments
 (0)