aboutsummaryrefslogtreecommitdiff
path: root/src/lib.rs
diff options
context:
space:
mode:
authorRobin Krahl <robin.krahl@ireas.org>2019-01-08 17:16:49 +0000
committerRobin Krahl <robin.krahl@ireas.org>2019-01-08 20:55:54 +0100
commit474d92046f17a98f8979e55edaf75ca867d85b86 (patch)
treef62af448dcdd0547a88954fb514fbb1eba735f43 /src/lib.rs
parent72200eb89e91019777f84c881caf5d3cc9649fbd (diff)
downloaddialog-rs-474d92046f17a98f8979e55edaf75ca867d85b86.tar.gz
dialog-rs-474d92046f17a98f8979e55edaf75ca867d85b86.tar.bz2
Add the Password dialog box
Diffstat (limited to 'src/lib.rs')
-rw-r--r--src/lib.rs52
1 files changed, 52 insertions, 0 deletions
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<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 {