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 'interact_text_opt' and 'interact_text_on_opt' for 'Input' prompts #278

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
40 changes: 36 additions & 4 deletions src/prompts/input.rs
Expand Up @@ -289,12 +289,38 @@ where
/// while [`interact`](Self::interact) allows virtually any character to be used e.g arrow keys.
///
/// The dialog is rendered on stderr.
/// This unlike [`interact_text_opt`](Self::interact_text_opt) does not allow to quit with 'Esc'
pub fn interact_text(self) -> Result<T> {
self.interact_text_on(&Term::stderr())
}

/// Enables the user to enter a printable ascii sequence and returns the result.
///
/// Its difference from [`interact_opt`](Self::interact_opt) is that it only allows ascii characters for string,
tbergerd marked this conversation as resolved.
Show resolved Hide resolved
/// while [`interact_opt`](Self::interact_opt) allows virtually any character to be used e.g arrow keys.
///
/// The dialog is rendered on stderr.
/// Result contains `Some(T)` if user hit 'Enter' or `None` if user cancelled with 'Esc'
#[inline]
pub fn interact_text_opt(self) -> Result<Option<T>> {
self.interact_text_on_opt(&Term::stderr())
}

/// Like [`interact_text`](Self::interact_text) but allows a specific terminal to be set.
pub fn interact_text_on(mut self, term: &Term) -> Result<T> {
#[inline]
pub fn interact_text_on(self, term: &Term) -> Result<T> {
Ok(self
._interact_text_on(term, false)?
.ok_or_else(|| io::Error::new(io::ErrorKind::Other, "Quit not allowed in this case"))?)
}

/// Like [`interact_text_opt`](Self::interact_text_opt) but allows a specific terminal to be set.
#[inline]
pub fn interact_text_on_opt(self, term: &Term) -> Result<Option<T>> {
self._interact_text_on(term, true)
}

fn _interact_text_on(mut self, term: &Term, allow_quit: bool) -> Result<Option<T>> {
Copy link
Contributor

Choose a reason for hiding this comment

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

You should follow a builder pattern here, so the signature of this function shouldn't change.
Instead, add a new private field to the Input, and a corresponding setter for it.
Access it here via self.

if !term.is_term() {
return Err(io::Error::new(io::ErrorKind::NotConnected, "not a terminal").into());
}
Expand All @@ -315,7 +341,7 @@ where

// Read input by keystroke so that we can suppress ascii control characters
if !term.features().is_attended() {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

note : removed this because the check is already performed at the beginning of the function (and returns an error instead which makes more sense)

return Ok("".to_owned().parse::<T>().unwrap());
return Ok(Some("".to_owned().parse::<T>().unwrap()));
}

let mut chars: Vec<char> = Vec::new();
Expand All @@ -332,6 +358,12 @@ where

loop {
match term.read_key()? {
Key::Escape if allow_quit => {
render.clear()?;
term.flush()?;
term.show_cursor()?;
return Ok(None);
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's instead add a new variant for the Error like UserInterrupted and return it here.

}
Key::Backspace if position > 0 => {
position -= 1;
chars.remove(position);
Expand Down Expand Up @@ -591,7 +623,7 @@ where
render.input_prompt_selection(&self.prompt, &default.to_string())?;
}
term.flush()?;
return Ok(default.clone());
return Ok(Some(default.clone()));
} else if !self.permit_empty {
continue;
}
Expand Down Expand Up @@ -620,7 +652,7 @@ where
}
term.flush()?;

return Ok(value);
return Ok(Some(value));
}
Err(err) => {
render.error(&err.to_string())?;
Expand Down