Skip to content

Commit

Permalink
Merge pull request #146 from miraclx/say-no-more
Browse files Browse the repository at this point in the history
feat: support optionally disabling the report after interaction
  • Loading branch information
pksunkara committed Nov 2, 2021
2 parents db7d7cc + 05cdddb commit f9c067b
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 34 deletions.
14 changes: 13 additions & 1 deletion src/prompts/confirm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use console::{Key, Term};
/// ```
pub struct Confirm<'a> {
prompt: String,
report: bool,
default: Option<bool>,
show_default: bool,
wait_for_newline: bool,
Expand All @@ -47,6 +48,14 @@ impl Confirm<'_> {
self
}

/// Indicates whether or not to report the chosen selection after interaction.
///
/// The default is to report the chosen selection.
pub fn report(&mut self, val: bool) -> &mut Self {
self.report = val;
self
}

#[deprecated(note = "Use with_prompt() instead", since = "0.6.0")]
#[inline]
pub fn with_text(&mut self, text: &str) -> &mut Self {
Expand Down Expand Up @@ -226,7 +235,9 @@ impl Confirm<'_> {
}

term.clear_line()?;
render.confirm_prompt_selection(&self.prompt, rv)?;
if self.report {
render.confirm_prompt_selection(&self.prompt, rv)?;
}
term.show_cursor()?;
term.flush()?;

Expand Down Expand Up @@ -254,6 +265,7 @@ impl<'a> Confirm<'a> {
pub fn with_theme(theme: &'a dyn Theme) -> Self {
Self {
prompt: "".into(),
report: true,
default: None,
show_default: true,
wait_for_newline: false,
Expand Down
15 changes: 14 additions & 1 deletion src/prompts/fuzzy_select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pub struct FuzzySelect<'a> {
default: usize,
items: Vec<String>,
prompt: String,
report: bool,
clear: bool,
theme: &'a dyn Theme,
}
Expand Down Expand Up @@ -92,6 +93,14 @@ impl FuzzySelect<'_> {
self
}

/// Indicates whether to report the selected value after interaction.
///
/// The default is to report the selection.
pub fn report(&mut self, val: bool) -> &mut Self {
self.report = val;
self
}

/// Enables user interaction and returns the result.
///
/// The user can select the items using 'Enter' and the index of selected item will be returned.
Expand Down Expand Up @@ -204,7 +213,10 @@ impl FuzzySelect<'_> {
render.clear()?;
}

render.input_prompt_selection(self.prompt.as_str(), &filtered_list[sel].0)?;
if self.report {
render
.input_prompt_selection(self.prompt.as_str(), &filtered_list[sel].0)?;
}

let sel_string = filtered_list[sel].0;
let sel_string_pos_in_items =
Expand Down Expand Up @@ -240,6 +252,7 @@ impl<'a> FuzzySelect<'a> {
default: !0,
items: vec![],
prompt: "".into(),
report: true,
clear: true,
theme,
}
Expand Down
26 changes: 22 additions & 4 deletions src/prompts/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ use console::{Key, Term};
/// ```
pub struct Input<'a, T> {
prompt: String,
report: bool,
default: Option<T>,
show_default: bool,
initial_text: Option<String>,
Expand Down Expand Up @@ -69,6 +70,14 @@ impl<T> Input<'_, T> {
self
}

/// Indicates whether to report the input value after interaction.
///
/// The default is to report the input value.
pub fn report(&mut self, val: bool) -> &mut Self {
self.report = val;
self
}

/// Sets initial text that user can accept or erase.
pub fn with_initial_text<S: Into<String>>(&mut self, val: S) -> &mut Self {
self.initial_text = Some(val.into());
Expand Down Expand Up @@ -110,6 +119,7 @@ impl<'a, T> Input<'a, T> {
pub fn with_theme(theme: &'a dyn Theme) -> Self {
Self {
prompt: "".into(),
report: true,
default: None,
show_default: true,
initial_text: None,
Expand Down Expand Up @@ -403,7 +413,9 @@ where
}
}

render.input_prompt_selection(&self.prompt, &default.to_string())?;
if self.report {
render.input_prompt_selection(&self.prompt, &default.to_string())?;
}
term.flush()?;
return Ok(default.clone());
} else if !self.permit_empty {
Expand All @@ -425,7 +437,9 @@ where
history.write(&value);
}

render.input_prompt_selection(&self.prompt, &input)?;
if self.report {
render.input_prompt_selection(&self.prompt, &input)?;
}
term.flush()?;

return Ok(value);
Expand Down Expand Up @@ -492,7 +506,9 @@ where
}
}

render.input_prompt_selection(&self.prompt, &default.to_string())?;
if self.report {
render.input_prompt_selection(&self.prompt, &default.to_string())?;
}
term.flush()?;
return Ok(default.clone());
} else if !self.permit_empty {
Expand All @@ -509,7 +525,9 @@ where
}
}

render.input_prompt_selection(&self.prompt, &input)?;
if self.report {
render.input_prompt_selection(&self.prompt, &input)?;
}
term.flush()?;

return Ok(value);
Expand Down
42 changes: 27 additions & 15 deletions src/prompts/multi_select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub struct MultiSelect<'a> {
defaults: Vec<bool>,
items: Vec<String>,
prompt: Option<String>,
report: bool,
clear: bool,
max_length: Option<usize>,
theme: &'a dyn Theme,
Expand Down Expand Up @@ -109,13 +110,21 @@ impl MultiSelect<'_> {

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

/// Indicates whether to report the selected values after interaction.
///
/// The default is to report the selections.
pub fn report(&mut self, val: bool) -> &mut Self {
self.report = val;
self
}

/// Enables user interaction and returns the result.
///
/// The user can select the items with the 'Space' bar and on 'Enter' the indices of selected items will be returned.
Expand Down Expand Up @@ -282,19 +291,21 @@ impl MultiSelect<'_> {
}

if let Some(ref prompt) = self.prompt {
let selections: Vec<_> = checked
.iter()
.enumerate()
.filter_map(|(idx, &checked)| {
if checked {
Some(self.items[idx].as_str())
} else {
None
}
})
.collect();

render.multi_select_prompt_selection(prompt, &selections[..])?;
if self.report {
let selections: Vec<_> = checked
.iter()
.enumerate()
.filter_map(|(idx, &checked)| {
if checked {
Some(self.items[idx].as_str())
} else {
None
}
})
.collect();

render.multi_select_prompt_selection(prompt, &selections[..])?;
}
}

term.show_cursor()?;
Expand Down Expand Up @@ -330,6 +341,7 @@ impl<'a> MultiSelect<'a> {
defaults: vec![],
clear: true,
prompt: None,
report: true,
max_length: None,
theme,
}
Expand Down
18 changes: 16 additions & 2 deletions src/prompts/password.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use zeroize::Zeroizing;
/// ```
pub struct Password<'a> {
prompt: String,
report: bool,
theme: &'a dyn Theme,
allow_empty_password: bool,
confirmation_prompt: Option<(String, String)>,
Expand All @@ -46,6 +47,14 @@ impl Password<'_> {
self
}

/// Indicates whether to report confirmation after interaction.
///
/// The default is to report.
pub fn report(&mut self, val: bool) -> &mut Self {
self.report = val;
self
}

/// Enables confirmation prompting.
pub fn with_confirmation<A, B>(&mut self, prompt: A, mismatch_err: B) -> &mut Self
where
Expand Down Expand Up @@ -85,15 +94,19 @@ impl Password<'_> {

if *password == *pw2 {
render.clear()?;
render.password_prompt_selection(&self.prompt)?;
if self.report {
render.password_prompt_selection(&self.prompt)?;
}
term.flush()?;
return Ok((*password).clone());
}

render.error(err)?;
} else {
render.clear()?;
render.password_prompt_selection(&self.prompt)?;
if self.report {
render.password_prompt_selection(&self.prompt)?;
}
term.flush()?;

return Ok((*password).clone());
Expand Down Expand Up @@ -122,6 +135,7 @@ impl<'a> Password<'a> {
pub fn with_theme(theme: &'a dyn Theme) -> Self {
Self {
prompt: "".into(),
report: true,
theme,
allow_empty_password: false,
confirmation_prompt: None,
Expand Down
19 changes: 16 additions & 3 deletions src/prompts/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ pub struct Select<'a> {
default: usize,
items: Vec<String>,
prompt: Option<String>,
report: bool,
clear: bool,
theme: &'a dyn Theme,
max_length: Option<usize>,
Expand Down Expand Up @@ -130,8 +131,8 @@ impl Select<'_> {

/// Sets the select prompt.
///
/// When a prompt is set the system also prints out a confirmation after
/// the selection.
/// By default, when a prompt is set the system also prints out a confirmation after
/// the selection. You can opt-out of this with [`report`](#method.report).
///
/// ## Examples
/// ```rust,no_run
Expand All @@ -149,6 +150,15 @@ impl Select<'_> {
/// ```
pub fn with_prompt<S: Into<String>>(&mut self, prompt: S) -> &mut Self {
self.prompt = Some(prompt.into());
self.report = true;
self
}

/// Indicates whether to report the selected value after interaction.
///
/// The default is to report the selection.
pub fn report(&mut self, val: bool) -> &mut Self {
self.report = val;
self
}

Expand Down Expand Up @@ -314,7 +324,9 @@ impl Select<'_> {
}

if let Some(ref prompt) = self.prompt {
render.select_prompt_selection(prompt, &self.items[sel])?;
if self.report {
render.select_prompt_selection(prompt, &self.items[sel])?;
}
}

term.show_cursor()?;
Expand Down Expand Up @@ -360,6 +372,7 @@ impl<'a> Select<'a> {
default: !0,
items: vec![],
prompt: None,
report: false,
clear: true,
max_length: None,
theme,
Expand Down

0 comments on commit f9c067b

Please sign in to comment.