Skip to main content

Mountain/IPC/WindServiceHandlers/Terminal/
LocalPTYGetDefaultShell.rs

1
2//! Pick the system default shell. Unix: `$SHELL`, then probe
3//! `/bin/{zsh,bash,sh}`. Windows: PowerShell 7 if installed,
4//! else stock Windows PowerShell. Used by Wind's "Open Default
5//! Terminal" command and by extensions that spawn unparented
6//! shells.
7
8use serde_json::{Value, json};
9
10pub async fn Fn() -> Result<Value, String> {
11	#[cfg(unix)]
12	{
13		let Shell = std::env::var("SHELL").unwrap_or_else(|_| {
14			for Path in &["/bin/zsh", "/bin/bash", "/bin/sh"] {
15				if std::path::Path::new(Path).exists() {
16					return Path.to_string();
17				}
18			}
19			"/bin/sh".to_string()
20		});
21
22		Ok(json!(Shell))
23	}
24
25	#[cfg(target_os = "windows")]
26	{
27		let SystemRoot = std::env::var("SystemRoot").unwrap_or_else(|_| "C:\\Windows".to_string());
28
29		let PwshPath = format!("{}\\PowerShell\\7\\pwsh.exe", std::env::var("ProgramFiles").unwrap_or_default());
30
31		if std::path::Path::new(&PwshPath).exists() {
32			return Ok(json!(PwshPath));
33		}
34
35		Ok(json!(format!(
36			"{}\\System32\\WindowsPowerShell\\v1.0\\powershell.exe",
37			SystemRoot
38		)))
39	}
40
41	#[cfg(not(any(unix, target_os = "windows")))]
42	{
43		Ok(json!("/bin/sh"))
44	}
45}