use shell_quote::{QuoteInto, QuoteRefExt};
pub enum ShellType {
Bash,
Dash,
Fish,
Sh,
Zsh,
Cmd,
PowerShell,
#[allow(unused)]
Other(String),
}
impl ShellType {
pub fn quote(&self, s: &str) -> String {
match self {
ShellType::Bash | ShellType::Zsh => s.quoted(shell_quote::Bash),
ShellType::Fish => s.quoted(shell_quote::Fish),
ShellType::Cmd => {
let escaped = s.replace('%', "%%").replace('"', "\"\"");
format!("\"{}\"", escaped)
}
ShellType::PowerShell => {
let escaped = s.replace('\'', "''");
format!("'{}'", escaped)
}
ShellType::Dash | ShellType::Sh | ShellType::Other(_) => {
let mut o = vec![];
shell_quote::Sh::quote_into(s, &mut o);
String::from_utf8(o).unwrap_or_default()
}
}
}
}