Skip to main content

Mountain/IPC/WindServiceHandlers/Terminal/
LocalPTYGetDefaultShell.rs

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