diff options
| -rw-r--r-- | CHANGELOG.md | 2 | ||||
| -rw-r--r-- | src/backends/mod.rs | 14 | ||||
| -rw-r--r-- | src/backends/zenity.rs | 4 | ||||
| -rw-r--r-- | src/lib.rs | 12 | 
4 files changed, 31 insertions, 1 deletions
| diff --git a/CHANGELOG.md b/CHANGELOG.md index 695a4ad..b11008c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,6 @@  # Unreleased  - Refactor `default_backend` to return a `Box<dyn Backend>`. -- Check the `DIALOG` environment variable in `default_backend`. +- Check the `DIALOG` and `DISPLAY` environment variables 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 20cb8c1..f5af7ef 100644 --- a/src/backends/mod.rs +++ b/src/backends/mod.rs @@ -7,6 +7,9 @@ mod zenity;  pub use crate::backends::dialog::Dialog;  pub use crate::backends::zenity::Zenity; +use std::env; +use std::path; +  use crate::Result;  /// A dialog backend. @@ -32,6 +35,17 @@ pub trait Backend {      fn show_question(&self, question: &super::Question) -> Result<super::Choice>;  } +pub(crate) fn is_available(name: &str) -> bool { +    if let Ok(path) = env::var("PATH") { +        for part in path.split(":") { +            if path::Path::new(part).join(name).exists() { +                return true; +            } +        } +    } +    false +} +  pub(crate) fn from_str(s: &str) -> Option<Box<dyn Backend>> {      match s.to_lowercase().as_ref() {          "dialog" => Some(Box::new(Dialog::new())), diff --git a/src/backends/zenity.rs b/src/backends/zenity.rs index 4bf862b..0730c64 100644 --- a/src/backends/zenity.rs +++ b/src/backends/zenity.rs @@ -59,6 +59,10 @@ impl Zenity {          self.timeout = Some(timeout.to_string());      } +    pub(crate) fn is_available() -> bool { +        super::is_available("zenity") +    } +      fn execute(&self, args: Vec<&str>, title: &Option<String>) -> Result<process::Output> {          let mut command = process::Command::new("zenity"); @@ -341,9 +341,13 @@ impl DialogBox for Question {  /// - 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. +/// - If the `DISPLAY` environment variable is set, the first available backend from this list is +///   used: +///   - [`Zenity`][]  /// - Otherwise, a [`Dialog`][] instance is returned.  ///  /// [`Dialog`]: backends/struct.Dialog.html +/// [`Zenity`]: backends/struct.Zenity.html  pub fn default_backend() -> Box<dyn backends::Backend> {      if let Ok(backend) = env::var("DIALOG") {          if let Some(backend) = backends::from_str(&backend) { @@ -351,5 +355,13 @@ pub fn default_backend() -> Box<dyn backends::Backend> {          }      } +    if let Ok(display) = env::var("DISPLAY") { +        if !display.is_empty() { +            if backends::Zenity::is_available() { +                return Box::new(backends::Zenity::new()); +            } +        } +    } +      Box::new(backends::Dialog::new())  } | 
