diff options
author | Robin Krahl <robin.krahl@ireas.org> | 2019-01-08 17:16:49 +0000 |
---|---|---|
committer | Robin Krahl <robin.krahl@ireas.org> | 2019-01-08 20:55:54 +0100 |
commit | 474d92046f17a98f8979e55edaf75ca867d85b86 (patch) | |
tree | f62af448dcdd0547a88954fb514fbb1eba735f43 /src | |
parent | 72200eb89e91019777f84c881caf5d3cc9649fbd (diff) | |
download | dialog-rs-474d92046f17a98f8979e55edaf75ca867d85b86.tar.gz dialog-rs-474d92046f17a98f8979e55edaf75ca867d85b86.tar.bz2 |
Add the Password dialog box
Diffstat (limited to 'src')
-rw-r--r-- | src/backends/dialog.rs | 8 | ||||
-rw-r--r-- | src/backends/mod.rs | 3 | ||||
-rw-r--r-- | src/lib.rs | 52 |
3 files changed, 62 insertions, 1 deletions
diff --git a/src/backends/dialog.rs b/src/backends/dialog.rs index da17ad1..939b8f3 100644 --- a/src/backends/dialog.rs +++ b/src/backends/dialog.rs @@ -3,7 +3,7 @@ use std::process; -use crate::{Choice, Error, Input, Message, Question, Result}; +use crate::{Choice, Error, Input, Message, Password, Question, Result}; /// The `dialog` backend. /// @@ -135,6 +135,12 @@ impl super::Backend for Dialog { .map(|_| ()) } + fn show_password(&self, password: &Password) -> Result<Option<String>> { + let args = vec!["--passwordbox", &password.text]; + self.execute(args, vec![], &password.title) + .and_then(get_stderr) + } + fn show_question(&self, question: &Question) -> Result<Choice> { let args = vec!["--yesno", &question.text]; self.execute(args, vec![], &question.title) diff --git a/src/backends/mod.rs b/src/backends/mod.rs index fd0ddcc..1d555e3 100644 --- a/src/backends/mod.rs +++ b/src/backends/mod.rs @@ -23,6 +23,9 @@ pub trait Backend { /// Shows the given message dialog. fn show_message(&self, message: &super::Message) -> Result<()>; + /// Shows the given password dialog and returns the password. + fn show_password(&self, password: &super::Password) -> Result<Option<String>>; + /// Shows the given question dialog and returns the choice. fn show_question(&self, question: &super::Question) -> Result<super::Choice>; } @@ -9,6 +9,7 @@ //! are: //! - [`Input`][]: a text input dialog //! - [`Message`][]: a simple message box +//! - [`Password`][]: a password input dialog //! - [`Question`][]: a question dialog box //! //! These dialog boxes can be displayed using various backends: @@ -65,6 +66,7 @@ //! [`Dialog`]: backends/struct.Dialog.html //! [`Input`]: struct.Input.html //! [`Message`]: struct.Message.html +//! [`Password`]: struct.Password.html //! [`Question`]: struct.Question.html //! [`default_backend`]: fn.default_backend.html //! [`show`]: trait.DialogBox.html#method.show @@ -258,6 +260,56 @@ impl DialogBox for Input { } } +/// A dialog box with a password input field. +/// +/// This dialog box displays a text and a password input field. It returns the password entered by +/// the user or `None` if the user cancelled the dialog. +/// +/// # Example +/// +/// ```no_run +/// use dialog::DialogBox; +/// +/// let password = dialog::Password::new("Please enter a new password") +/// .title("Password") +/// .show() +/// .expect("Could not display dialog box"); +/// match password { +/// Some(password) => println!("Your new password is: {}", password), +/// None => println!("You do not want to have a password."), +/// }; +/// ``` +pub struct Password { + text: String, + title: Option<String>, +} + +impl Password { + /// Creates a new password dialog box with the given text. + pub fn new(text: impl Into<String>) -> Password { + Password { + text: text.into(), + title: None, + } + } + + /// Sets the title of this password dialog box. + /// + /// This method returns a reference to `self` to enable chaining. + pub fn title(&mut self, title: impl Into<String>) -> &mut Password { + self.title = Some(title.into()); + self + } +} + +impl DialogBox for Password { + type Output = Option<String>; + + fn show_with(&self, backend: &impl backends::Backend) -> Result<Self::Output> { + backend.show_password(self) + } +} + /// A user choise in a dialog box. #[derive(Clone, Copy, Debug, PartialEq)] pub enum Choice { |