aboutsummaryrefslogtreecommitdiff
path: root/src/lib.rs
diff options
context:
space:
mode:
authorRobin Krahl <robin.krahl@ireas.org>2019-01-08 03:49:42 +0000
committerRobin Krahl <robin.krahl@ireas.org>2019-01-08 04:54:39 +0100
commitfce7d2b9bf30e09b615dbd77b4dd7db94d623bfb (patch)
tree5a762e9b1147329c73234e7e06aab73a3970d911 /src/lib.rs
parentec84c425282c3fd26f5e862b7864ad2ae7ec1e2e (diff)
downloaddialog-rs-fce7d2b9bf30e09b615dbd77b4dd7db94d623bfb.tar.gz
dialog-rs-fce7d2b9bf30e09b615dbd77b4dd7db94d623bfb.tar.bz2
Add question dialog boxes
Diffstat (limited to 'src/lib.rs')
-rw-r--r--src/lib.rs60
1 files changed, 60 insertions, 0 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 0c9d9f9..b51a3cc 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -9,6 +9,7 @@
//! are:
//! - [`Input`][]: a text input dialog
//! - [`Message`][]: a simple message box
+//! - [`Question`][]: a question dialog box
//!
//! These dialog boxes can be displayed using various backends:
//! - [`Dialog`][]: uses `dialog` to display ncurses-based dialog boxes (requires the external
@@ -64,6 +65,7 @@
//! [`Dialog`]: backends/struct.Dialog.html
//! [`Input`]: struct.Input.html
//! [`Message`]: struct.Message.html
+//! [`Question`]: struct.Question.html
//! [`default_backend`]: fn.default_backend.html
//! [`show`]: trait.DialogBox.html#method.show
//! [`show_with`]: trait.DialogBox.html#method.show_with
@@ -203,6 +205,64 @@ impl DialogBox for Input {
}
}
+/// A user choise in a dialog box.
+#[derive(Clone, Copy, Debug, PartialEq)]
+pub enum Choice {
+ /// The yes button.
+ Yes,
+ /// The no button.
+ No,
+ /// The cancel button or a cancelled dialog.
+ Cancel,
+}
+
+/// A question dialog box.
+///
+/// This dialog box displays a text and an optional title and has a yes and a no button. The
+/// output is the button presed by the user, or Cancel if the dialog has been cancelled.
+///
+/// # Example
+///
+/// ```no_run
+/// use dialog::DialogBox;
+///
+/// let choice = dialog::Question::new("Do you want to continue?")
+/// .title("Question")
+/// .show()
+/// .expect("Could not display dialog box");
+/// println!("The user chose: {:?}", choice);
+/// ```
+pub struct Question {
+ text: String,
+ title: Option<String>,
+}
+
+impl Question {
+ /// Creates a new question dialog with the given text.
+ pub fn new(text: impl Into<String>) -> Question {
+ Question {
+ text: text.into(),
+ title: None,
+ }
+ }
+
+ /// Sets the title of this question dialog box.
+ ///
+ /// This method returns a reference to `self` to enable chaining.
+ pub fn title(&mut self, title: impl Into<String>) -> &mut Question {
+ self.title = Some(title.into());
+ self
+ }
+}
+
+impl DialogBox for Question {
+ type Output = Choice;
+
+ fn show_with(&self, backend: &impl backends::Backend) -> Result<Self::Output> {
+ backend.show_question(self)
+ }
+}
+
/// Creates a new instance of the default backend.
///
/// The current implementation always returns a [`Dialog`][] instance.