aboutsummaryrefslogtreecommitdiff
path: root/src/file_system_utils.rs
diff options
context:
space:
mode:
author1992414357@qq.com <1992414357@qq.com>2025-06-09 10:26:17 +0800
committer1992414357@qq.com <1992414357@qq.com>2025-06-09 10:26:17 +0800
commitd38f9e3c3e680fb21d0dcd6e94d486bbadf8b6e9 (patch)
tree91fde277cb5346a4d9c4141d17ba84963eec1088 /src/file_system_utils.rs
parent7b68281155ea7bc2f5840b921f4a2f5ab7b8ff68 (diff)
修改了 Idea 环境下的运行配置.
Diffstat (limited to 'src/file_system_utils.rs')
-rw-r--r--src/file_system_utils.rs54
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