From 60e422e4a1998948518db72bc7d1630e496653c4 Mon Sep 17 00:00:00 2001 From: Jeremy Soller Date: Wed, 3 Jan 2024 15:41:01 -0700 Subject: [PATCH] Add commands for opening files for different OS --- src/tab.rs | 39 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/src/tab.rs b/src/tab.rs index 6590ebc..1cf9580 100644 --- a/src/tab.rs +++ b/src/tab.rs @@ -16,6 +16,36 @@ use crate::mime_icon::mime_icon; const DOUBLE_CLICK_DURATION: Duration = Duration::from_millis(500); +#[cfg(target_os = "linux")] +pub fn open_command(path: &PathBuf) -> process::Command { + let mut command = process::Command::new("xdg-open"); + command.arg(path); + command +} + +#[cfg(target_os = "macos")] +pub fn open_command(path: &PathBuf) -> process::Command { + let mut command = process::Command::new("open"); + command.arg(path); + command +} + +#[cfg(target_os = "redox")] +pub fn open_command(path: &PathBuf) -> process::Command { + let mut command = process::Command::new("launcher"); + command.arg(path); + command +} + +#[cfg(target_os = "windows")] +pub fn open_command(path: &PathBuf) -> process::Command { + let mut command = process::Command::new("cmd"); + command.arg("/c"); + command.arg("start"); + command.arg(path); + command +} + #[derive(Clone, Copy, Debug)] pub enum Message { Click(usize), @@ -120,8 +150,13 @@ impl Tab { if item.is_dir { cd = Some(item.path.clone()); } else { - //TODO: support multiple OS - process::Command::new("xdg-open").arg(&item.path).spawn(); + let mut command = open_command(&item.path); + match command.spawn() { + Ok(_) => (), + Err(err) => { + log::warn!("failed to open {:?}: {}", item.path, err); + } + } } } }