summaryrefslogtreecommitdiff
path: root/src/backends
diff options
context:
space:
mode:
authorRobin Krahl <robin.krahl@ireas.org>2019-01-11 01:54:49 +0000
committerRobin Krahl <robin.krahl@ireas.org>2019-01-11 02:56:32 +0100
commitdc754cbc9763b3ea6979ed55bd4e8030fe073078 (patch)
treea9ea62a8486450861883fa2d9e2362c594a3f1b4 /src/backends
parent02190c44f7072b4fdaca50836a583f955866f8de (diff)
downloaddialog-rs-dc754cbc9763b3ea6979ed55bd4e8030fe073078.tar.gz
dialog-rs-dc754cbc9763b3ea6979ed55bd4e8030fe073078.tar.bz2
Check DISPLAY environment variable in default_backend
This patch changes the default_backend to also check the DISPLAY environment variable. If it is set, there probably is a X server running, so we try to use the zenity backend. Otherwise, the dialog backend is used.
Diffstat (limited to 'src/backends')
-rw-r--r--src/backends/mod.rs14
-rw-r--r--src/backends/zenity.rs4
2 files changed, 18 insertions, 0 deletions
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");