diff options
| author | sigoden <sigoden@gmail.com> | 2024-03-02 15:44:08 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-03-02 15:44:08 +0800 |
| commit | bc3cbce2f4e9d7cd543ce39a6630f0c451f35d12 (patch) | |
| tree | c58bd3e37ec5973c8c2f374df4d188e61adb1171 /src/utils/mod.rs | |
| parent | bbb13d8227913ff971efff5466d1caa0061d4990 (diff) | |
| download | aichat-bc3cbce2f4e9d7cd543ce39a6630f0c451f35d12.tar.gz | |
refactor: enhanced shell support (#326)
Diffstat (limited to 'src/utils/mod.rs')
| -rw-r--r-- | src/utils/mod.rs | 53 |
1 files changed, 35 insertions, 18 deletions
diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 7dacd5c..b59b7ad 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -103,33 +103,50 @@ pub fn detect_os() -> String { os.to_string() } -pub fn detect_shell() -> String { +pub fn detect_shell() -> (String, String, &'static str) { let os = env::consts::OS; if os == "windows" { - if let Some(true) = env::var("PSModulePath") - .ok() - .map(|v| v.split(';').count() >= 3) - { - "powershell.exe".into() + if env::var("NU_VERSION").is_ok() { + ("nushell".into(), "nu.exe".into(), "-c") + } else if let Some(ret) = env::var("PSModulePath").ok().and_then(|v| { + let v = v.to_lowercase(); + if v.split(';').count() >= 3 { + if v.contains("powershell\\7\\") { + Some(("pwsh".into(), "pwsh.exe".into(), "-c")) + } else { + Some(("powershell".into(), "powershell.exe".into(), "-Command")) + } + } else { + None + } + }) { + ret } else { - "cmd.exe".into() + ("cmd".into(), "cmd.exe".into(), "/C") } + } else if env::var("NU_VERSION").is_ok() { + ("nushell".into(), "nu".into(), "-c") } else { - env::var("SHELL").unwrap_or_else(|_| "/bin/sh".to_string()) + let shell_cmd = env::var("SHELL").unwrap_or_else(|_| "/bin/sh".to_string()); + let shell_name = match shell_cmd.rsplit_once('/') { + Some((_, name)) => name.to_string(), + None => shell_cmd.clone(), + }; + let shell_name = if shell_name == "nu" { + "nushell".into() + } else { + shell_name + }; + (shell_name, shell_cmd, "-c") } } pub fn run_command(eval_str: &str) -> anyhow::Result<i32> { - let shell = detect_shell(); - let mut command = Command::new(&shell); - if shell == "powershell.exe" { - command.arg("-Command").arg(eval_str); - } else if shell == "cmd.exe" { - command.arg("/c").arg(eval_str); - } else { - command.arg("-c").arg(eval_str); - }; - let status = command.status()?; + let (_shell_name, shell_cmd, shell_arg) = detect_shell(); + let status = Command::new(shell_cmd) + .arg(shell_arg) + .arg(eval_str) + .status()?; Ok(status.code().unwrap_or_default()) } |
