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
23		Ok(json!(Shell))
24	}
25
26	#[cfg(target_os = "windows")]
27	{
28		let SystemRoot = std::env::var("SystemRoot").unwrap_or_else(|_| "C:\\Windows".to_string());
29
30		let PwshPath = format!("{}\\PowerShell\\7\\pwsh.exe", std::env::var("ProgramFiles").unwrap_or_default());
31
32		if std::path::Path::new(&PwshPath).exists() {
33			return Ok(json!(PwshPath));
34		}
35
36		Ok(json!(format!(
37			"{}\\System32\\WindowsPowerShell\\v1.0\\powershell.exe",
38			SystemRoot
39		)))
40	}
41
42	#[cfg(not(any(unix, target_os = "windows")))]
43	{
44		Ok(json!("/bin/sh"))
45	}
46}