diff options
-rw-r--r-- | CHANGELOG.md | 3 | ||||
-rw-r--r-- | examples/password.rs | 15 | ||||
-rw-r--r-- | src/backends/dialog.rs | 8 | ||||
-rw-r--r-- | src/backends/mod.rs | 3 | ||||
-rw-r--r-- | src/lib.rs | 52 |
5 files changed, 80 insertions, 1 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 11dc234..8759e6e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +# Unreleased +- Add the `Password` dialog box. + # v0.1.0 (2019-01-08) - Initial release with the `Input`, `Message` and `Question` dialog boxes and the `Dialog` backend. diff --git a/examples/password.rs b/examples/password.rs new file mode 100644 index 0000000..7504bdd --- /dev/null +++ b/examples/password.rs @@ -0,0 +1,15 @@ +// Copyright (C) 2019 Robin Krahl <robin.krahl@ireas.org> +// SPDX-License-Identifier: MIT + +use dialog::DialogBox; + +fn main() -> dialog::Result<()> { + let password = dialog::Password::new("Please enter a new password") + .title("Password") + .show()?; + match password { + Some(password) => println!("Your new password is: {}", password), + None => println!("You do not want to have a password."), + }; + Ok(()) +} 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 { |