diff --git a/src/check.rs b/src/check.rs new file mode 100644 index 0000000..1ba1092 --- /dev/null +++ b/src/check.rs @@ -0,0 +1,40 @@ +// This functions are used to check if the required dependencies are satisfied +// before starting espanso + +#[cfg(target_os = "linux")] +pub fn check_dependencies() -> bool { + use std::process::Command; + + let mut result = true; + + // Make sure notify-send is installed + let status = Command::new("notify-send") + .arg("-v") + .output(); + if let Err(_) = status { + println!("Error: 'notify-send' command is needed for espanso to work correctly, please install it."); + result = false; + } + + // Make sure xclip is installed + let status = Command::new("xclip") + .arg("-version") + .output(); + if let Err(_) = status { + println!("Error: 'xclip' command is needed for espanso to work correctly, please install it."); + result = false; + } + + result +} + +#[cfg(target_os = "macos")] +pub fn check_dependencies() -> bool { + // TODO: check accessibility +} + +#[cfg(target_os = "windows")] +pub fn check_dependencies() -> bool { + // Nothing needed on windows + true +} \ No newline at end of file diff --git a/src/clipboard/linux.rs b/src/clipboard/linux.rs index c2c0b58..8b4eedf 100644 --- a/src/clipboard/linux.rs +++ b/src/clipboard/linux.rs @@ -1,5 +1,6 @@ use std::process::{Command, Stdio}; use std::io::{Write}; +use log::error; pub struct LinuxClipboardManager {} @@ -31,8 +32,8 @@ impl super::ClipboardManager for LinuxClipboardManager { if let Some(output) = stdin { let res = output.write_all(payload.as_bytes()); - if let Err(_) = res { - // TODO: log error + if let Err(e) = res { + error!("Could not set clipboard: {}", e); } } } @@ -41,8 +42,6 @@ impl super::ClipboardManager for LinuxClipboardManager { impl LinuxClipboardManager { pub fn new() -> LinuxClipboardManager { - // TODO: check if xclip is present and log an error otherwise. - LinuxClipboardManager{} } } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 6230963..09f62e3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -27,6 +27,7 @@ use std::io::{BufReader, BufRead}; mod ui; mod event; +mod check; mod bridge; mod engine; mod config; @@ -154,6 +155,9 @@ fn main() { restart_main(config_set); return; } + + // Defaults to start subcommand + start_main(config_set); } /// Daemon subcommand, start the event loop and spawn a background thread worker @@ -165,6 +169,8 @@ fn daemon_main(config_set: ConfigSet) { exit(3); } + precheck_guard(); + // Initialize log let log_level = match config_set.default.log_level { 0 => LevelFilter::Warn, @@ -261,6 +267,8 @@ fn start_main(config_set: ConfigSet) { } release_lock(lock_file.unwrap()); + precheck_guard(); + if cfg!(target_os = "windows") { // TODO: start windows detached }else{ @@ -468,4 +476,14 @@ fn acquire_lock() -> Option<File> { fn release_lock(lock_file: File) { lock_file.unlock().unwrap() +} + +/// Used to make sure all the required dependencies are present before starting espanso. +fn precheck_guard() { + let satisfied = check::check_dependencies(); + if !satisfied { + println!(); + println!("Pre-check was not successful, espanso could not be started."); + exit(5); + } } \ No newline at end of file