From 474d92046f17a98f8979e55edaf75ca867d85b86 Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Tue, 8 Jan 2019 17:16:49 +0000 Subject: Add the Password dialog box --- CHANGELOG.md | 3 +++ examples/password.rs | 15 +++++++++++++++ src/backends/dialog.rs | 8 +++++++- src/backends/mod.rs | 3 +++ src/lib.rs | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 examples/password.rs 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 +// 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> { + let args = vec!["--passwordbox", &password.text]; + self.execute(args, vec![], &password.title) + .and_then(get_stderr) + } + fn show_question(&self, question: &Question) -> Result { 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>; + /// Shows the given question dialog and returns the choice. fn show_question(&self, question: &super::Question) -> Result; } diff --git a/src/lib.rs b/src/lib.rs index 422252b..dd255d9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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, +} + +impl Password { + /// Creates a new password dialog box with the given text. + pub fn new(text: impl Into) -> 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) -> &mut Password { + self.title = Some(title.into()); + self + } +} + +impl DialogBox for Password { + type Output = Option; + + fn show_with(&self, backend: &impl backends::Backend) -> Result { + backend.show_password(self) + } +} + /// A user choise in a dialog box. #[derive(Clone, Copy, Debug, PartialEq)] pub enum Choice { -- cgit v1.2.3