aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobin Krahl <robin.krahl@ireas.org>2019-01-11 01:24:16 +0000
committerRobin Krahl <robin.krahl@ireas.org>2019-01-11 02:25:54 +0100
commit02190c44f7072b4fdaca50836a583f955866f8de (patch)
tree46dd928ca34ff174b6393bb3ffc71c73c0ef3f4f
parent3efbb1ddc8b85f85621db9b37a0980ec88d2386d (diff)
downloaddialog-rs-02190c44f7072b4fdaca50836a583f955866f8de.tar.gz
dialog-rs-02190c44f7072b4fdaca50836a583f955866f8de.tar.bz2
Check the DIALOG environment variable in default_backend
This patch changes the logic in default_backend to respect the DIALOG environment variable that may contain the name of the backend to use.
-rw-r--r--CHANGELOG.md3
-rw-r--r--src/backends/mod.rs8
-rw-r--r--src/lib.rs14
3 files changed, 23 insertions, 2 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 58a4919..695a4ad 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,5 +1,6 @@
# Unreleased
-- Refactor `default_backend()` to return a `Box<dyn Backend>`.
+- Refactor `default_backend` to return a `Box<dyn Backend>`.
+- Check the `DIALOG` environment variable in `default_backend`.
# v0.1.1 (2019-01-11)
- Add the `Password` dialog box.
diff --git a/src/backends/mod.rs b/src/backends/mod.rs
index b9cecd0..20cb8c1 100644
--- a/src/backends/mod.rs
+++ b/src/backends/mod.rs
@@ -31,3 +31,11 @@ pub trait Backend {
/// Shows the given question dialog and returns the choice.
fn show_question(&self, question: &super::Question) -> Result<super::Choice>;
}
+
+pub(crate) fn from_str(s: &str) -> Option<Box<dyn Backend>> {
+ match s.to_lowercase().as_ref() {
+ "dialog" => Some(Box::new(Dialog::new())),
+ "zenity" => Some(Box::new(Zenity::new())),
+ _ => None,
+ }
+}
diff --git a/src/lib.rs b/src/lib.rs
index 87234bd..3a6a090 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -85,6 +85,8 @@ mod error;
/// [`Backend`]: trait.Backend.html
pub mod backends;
+use std::env;
+
pub use crate::error::{Error, Result};
/// A dialog box that can be shown using a backend.
@@ -335,9 +337,19 @@ impl DialogBox for Question {
/// Creates a new instance of the default backend.
///
-/// The current implementation always returns a [`Dialog`][] instance.
+/// The following steps are performed to determine the default backend:
+/// - If the `DIALOG` environment variable is set to a valid backend name, this backend is used.
+/// A valid backend name is the name of a struct in the `backends` module implementing the
+/// `Backend` trait in any case.
+/// - Otherwise, a [`Dialog`][] instance is returned.
///
/// [`Dialog`]: backends/struct.Dialog.html
pub fn default_backend() -> Box<dyn backends::Backend> {
+ if let Ok(backend) = env::var("DIALOG") {
+ if let Some(backend) = backends::from_str(&backend) {
+ return backend;
+ }
+ }
+
Box::new(backends::Dialog::new())
}