shell.rs

  1use schemars::JsonSchema;
  2use serde::{Deserialize, Serialize};
  3use std::{borrow::Cow, fmt, path::Path, sync::LazyLock};
  4
  5/// Shell configuration to open the terminal with.
  6#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq, Eq, JsonSchema, Hash)]
  7#[serde(rename_all = "snake_case")]
  8pub enum Shell {
  9    /// Use the system's default terminal configuration in /etc/passwd
 10    #[default]
 11    System,
 12    /// Use a specific program with no arguments.
 13    Program(String),
 14    /// Use a specific program with arguments.
 15    WithArguments {
 16        /// The program to run.
 17        program: String,
 18        /// The arguments to pass to the program.
 19        args: Vec<String>,
 20        /// An optional string to override the title of the terminal tab
 21        title_override: Option<String>,
 22    },
 23}
 24
 25impl Shell {
 26    pub fn program(&self) -> String {
 27        match self {
 28            Shell::Program(program) => program.clone(),
 29            Shell::WithArguments { program, .. } => program.clone(),
 30            Shell::System => get_system_shell(),
 31        }
 32    }
 33
 34    pub fn program_and_args(&self) -> (String, &[String]) {
 35        match self {
 36            Shell::Program(program) => (program.clone(), &[]),
 37            Shell::WithArguments { program, args, .. } => (program.clone(), args),
 38            Shell::System => (get_system_shell(), &[]),
 39        }
 40    }
 41
 42    pub fn shell_kind(&self, is_windows: bool) -> ShellKind {
 43        match self {
 44            Shell::Program(program) => ShellKind::new(program, is_windows),
 45            Shell::WithArguments { program, .. } => ShellKind::new(program, is_windows),
 46            Shell::System => ShellKind::system(),
 47        }
 48    }
 49}
 50
 51#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
 52pub enum ShellKind {
 53    #[default]
 54    Posix,
 55    Csh,
 56    Tcsh,
 57    Rc,
 58    Fish,
 59    /// Pre-installed "legacy" powershell for windows
 60    PowerShell,
 61    /// PowerShell 7.x
 62    Pwsh,
 63    Nushell,
 64    Cmd,
 65    Xonsh,
 66    Elvish,
 67}
 68
 69pub fn get_system_shell() -> String {
 70    if cfg!(windows) {
 71        get_windows_system_shell()
 72    } else {
 73        std::env::var("SHELL").unwrap_or("/bin/sh".to_string())
 74    }
 75}
 76
 77pub fn get_default_system_shell() -> String {
 78    if cfg!(windows) {
 79        get_windows_system_shell()
 80    } else {
 81        "/bin/sh".to_string()
 82    }
 83}
 84
 85/// Get the default system shell, preferring bash on Windows.
 86pub fn get_default_system_shell_preferring_bash() -> String {
 87    if cfg!(windows) {
 88        get_windows_bash().unwrap_or_else(|| get_windows_system_shell())
 89    } else {
 90        "/bin/sh".to_string()
 91    }
 92}
 93
 94pub fn get_windows_bash() -> Option<String> {
 95    use std::path::PathBuf;
 96
 97    fn find_bash_in_scoop() -> Option<PathBuf> {
 98        let bash_exe =
 99            PathBuf::from(std::env::var_os("USERPROFILE")?).join("scoop\\shims\\bash.exe");
100        bash_exe.exists().then_some(bash_exe)
101    }
102
103    fn find_bash_in_git() -> Option<PathBuf> {
104        // /path/to/git/cmd/git.exe/../../bin/bash.exe
105        let git = which::which("git").ok()?;
106        let git_bash = git.parent()?.parent()?.join("bin").join("bash.exe");
107        git_bash.exists().then_some(git_bash)
108    }
109
110    static BASH: LazyLock<Option<String>> = LazyLock::new(|| {
111        let bash = find_bash_in_scoop()
112            .or_else(|| find_bash_in_git())
113            .map(|p| p.to_string_lossy().into_owned());
114        if let Some(ref path) = bash {
115            log::info!("Found bash at {}", path);
116        }
117        bash
118    });
119
120    (*BASH).clone()
121}
122
123pub fn get_windows_system_shell() -> String {
124    use std::path::PathBuf;
125
126    fn find_pwsh_in_programfiles(find_alternate: bool, find_preview: bool) -> Option<PathBuf> {
127        #[cfg(target_pointer_width = "64")]
128        let env_var = if find_alternate {
129            "ProgramFiles(x86)"
130        } else {
131            "ProgramFiles"
132        };
133
134        #[cfg(target_pointer_width = "32")]
135        let env_var = if find_alternate {
136            "ProgramW6432"
137        } else {
138            "ProgramFiles"
139        };
140
141        let install_base_dir = PathBuf::from(std::env::var_os(env_var)?).join("PowerShell");
142        install_base_dir
143            .read_dir()
144            .ok()?
145            .filter_map(Result::ok)
146            .filter(|entry| matches!(entry.file_type(), Ok(ft) if ft.is_dir()))
147            .filter_map(|entry| {
148                let dir_name = entry.file_name();
149                let dir_name = dir_name.to_string_lossy();
150
151                let version = if find_preview {
152                    let dash_index = dir_name.find('-')?;
153                    if &dir_name[dash_index + 1..] != "preview" {
154                        return None;
155                    };
156                    dir_name[..dash_index].parse::<u32>().ok()?
157                } else {
158                    dir_name.parse::<u32>().ok()?
159                };
160
161                let exe_path = entry.path().join("pwsh.exe");
162                if exe_path.exists() {
163                    Some((version, exe_path))
164                } else {
165                    None
166                }
167            })
168            .max_by_key(|(version, _)| *version)
169            .map(|(_, path)| path)
170    }
171
172    fn find_pwsh_in_msix(find_preview: bool) -> Option<PathBuf> {
173        let msix_app_dir =
174            PathBuf::from(std::env::var_os("LOCALAPPDATA")?).join("Microsoft\\WindowsApps");
175        if !msix_app_dir.exists() {
176            return None;
177        }
178
179        let prefix = if find_preview {
180            "Microsoft.PowerShellPreview_"
181        } else {
182            "Microsoft.PowerShell_"
183        };
184        msix_app_dir
185            .read_dir()
186            .ok()?
187            .filter_map(|entry| {
188                let entry = entry.ok()?;
189                if !matches!(entry.file_type(), Ok(ft) if ft.is_dir()) {
190                    return None;
191                }
192
193                if !entry.file_name().to_string_lossy().starts_with(prefix) {
194                    return None;
195                }
196
197                let exe_path = entry.path().join("pwsh.exe");
198                exe_path.exists().then_some(exe_path)
199            })
200            .next()
201    }
202
203    fn find_pwsh_in_scoop() -> Option<PathBuf> {
204        let pwsh_exe =
205            PathBuf::from(std::env::var_os("USERPROFILE")?).join("scoop\\shims\\pwsh.exe");
206        pwsh_exe.exists().then_some(pwsh_exe)
207    }
208
209    static SYSTEM_SHELL: LazyLock<String> = LazyLock::new(|| {
210        let locations = [
211            || find_pwsh_in_programfiles(false, false),
212            || find_pwsh_in_programfiles(true, false),
213            || find_pwsh_in_msix(false),
214            || find_pwsh_in_programfiles(false, true),
215            || find_pwsh_in_msix(true),
216            || find_pwsh_in_programfiles(true, true),
217            || find_pwsh_in_scoop(),
218            || which::which_global("pwsh.exe").ok(),
219            || which::which_global("powershell.exe").ok(),
220        ];
221
222        locations
223            .into_iter()
224            .find_map(|f| f())
225            .map(|p| p.to_string_lossy().trim().to_owned())
226            .inspect(|shell| log::info!("Found powershell in: {}", shell))
227            .unwrap_or_else(|| {
228                log::warn!("Powershell not found, falling back to `cmd`");
229                "cmd.exe".to_string()
230            })
231    });
232
233    (*SYSTEM_SHELL).clone()
234}
235
236impl fmt::Display for ShellKind {
237    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
238        match self {
239            ShellKind::Posix => write!(f, "sh"),
240            ShellKind::Csh => write!(f, "csh"),
241            ShellKind::Tcsh => write!(f, "tcsh"),
242            ShellKind::Fish => write!(f, "fish"),
243            ShellKind::PowerShell => write!(f, "powershell"),
244            ShellKind::Pwsh => write!(f, "pwsh"),
245            ShellKind::Nushell => write!(f, "nu"),
246            ShellKind::Cmd => write!(f, "cmd"),
247            ShellKind::Rc => write!(f, "rc"),
248            ShellKind::Xonsh => write!(f, "xonsh"),
249            ShellKind::Elvish => write!(f, "elvish"),
250        }
251    }
252}
253
254impl ShellKind {
255    pub fn system() -> Self {
256        Self::new(&get_system_shell(), cfg!(windows))
257    }
258
259    pub fn new(program: impl AsRef<Path>, is_windows: bool) -> Self {
260        let program = program.as_ref();
261        let program = program
262            .file_stem()
263            .unwrap_or_else(|| program.as_os_str())
264            .to_string_lossy();
265
266        match &*program {
267            "powershell" => ShellKind::PowerShell,
268            "pwsh" => ShellKind::Pwsh,
269            "cmd" => ShellKind::Cmd,
270            "nu" => ShellKind::Nushell,
271            "fish" => ShellKind::Fish,
272            "csh" => ShellKind::Csh,
273            "tcsh" => ShellKind::Tcsh,
274            "rc" => ShellKind::Rc,
275            "xonsh" => ShellKind::Xonsh,
276            "elvish" => ShellKind::Elvish,
277            "sh" | "bash" | "zsh" => ShellKind::Posix,
278            _ if is_windows => ShellKind::PowerShell,
279            // Some other shell detected, the user might install and use a
280            // unix-like shell.
281            _ => ShellKind::Posix,
282        }
283    }
284
285    pub fn to_shell_variable(self, input: &str) -> String {
286        match self {
287            Self::PowerShell | Self::Pwsh => Self::to_powershell_variable(input),
288            Self::Cmd => Self::to_cmd_variable(input),
289            Self::Posix => input.to_owned(),
290            Self::Fish => input.to_owned(),
291            Self::Csh => input.to_owned(),
292            Self::Tcsh => input.to_owned(),
293            Self::Rc => input.to_owned(),
294            Self::Nushell => Self::to_nushell_variable(input),
295            Self::Xonsh => input.to_owned(),
296            Self::Elvish => input.to_owned(),
297        }
298    }
299
300    fn to_cmd_variable(input: &str) -> String {
301        if let Some(var_str) = input.strip_prefix("${") {
302            if var_str.find(':').is_none() {
303                // If the input starts with "${", remove the trailing "}"
304                format!("%{}%", &var_str[..var_str.len() - 1])
305            } else {
306                // `${SOME_VAR:-SOME_DEFAULT}`, we currently do not handle this situation,
307                // which will result in the task failing to run in such cases.
308                input.into()
309            }
310        } else if let Some(var_str) = input.strip_prefix('$') {
311            // If the input starts with "$", directly append to "$env:"
312            format!("%{}%", var_str)
313        } else {
314            // If no prefix is found, return the input as is
315            input.into()
316        }
317    }
318
319    fn to_powershell_variable(input: &str) -> String {
320        if let Some(var_str) = input.strip_prefix("${") {
321            if var_str.find(':').is_none() {
322                // If the input starts with "${", remove the trailing "}"
323                format!("$env:{}", &var_str[..var_str.len() - 1])
324            } else {
325                // `${SOME_VAR:-SOME_DEFAULT}`, we currently do not handle this situation,
326                // which will result in the task failing to run in such cases.
327                input.into()
328            }
329        } else if let Some(var_str) = input.strip_prefix('$') {
330            // If the input starts with "$", directly append to "$env:"
331            format!("$env:{}", var_str)
332        } else {
333            // If no prefix is found, return the input as is
334            input.into()
335        }
336    }
337
338    fn to_nushell_variable(input: &str) -> String {
339        let mut result = String::new();
340        let mut source = input;
341        let mut is_start = true;
342
343        loop {
344            match source.chars().next() {
345                None => return result,
346                Some('$') => {
347                    source = Self::parse_nushell_var(&source[1..], &mut result, is_start);
348                    is_start = false;
349                }
350                Some(_) => {
351                    is_start = false;
352                    let chunk_end = source.find('$').unwrap_or(source.len());
353                    let (chunk, rest) = source.split_at(chunk_end);
354                    result.push_str(chunk);
355                    source = rest;
356                }
357            }
358        }
359    }
360
361    fn parse_nushell_var<'a>(source: &'a str, text: &mut String, is_start: bool) -> &'a str {
362        if source.starts_with("env.") {
363            text.push('$');
364            return source;
365        }
366
367        match source.chars().next() {
368            Some('{') => {
369                let source = &source[1..];
370                if let Some(end) = source.find('}') {
371                    let var_name = &source[..end];
372                    if !var_name.is_empty() {
373                        if !is_start {
374                            text.push_str("(");
375                        }
376                        text.push_str("$env.");
377                        text.push_str(var_name);
378                        if !is_start {
379                            text.push_str(")");
380                        }
381                        &source[end + 1..]
382                    } else {
383                        text.push_str("${}");
384                        &source[end + 1..]
385                    }
386                } else {
387                    text.push_str("${");
388                    source
389                }
390            }
391            Some(c) if c.is_alphabetic() || c == '_' => {
392                let end = source
393                    .find(|c: char| !c.is_alphanumeric() && c != '_')
394                    .unwrap_or(source.len());
395                let var_name = &source[..end];
396                if !is_start {
397                    text.push_str("(");
398                }
399                text.push_str("$env.");
400                text.push_str(var_name);
401                if !is_start {
402                    text.push_str(")");
403                }
404                &source[end..]
405            }
406            _ => {
407                text.push('$');
408                source
409            }
410        }
411    }
412
413    pub fn args_for_shell(&self, interactive: bool, combined_command: String) -> Vec<String> {
414        match self {
415            ShellKind::PowerShell | ShellKind::Pwsh => vec!["-C".to_owned(), combined_command],
416            ShellKind::Cmd => vec![
417                "/S".to_owned(),
418                "/C".to_owned(),
419                format!("\"{combined_command}\""),
420            ],
421            ShellKind::Posix
422            | ShellKind::Nushell
423            | ShellKind::Fish
424            | ShellKind::Csh
425            | ShellKind::Tcsh
426            | ShellKind::Rc
427            | ShellKind::Xonsh
428            | ShellKind::Elvish => interactive
429                .then(|| "-i".to_owned())
430                .into_iter()
431                .chain(["-c".to_owned(), combined_command])
432                .collect(),
433        }
434    }
435
436    pub const fn command_prefix(&self) -> Option<char> {
437        match self {
438            ShellKind::PowerShell | ShellKind::Pwsh => Some('&'),
439            ShellKind::Nushell => Some('^'),
440            ShellKind::Posix
441            | ShellKind::Csh
442            | ShellKind::Tcsh
443            | ShellKind::Rc
444            | ShellKind::Fish
445            | ShellKind::Cmd
446            | ShellKind::Xonsh
447            | ShellKind::Elvish => None,
448        }
449    }
450
451    pub fn prepend_command_prefix<'a>(&self, command: &'a str) -> Cow<'a, str> {
452        match self.command_prefix() {
453            Some(prefix) if !command.starts_with(prefix) => {
454                Cow::Owned(format!("{prefix}{command}"))
455            }
456            _ => Cow::Borrowed(command),
457        }
458    }
459
460    pub const fn sequential_commands_separator(&self) -> char {
461        match self {
462            ShellKind::Cmd => '&',
463            ShellKind::Posix
464            | ShellKind::Csh
465            | ShellKind::Tcsh
466            | ShellKind::Rc
467            | ShellKind::Fish
468            | ShellKind::PowerShell
469            | ShellKind::Pwsh
470            | ShellKind::Nushell
471            | ShellKind::Xonsh
472            | ShellKind::Elvish => ';',
473        }
474    }
475
476    pub const fn sequential_and_commands_separator(&self) -> &'static str {
477        match self {
478            ShellKind::Cmd
479            | ShellKind::Posix
480            | ShellKind::Csh
481            | ShellKind::Tcsh
482            | ShellKind::Rc
483            | ShellKind::Fish
484            | ShellKind::Pwsh
485            | ShellKind::Xonsh => "&&",
486            ShellKind::PowerShell | ShellKind::Nushell | ShellKind::Elvish => ";",
487        }
488    }
489
490    pub fn try_quote<'a>(&self, arg: &'a str) -> Option<Cow<'a, str>> {
491        match self {
492            ShellKind::PowerShell => Some(Self::quote_powershell(arg)),
493            ShellKind::Pwsh => Some(Self::quote_pwsh(arg)),
494            ShellKind::Cmd => Some(Self::quote_cmd(arg)),
495            ShellKind::Posix
496            | ShellKind::Csh
497            | ShellKind::Tcsh
498            | ShellKind::Rc
499            | ShellKind::Fish
500            | ShellKind::Nushell
501            | ShellKind::Xonsh
502            | ShellKind::Elvish => shlex::try_quote(arg).ok(),
503        }
504    }
505
506    fn quote_windows(arg: &str, enclose: bool) -> Cow<'_, str> {
507        if arg.is_empty() {
508            return Cow::Borrowed("\"\"");
509        }
510
511        let needs_quoting = arg.chars().any(|c| c == ' ' || c == '\t' || c == '"');
512        if !needs_quoting {
513            return Cow::Borrowed(arg);
514        }
515
516        let mut result = String::with_capacity(arg.len() + 2);
517
518        if enclose {
519            result.push('"');
520        }
521
522        let chars: Vec<char> = arg.chars().collect();
523        let mut i = 0;
524
525        while i < chars.len() {
526            if chars[i] == '\\' {
527                let mut num_backslashes = 0;
528                while i < chars.len() && chars[i] == '\\' {
529                    num_backslashes += 1;
530                    i += 1;
531                }
532
533                if i < chars.len() && chars[i] == '"' {
534                    // Backslashes followed by quote: double the backslashes and escape the quote
535                    for _ in 0..(num_backslashes * 2 + 1) {
536                        result.push('\\');
537                    }
538                    result.push('"');
539                    i += 1;
540                } else if i >= chars.len() {
541                    // Trailing backslashes: double them (they precede the closing quote)
542                    for _ in 0..(num_backslashes * 2) {
543                        result.push('\\');
544                    }
545                } else {
546                    // Backslashes not followed by quote: output as-is
547                    for _ in 0..num_backslashes {
548                        result.push('\\');
549                    }
550                }
551            } else if chars[i] == '"' {
552                // Quote not preceded by backslash: escape it
553                result.push('\\');
554                result.push('"');
555                i += 1;
556            } else {
557                result.push(chars[i]);
558                i += 1;
559            }
560        }
561
562        if enclose {
563            result.push('"');
564        }
565        Cow::Owned(result)
566    }
567
568    fn needs_quoting_powershell(s: &str) -> bool {
569        s.is_empty()
570            || s.chars().any(|c| {
571                c.is_whitespace()
572                    || matches!(
573                        c,
574                        '"' | '`'
575                            | '$'
576                            | '&'
577                            | '|'
578                            | '<'
579                            | '>'
580                            | ';'
581                            | '('
582                            | ')'
583                            | '['
584                            | ']'
585                            | '{'
586                            | '}'
587                            | ','
588                            | '\''
589                            | '@'
590                    )
591            })
592    }
593
594    fn need_quotes_powershell(arg: &str) -> bool {
595        let mut quote_count = 0;
596        for c in arg.chars() {
597            if c == '"' {
598                quote_count += 1;
599            } else if c.is_whitespace() && (quote_count % 2 == 0) {
600                return true;
601            }
602        }
603        false
604    }
605
606    fn escape_powershell_quotes(s: &str) -> String {
607        let mut result = String::with_capacity(s.len() + 4);
608        result.push('\'');
609        for c in s.chars() {
610            if c == '\'' {
611                result.push('\'');
612            }
613            result.push(c);
614        }
615        result.push('\'');
616        result
617    }
618
619    pub fn quote_powershell(arg: &str) -> Cow<'_, str> {
620        let ps_will_quote = Self::need_quotes_powershell(arg);
621        let crt_quoted = Self::quote_windows(arg, !ps_will_quote);
622
623        if !Self::needs_quoting_powershell(arg) {
624            return crt_quoted;
625        }
626
627        Cow::Owned(Self::escape_powershell_quotes(&crt_quoted))
628    }
629
630    pub fn quote_pwsh(arg: &str) -> Cow<'_, str> {
631        if arg.is_empty() {
632            return Cow::Borrowed("''");
633        }
634
635        if !Self::needs_quoting_powershell(arg) {
636            return Cow::Borrowed(arg);
637        }
638
639        Cow::Owned(Self::escape_powershell_quotes(arg))
640    }
641
642    pub fn quote_cmd(arg: &str) -> Cow<'_, str> {
643        let crt_quoted = Self::quote_windows(arg, true);
644
645        let needs_cmd_escaping = crt_quoted.contains(['"', '%', '^', '<', '>', '&', '|', '(', ')']);
646
647        if !needs_cmd_escaping {
648            return crt_quoted;
649        }
650
651        let mut result = String::with_capacity(crt_quoted.len() * 2);
652        for c in crt_quoted.chars() {
653            match c {
654                '^' | '"' | '<' | '>' | '&' | '|' | '(' | ')' => {
655                    result.push('^');
656                    result.push(c);
657                }
658                '%' => {
659                    result.push_str("%%cd:~,%");
660                }
661                _ => result.push(c),
662            }
663        }
664        Cow::Owned(result)
665    }
666
667    /// Quotes the given argument if necessary, taking into account the command prefix.
668    ///
669    /// In other words, this will consider quoting arg without its command prefix to not break the command.
670    /// You should use this over `try_quote` when you want to quote a shell command.
671    pub fn try_quote_prefix_aware<'a>(&self, arg: &'a str) -> Option<Cow<'a, str>> {
672        if let Some(char) = self.command_prefix() {
673            if let Some(arg) = arg.strip_prefix(char) {
674                // we have a command that is prefixed
675                for quote in ['\'', '"'] {
676                    if let Some(arg) = arg
677                        .strip_prefix(quote)
678                        .and_then(|arg| arg.strip_suffix(quote))
679                    {
680                        // and the command itself is wrapped as a literal, that
681                        // means the prefix exists to interpret a literal as a
682                        // command. So strip the quotes, quote the command, and
683                        // re-add the quotes if they are missing after requoting
684                        let quoted = self.try_quote(arg)?;
685                        return Some(if quoted.starts_with(['\'', '"']) {
686                            Cow::Owned(self.prepend_command_prefix(&quoted).into_owned())
687                        } else {
688                            Cow::Owned(
689                                self.prepend_command_prefix(&format!("{quote}{quoted}{quote}"))
690                                    .into_owned(),
691                            )
692                        });
693                    }
694                }
695                return self
696                    .try_quote(arg)
697                    .map(|quoted| Cow::Owned(self.prepend_command_prefix(&quoted).into_owned()));
698            }
699        }
700        self.try_quote(arg).map(|quoted| match quoted {
701            unquoted @ Cow::Borrowed(_) => unquoted,
702            Cow::Owned(quoted) => Cow::Owned(self.prepend_command_prefix(&quoted).into_owned()),
703        })
704    }
705
706    pub fn split(&self, input: &str) -> Option<Vec<String>> {
707        shlex::split(input)
708    }
709
710    pub const fn activate_keyword(&self) -> &'static str {
711        match self {
712            ShellKind::Cmd => "",
713            ShellKind::Nushell => "overlay use",
714            ShellKind::PowerShell | ShellKind::Pwsh => ".",
715            ShellKind::Fish
716            | ShellKind::Csh
717            | ShellKind::Tcsh
718            | ShellKind::Posix
719            | ShellKind::Rc
720            | ShellKind::Xonsh
721            | ShellKind::Elvish => "source",
722        }
723    }
724
725    pub const fn clear_screen_command(&self) -> &'static str {
726        match self {
727            ShellKind::Cmd => "cls",
728            ShellKind::Posix
729            | ShellKind::Csh
730            | ShellKind::Tcsh
731            | ShellKind::Rc
732            | ShellKind::Fish
733            | ShellKind::PowerShell
734            | ShellKind::Pwsh
735            | ShellKind::Nushell
736            | ShellKind::Xonsh
737            | ShellKind::Elvish => "clear",
738        }
739    }
740
741    #[cfg(windows)]
742    /// We do not want to escape arguments if we are using CMD as our shell.
743    /// If we do we end up with too many quotes/escaped quotes for CMD to handle.
744    pub const fn tty_escape_args(&self) -> bool {
745        match self {
746            ShellKind::Cmd => false,
747            ShellKind::Posix
748            | ShellKind::Csh
749            | ShellKind::Tcsh
750            | ShellKind::Rc
751            | ShellKind::Fish
752            | ShellKind::PowerShell
753            | ShellKind::Pwsh
754            | ShellKind::Nushell
755            | ShellKind::Xonsh
756            | ShellKind::Elvish => true,
757        }
758    }
759}
760
761#[cfg(test)]
762mod tests {
763    use super::*;
764
765    // Examples
766    // WSL
767    // wsl.exe --distribution NixOS --cd /home/user -- /usr/bin/zsh -c "echo hello"
768    // wsl.exe --distribution NixOS --cd /home/user -- /usr/bin/zsh -c "\"echo hello\"" | grep hello"
769    // wsl.exe --distribution NixOS --cd ~ env RUST_LOG=info,remote=debug .zed_wsl_server/zed-remote-server-dev-build proxy --identifier dev-workspace-53
770    // PowerShell from Nushell
771    // nu -c overlay use "C:\Users\kubko\dev\python\39007\tests\.venv\Scripts\activate.nu"; ^"C:\Program Files\PowerShell\7\pwsh.exe" -C "C:\Users\kubko\dev\python\39007\tests\.venv\Scripts\python.exe -m pytest \"test_foo.py::test_foo\""
772    // PowerShell from CMD
773    // cmd /C \" \"C:\\\\Users\\\\kubko\\\\dev\\\\python\\\\39007\\\\tests\\\\.venv\\\\Scripts\\\\activate.bat\"& \"C:\\\\Program Files\\\\PowerShell\\\\7\\\\pwsh.exe\" -C \"C:\\\\Users\\\\kubko\\\\dev\\\\python\\\\39007\\\\tests\\\\.venv\\\\Scripts\\\\python.exe -m pytest \\\"test_foo.py::test_foo\\\"\"\"
774
775    #[test]
776    fn test_try_quote_powershell() {
777        let shell_kind = ShellKind::PowerShell;
778        assert_eq!(
779            shell_kind
780                .try_quote("C:\\Users\\johndoe\\dev\\python\\39007\\tests\\.venv\\Scripts\\python.exe -m pytest \"test_foo.py::test_foo\"")
781                .unwrap()
782                .into_owned(),
783            "'C:\\Users\\johndoe\\dev\\python\\39007\\tests\\.venv\\Scripts\\python.exe -m pytest \\\"test_foo.py::test_foo\\\"'".to_string()
784        );
785    }
786
787    #[test]
788    fn test_try_quote_cmd() {
789        let shell_kind = ShellKind::Cmd;
790        assert_eq!(
791            shell_kind
792                .try_quote("C:\\Users\\johndoe\\dev\\python\\39007\\tests\\.venv\\Scripts\\python.exe -m pytest \"test_foo.py::test_foo\"")
793                .unwrap()
794                .into_owned(),
795            "^\"C:\\Users\\johndoe\\dev\\python\\39007\\tests\\.venv\\Scripts\\python.exe -m pytest \\^\"test_foo.py::test_foo\\^\"^\"".to_string()
796        );
797    }
798
799    #[test]
800    fn test_try_quote_powershell_edge_cases() {
801        let shell_kind = ShellKind::PowerShell;
802
803        // Empty string
804        assert_eq!(
805            shell_kind.try_quote("").unwrap().into_owned(),
806            "'\"\"'".to_string()
807        );
808
809        // String without special characters (no quoting needed)
810        assert_eq!(shell_kind.try_quote("simple").unwrap(), "simple");
811
812        // String with spaces
813        assert_eq!(
814            shell_kind.try_quote("hello world").unwrap().into_owned(),
815            "'hello world'".to_string()
816        );
817
818        // String with dollar signs
819        assert_eq!(
820            shell_kind.try_quote("$variable").unwrap().into_owned(),
821            "'$variable'".to_string()
822        );
823
824        // String with backticks
825        assert_eq!(
826            shell_kind.try_quote("test`command").unwrap().into_owned(),
827            "'test`command'".to_string()
828        );
829
830        // String with multiple special characters
831        assert_eq!(
832            shell_kind
833                .try_quote("test `\"$var`\" end")
834                .unwrap()
835                .into_owned(),
836            "'test `\\\"$var`\\\" end'".to_string()
837        );
838
839        // String with backslashes and colon (path without spaces doesn't need quoting)
840        assert_eq!(
841            shell_kind.try_quote("C:\\path\\to\\file").unwrap(),
842            "C:\\path\\to\\file"
843        );
844    }
845
846    #[test]
847    fn test_try_quote_cmd_edge_cases() {
848        let shell_kind = ShellKind::Cmd;
849
850        // Empty string
851        assert_eq!(
852            shell_kind.try_quote("").unwrap().into_owned(),
853            "^\"^\"".to_string()
854        );
855
856        // String without special characters (no quoting needed)
857        assert_eq!(shell_kind.try_quote("simple").unwrap(), "simple");
858
859        // String with spaces
860        assert_eq!(
861            shell_kind.try_quote("hello world").unwrap().into_owned(),
862            "^\"hello world^\"".to_string()
863        );
864
865        // String with space and backslash (backslash not at end, so not doubled)
866        assert_eq!(
867            shell_kind.try_quote("path\\ test").unwrap().into_owned(),
868            "^\"path\\ test^\"".to_string()
869        );
870
871        // String ending with backslash (must be doubled before closing quote)
872        assert_eq!(
873            shell_kind.try_quote("test path\\").unwrap().into_owned(),
874            "^\"test path\\\\^\"".to_string()
875        );
876
877        // String ending with multiple backslashes (all doubled before closing quote)
878        assert_eq!(
879            shell_kind.try_quote("test path\\\\").unwrap().into_owned(),
880            "^\"test path\\\\\\\\^\"".to_string()
881        );
882
883        // String with embedded quote (quote is escaped, backslash before it is doubled)
884        assert_eq!(
885            shell_kind.try_quote("test\\\"quote").unwrap().into_owned(),
886            "^\"test\\\\\\^\"quote^\"".to_string()
887        );
888
889        // String with multiple backslashes before embedded quote (all doubled)
890        assert_eq!(
891            shell_kind
892                .try_quote("test\\\\\"quote")
893                .unwrap()
894                .into_owned(),
895            "^\"test\\\\\\\\\\^\"quote^\"".to_string()
896        );
897
898        // String with backslashes not before quotes (path without spaces doesn't need quoting)
899        assert_eq!(
900            shell_kind.try_quote("C:\\path\\to\\file").unwrap(),
901            "C:\\path\\to\\file"
902        );
903    }
904
905    #[test]
906    fn test_try_quote_nu_command() {
907        let shell_kind = ShellKind::Nushell;
908        assert_eq!(
909            shell_kind.try_quote("'uname'").unwrap().into_owned(),
910            "\"'uname'\"".to_string()
911        );
912        assert_eq!(
913            shell_kind
914                .try_quote_prefix_aware("'uname'")
915                .unwrap()
916                .into_owned(),
917            "^\"'uname'\"".to_string()
918        );
919        assert_eq!(
920            shell_kind.try_quote("^uname").unwrap().into_owned(),
921            "'^uname'".to_string()
922        );
923        assert_eq!(
924            shell_kind
925                .try_quote_prefix_aware("^uname")
926                .unwrap()
927                .into_owned(),
928            "^uname".to_string()
929        );
930        assert_eq!(
931            shell_kind.try_quote("^'uname'").unwrap().into_owned(),
932            "'^'\"'uname\'\"".to_string()
933        );
934        assert_eq!(
935            shell_kind
936                .try_quote_prefix_aware("^'uname'")
937                .unwrap()
938                .into_owned(),
939            "^'uname'".to_string()
940        );
941        assert_eq!(
942            shell_kind.try_quote("'uname a'").unwrap().into_owned(),
943            "\"'uname a'\"".to_string()
944        );
945        assert_eq!(
946            shell_kind
947                .try_quote_prefix_aware("'uname a'")
948                .unwrap()
949                .into_owned(),
950            "^\"'uname a'\"".to_string()
951        );
952        assert_eq!(
953            shell_kind.try_quote("^'uname a'").unwrap().into_owned(),
954            "'^'\"'uname a'\"".to_string()
955        );
956        assert_eq!(
957            shell_kind
958                .try_quote_prefix_aware("^'uname a'")
959                .unwrap()
960                .into_owned(),
961            "^'uname a'".to_string()
962        );
963        assert_eq!(
964            shell_kind.try_quote("uname").unwrap().into_owned(),
965            "uname".to_string()
966        );
967        assert_eq!(
968            shell_kind
969                .try_quote_prefix_aware("uname")
970                .unwrap()
971                .into_owned(),
972            "uname".to_string()
973        );
974    }
975}