summaryrefslogtreecommitdiffstats
path: root/src/utils
diff options
context:
space:
mode:
authorsigoden <sigoden@gmail.com>2024-05-18 22:34:23 +0800
committerGitHub <noreply@github.com>2024-05-18 22:34:23 +0800
commitd16cca23acf2bcae05cecdb2f6bf85fe3b7cb1cb (patch)
treefecae3ac64cd7a8296018a8bc1a3d37fcee85b0e /src/utils
parent665693ccf2f6c77a261b0f34c8fab7bf9e26d6c0 (diff)
downloadaichat-d16cca23acf2bcae05cecdb2f6bf85fe3b7cb1cb.tar.gz
refactor: shell detect/prompt (#519)
Diffstat (limited to 'src/utils')
-rw-r--r--src/utils/command.rs48
1 files changed, 29 insertions, 19 deletions
diff --git a/src/utils/command.rs b/src/utils/command.rs
index 77ca813..873e40b 100644
--- a/src/utils/command.rs
+++ b/src/utils/command.rs
@@ -16,18 +16,32 @@ pub fn detect_os() -> String {
os.to_string()
}
-pub fn detect_shell() -> (String, String, &'static str) {
+pub struct Shell {
+ pub name: String,
+ pub cmd: String,
+ pub arg: String,
+}
+
+impl Shell {
+ pub fn new(name: &str, cmd: &str, arg: &str) -> Self {
+ Self {
+ name: name.to_string(),
+ cmd: cmd.to_string(),
+ arg: arg.to_string(),
+ }
+ }
+}
+
+pub fn detect_shell() -> Shell {
let os = env::consts::OS;
if os == "windows" {
- 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| {
+ 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"))
+ Some(Shell::new("pwsh", "pwsh.exe", "-c"))
} else {
- Some(("powershell".into(), "powershell.exe".into(), "-Command"))
+ Some(Shell::new("powershell", "powershell.exe", "-Command"))
}
} else {
None
@@ -35,22 +49,18 @@ pub fn detect_shell() -> (String, String, &'static str) {
}) {
ret
} else {
- ("cmd".into(), "cmd.exe".into(), "/C")
+ Shell::new("cmd", "cmd.exe", "/C")
}
- } else if env::var("NU_VERSION").is_ok() {
- ("nushell".into(), "nu".into(), "-c")
} else {
- 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
+ let shell = env::var("SHELL").unwrap_or_else(|_| "/bin/sh".to_string());
+ let shell = match shell.rsplit_once('/') {
+ Some((_, v)) => v,
+ None => &shell,
};
- (shell_name, shell_cmd, "-c")
+ match shell {
+ "bash" | "zsh" | "fish" | "pwsh" => Shell::new(shell, shell, "-c"),
+ _ => Shell::new("sh", "sh", "-c"),
+ }
}
}