Skip to main content

Mountain/IPC/WindServiceHandlers/Terminal/
LocalPTYGetDefaultShell.rs

1//! Pick the system default shell. Unix: `$SHELL`, then probe
2//! `/bin/{zsh,bash,sh}`. Windows: PowerShell 7 if installed,
3//! else stock Windows PowerShell. Used by Wind's "Open Default
4//! Terminal" command and by extensions that spawn unparented
5//! shells.
6
7use serde_json::{Value, json};
8
9pub async fn Fn() -> Result<Value, String> {
10	#[cfg(unix)]
11	{
12		let Shell = std::env::var("SHELL").unwrap_or_else(|_| {
13			for Path in &["/bin/zsh", "/bin/bash", "/bin/sh"] {
14				if std::path::Path::new(Path).exists() {
15					return Path.to_string();
16				}
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}