diff --git a/espanso/src/cli/launcher/mod.rs b/espanso/src/cli/launcher/mod.rs index a440e75..bd71640 100644 --- a/espanso/src/cli/launcher/mod.rs +++ b/espanso/src/cli/launcher/mod.rs @@ -71,16 +71,25 @@ fn launcher_main(args: CliModuleArgs) -> i32 { }, }); - // TODO: enable "Add to PATH" page only when NOT in portable mode - // TODO: if the user clicks on "Continue" after unchecking the "ADD to PATH" - // checkbox, we should remember (with the kvs) the choice and avoid asking again. - let is_add_path_page_enabled = if cfg!(target_os = "macos") { - // TODO: add actual check - // TODO: consider also Windows case - true - } else { - false - }; + let is_add_path_page_enabled = + if cfg!(not(target_os = "linux")) && !preferences.has_completed_wizard() { + if cfg!(target_os = "macos") { + !crate::path::is_espanso_in_path() + } else { + // TODO: enable "Add to PATH" page only when NOT in portable mode + !crate::path::is_espanso_in_path() + } + } else { + false + }; + // TODO: consider also Windows case? + let add_to_path_handler = Box::new(move || match util::add_espanso_to_path() { + Ok(_) => true, + Err(error) => { + eprintln!("Add to path returned error: {}", error); + false + } + }); let is_accessibility_page_enabled = if cfg!(target_os = "macos") { // TODO: add actual check @@ -89,6 +98,11 @@ fn launcher_main(args: CliModuleArgs) -> i32 { false }; + let preferences_clone = preferences.clone(); + let on_completed_handler = Box::new(move || { + preferences_clone.set_completed_wizard(true); + }); + // TODO: show a "espanso is now running page at the end" (it should be triggered everytime // espanso is started, unless the user unchecks "show this message at startup") // This page could also be used when the user starts espanso, but an instance is already running. @@ -120,14 +134,13 @@ fn launcher_main(args: CliModuleArgs) -> i32 { handlers: WizardHandlers { is_legacy_version_running: Some(is_legacy_version_running_handler), backup_and_migrate: Some(backup_and_migrate_handler), - add_to_path: None, // TODO + add_to_path: Some(add_to_path_handler), enable_accessibility: None, // TODO is_accessibility_enabled: None, // TODO + on_completed: Some(on_completed_handler), }, }); - // TODO: check the wizard return status? - preferences.set_completed_wizard(true); } // TODO: initialize config directory if not present diff --git a/espanso/src/cli/launcher/util.rs b/espanso/src/cli/launcher/util.rs index b792417..020c661 100644 --- a/espanso/src/cli/launcher/util.rs +++ b/espanso/src/cli/launcher/util.rs @@ -76,3 +76,24 @@ pub enum MigrationError { #[error("unexpected error")] UnexpectedError, } + +pub fn add_espanso_to_path() -> Result<()> { + let espanso_exe_path = std::env::current_exe()?; + let mut command = Command::new(&espanso_exe_path.to_string_lossy().to_string()); + command.args(&["env-path", "--prompt", "register"]); + + let mut child = command.spawn()?; + let result = child.wait()?; + + if result.success() { + Ok(()) + } else { + Err(AddToPathError::NonZeroExitCode.into()) + } +} + +#[derive(Error, Debug)] +pub enum AddToPathError { + #[error("unexpected error, 'espanso env-path register' returned a non-zero exit code.")] + NonZeroExitCode, +} \ No newline at end of file diff --git a/espanso/src/preferences/mod.rs b/espanso/src/preferences/mod.rs index a83c57f..bc86a1b 100644 --- a/espanso/src/preferences/mod.rs +++ b/espanso/src/preferences/mod.rs @@ -22,7 +22,7 @@ use anyhow::Result; mod default; -pub trait Preferences: Send + Sync { +pub trait Preferences: Send + Sync + Clone { fn has_completed_wizard(&self) -> bool; fn set_completed_wizard(&self, value: bool); }