Skip to content

Commit

Permalink
fix(init): improve starship path escaping (starship#2848)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidkna authored Jul 3, 2021
1 parent 2d92e70 commit a4a2adb
Showing 1 changed file with 41 additions and 11 deletions.
52 changes: 41 additions & 11 deletions src/init/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,17 @@ impl StarshipPath {
.ok_or_else(|| io::Error::new(io::ErrorKind::Other, "can't convert to str"))?;
Ok(current_exe)
}

/// Returns POSIX quoted path to starship binary
fn sprint(&self) -> io::Result<String> {
self.str_path().map(|s| s.replace("\"", "\"'\"'\""))
self.str_path().map(|p| shell_words::quote(p).into_owned())
}

/// PowerShell specific path escaping
fn sprint_pwsh(&self) -> io::Result<String> {
self.str_path()
.map(|s| s.replace("'", "''"))
.map(|s| format!("'{}'", s))
}
fn sprint_posix(&self) -> io::Result<String> {
// On non-Windows platform, return directly.
Expand Down Expand Up @@ -71,7 +80,7 @@ impl StarshipPath {
str_path
}
};
Ok(posix_path.replace("\"", "\"'\"'\""))
Ok(shell_words::quote(posix_path).into_owned())
}
}

Expand Down Expand Up @@ -138,25 +147,25 @@ pub fn init_stub(shell_name: &str) -> io::Result<()> {
starship.sprint_posix()?
),
"zsh" => print!(
r#"source <("{}" init zsh --print-full-init)"#,
r#"source <({} init zsh --print-full-init)"#,
starship.sprint_posix()?
),
"fish" => print!(
// Fish does process substitution with pipes and psub instead of bash syntax
r#"source ("{}" init fish --print-full-init | psub)"#,
r#"source ({} init fish --print-full-init | psub)"#,
starship.sprint_posix()?
),
"powershell" => print!(
r#"Invoke-Expression (& "{}" init powershell --print-full-init | Out-String)"#,
starship.sprint()?
r#"Invoke-Expression (& {} init powershell --print-full-init | Out-String)"#,
starship.sprint_pwsh()?
),
"ion" => print!("eval $({} init ion --print-full-init)", starship.sprint()?),
"elvish" => print!(
r#"eval ("{}" init elvish --print-full-init | slurp)"#,
r#"eval ({} init elvish --print-full-init | slurp)"#,
starship.sprint_posix()?
),
"tcsh" => print!(
r#"eval `("{}" init tcsh --print-full-init)`"#,
r#"eval `({} init tcsh --print-full-init)`"#,
starship.sprint_posix()?
),
_ => {
Expand Down Expand Up @@ -190,7 +199,7 @@ pub fn init_main(shell_name: &str) -> io::Result<()> {
"bash" => print_script(BASH_INIT, &starship_path.sprint_posix()?),
"zsh" => print_script(ZSH_INIT, &starship_path.sprint_posix()?),
"fish" => print_script(FISH_INIT, &starship_path.sprint_posix()?),
"powershell" => print_script(PWSH_INIT, &starship_path.sprint()?),
"powershell" => print_script(PWSH_INIT, &starship_path.sprint_pwsh()?),
"ion" => print_script(ION_INIT, &starship_path.sprint()?),
"elvish" => print_script(ELVISH_INIT, &starship_path.sprint_posix()?),
"tcsh" => print_script(TCSH_INIT, &starship_path.sprint_posix()?),
Expand All @@ -206,8 +215,7 @@ pub fn init_main(shell_name: &str) -> io::Result<()> {
}

fn print_script(script: &str, path: &str) {
let starship_path_string = format!("\"{}\"", path);
let script = script.replace("::STARSHIP::", &starship_path_string);
let script = script.replace("::STARSHIP::", path);
print!("{}", script);
}

Expand Down Expand Up @@ -239,3 +247,25 @@ const ION_INIT: &str = include_str!("starship.ion");
const ELVISH_INIT: &str = include_str!("starship.elv");

const TCSH_INIT: &str = include_str!("starship.tcsh");

#[cfg(test)]
mod tests {
use super::*;
#[test]
fn escape_pwsh() -> io::Result<()> {
let starship_path = StarshipPath {
native_path: PathBuf::from(r"C:\starship.exe"),
};
assert_eq!(starship_path.sprint_pwsh()?, r"'C:\starship.exe'");
Ok(())
}

#[test]
fn escape_tick_pwsh() -> io::Result<()> {
let starship_path = StarshipPath {
native_path: PathBuf::from(r"C:\'starship.exe"),
};
assert_eq!(starship_path.sprint_pwsh()?, r"'C:\''starship.exe'");
Ok(())
}
}

0 comments on commit a4a2adb

Please sign in to comment.