Skip to main content

DevelopmentNodeEnvironment_MicrosoftVSCodeDependency_22NodeVersion_Bundle_Clean_Debug_ElectronProfile_EsbuildCompiler_Mountain/IPC/WindServiceHandlers/NativeHost/
ShowItemInFolder.rs

1//! Wire method: `native:showItemInFolder`, `nativeHost:showItemInFolder`.
2//! Reveals a path in the platform file manager (Finder / Explorer / Linux FM).
3
4use std::sync::Arc;
5
6use serde_json::Value;
7
8use crate::{RunTime::ApplicationRunTime::ApplicationRunTime, dev_log};
9
10pub async fn Fn(RunTime:Arc<ApplicationRunTime>, Arguments:Vec<Value>) -> Result<Value, String> {
11	let path_str = Arguments
12		.get(0)
13		.ok_or("Missing file path".to_string())?
14		.as_str()
15		.ok_or("File path must be a string".to_string())?;
16
17	dev_log!("vfs", "showInFolder: {}", path_str);
18
19	let path = std::path::PathBuf::from(path_str);
20
21	if !path.exists() {
22		return Err(format!("Path does not exist: {}", path_str));
23	}
24
25	#[cfg(target_os = "macos")]
26	{
27		use std::process::Command;
28
29		let result = Command::new("open")
30			.arg("-R")
31			.arg(&path)
32			.output()
33			.map_err(|Error| format!("Failed to execute open command: {}", Error))?;
34
35		if !result.status.success() {
36			return Err(format!(
37				"Failed to show item in folder: {}",
38				String::from_utf8_lossy(&result.stderr)
39			));
40		}
41	}
42
43	#[cfg(target_os = "windows")]
44	{
45		use std::process::Command;
46
47		let result = Command::new("explorer")
48			.arg("/select,")
49			.arg(&path)
50			.output()
51			.map_err(|Error| format!("Failed to execute explorer command: {}", Error))?;
52
53		if !result.status.success() {
54			return Err(format!(
55				"Failed to show item in folder: {}",
56				String::from_utf8_lossy(&result.stderr)
57			));
58		}
59	}
60
61	#[cfg(target_os = "linux")]
62	{
63		use std::process::Command;
64
65		let file_managers = ["nautilus", "dolphin", "thunar", "pcmanfm", "nemo"];
66
67		let mut last_error = String::new();
68
69		for manager in file_managers.iter() {
70			let result = Command::new(manager).arg(&path).output();
71
72			match result {
73				Ok(output) if output.status.success() => {
74					dev_log!("lifecycle", "opened with {}", manager);
75
76					break;
77				},
78
79				Err(e) => {
80					last_error = e.to_string();
81
82					continue;
83				},
84
85				_ => continue,
86			}
87		}
88
89		if !last_error.is_empty() {
90			return Err(format!("Failed to show item in folder with any file manager: {}", last_error));
91		}
92	}
93
94	dev_log!("vfs", "showed in folder: {}", path_str);
95
96	Ok(Value::Bool(true))
97}