diff options
Diffstat (limited to 'src/file_system_utils.rs')
| -rw-r--r-- | src/file_system_utils.rs | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/src/file_system_utils.rs b/src/file_system_utils.rs new file mode 100644 index 0000000..3296324 --- /dev/null +++ b/src/file_system_utils.rs @@ -0,0 +1,54 @@ +use std::io::Error; +use std::path::PathBuf; +use std::process::Command; + +pub fn open_in_explorer(path: PathBuf) -> Result<(), Error> { + let absolute_path = if path.is_relative() { + std::env::current_dir()?.join(path) + } else { + path + }; + + let absolute_path = absolute_path.canonicalize().unwrap_or(absolute_path); + + if !absolute_path.exists() { + return Err(Error::new( + std::io::ErrorKind::NotFound, + format!("Path does not exist: {}", absolute_path.display()) + )); + } + + if cfg!(target_os = "windows") { + if absolute_path.is_file() { + absolute_path.parent() + .ok_or_else(|| Error::new( + std::io::ErrorKind::InvalidInput, + "File has no parent directory" + ))?; + + Command::new("explorer") + .args(&["/select,", absolute_path.to_str().unwrap()]) + .spawn()?; + } else { + Command::new("explorer") + .arg(absolute_path.to_str().unwrap()) + .spawn()?; + } + } else if cfg!(target_os = "macos") { + if absolute_path.is_file() { + Command::new("open") + .args(&["-R", absolute_path.to_str().unwrap()]) + .spawn()?; + } else { + Command::new("open") + .arg(absolute_path.to_str().unwrap()) + .spawn()?; + } + } else { + Command::new("xdg-open") + .arg(absolute_path.to_str().unwrap()) + .spawn()?; + } + + Ok(()) +}
\ No newline at end of file |
