refactor(init): cleanup init stub (#2548)

* refactor(init): cleanup init stub

* refactor(init): use iex PowerShell alias

* fix(init): prevent bash scope pollution

* refactor(init): update PowerShell snippet
This commit is contained in:
Dario Vladović 2021-04-05 16:52:10 +02:00 committed by GitHub
parent 2b0010ffe3
commit c7d5ce72b5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 39 additions and 67 deletions

View File

@ -88,8 +88,8 @@ pub fn init_stub(shell_name: &str) -> io::Result<()> {
let starship = StarshipPath::init()?; let starship = StarshipPath::init()?;
let setup_stub = match shell_basename { match shell_basename {
"bash" => { "bash" => print!(
/* /*
* The standard bash bootstrap is: * The standard bash bootstrap is:
* `source <(starship init bash --print-full-init)` * `source <(starship init bash --print-full-init)`
@ -121,68 +121,44 @@ pub fn init_stub(shell_name: &str) -> io::Result<()> {
* https://github.com/starship/starship/pull/241 * https://github.com/starship/starship/pull/241
* https://github.com/starship/starship/pull/278 * https://github.com/starship/starship/pull/278
*/ */
let script = { r#"
format!( __main() {{
r#"if [ "${{BASH_VERSINFO[0]}}" -gt 4 ] || ([ "${{BASH_VERSINFO[0]}}" -eq 4 ] && [ "${{BASH_VERSINFO[1]}}" -ge 1 ]) local major="${{BASH_VERSINFO[0]}}"
then local minor="${{BASH_VERSINFO[1]}}"
source <("{0}" init bash --print-full-init)
else
source /dev/stdin <<<"$("{0}" init bash --print-full-init)"
fi"#,
starship.sprint_posix()?
)
};
Some(script) if ((major > 4)) || {{ ((major == 4)) && ((minor >= 1)); }}; then
} source <("{0}" init bash --print-full-init)
"zsh" => { else
let script = format!( source /dev/stdin <<<"$("{0}" init bash --print-full-init)"
"source <(\"{}\" init zsh --print-full-init)", fi
starship.sprint_posix()? }}
); __main
Some(script) unset -f __main
} "#,
"fish" => { starship.sprint_posix()?
),
"zsh" => print!(
r#"source <("{}" init zsh --print-full-init)"#,
starship.sprint_posix()?
),
"fish" => print!(
// Fish does process substitution with pipes and psub instead of bash syntax // Fish does process substitution with pipes and psub instead of bash syntax
let script = format!( r#"source ("{}" init fish --print-full-init | psub)"#,
"source (\"{}\" init fish --print-full-init | psub)", starship.sprint_posix()?
starship.sprint_posix()? ),
); "powershell" => print!(
Some(script) r#"Invoke-Expression (& "{}" init powershell --print-full-init | Out-String)"#,
} starship.sprint()?
"powershell" => { ),
// Explanation of syntax: "ion" => print!("eval $({} init ion --print-full-init)", starship.sprint()?),
// &: Explicitly tells powershell to execute path with starship executable. "elvish" => print!(
// r#"eval ("{}" init elvish --print-full-init | slurp)"#,
// @: multi-line stdout is returned as an array, but a single line or no lines starship.sprint_posix()?
// are returned as-is. @ ensures it's always an array. ),
// "tcsh" => print!(
// -join "`n": Joins the stdout array together as a string with newlines. r#"eval `("{}" init tcsh --print-full-init)`"#,
// Powershell escapes with ` instead of \ thus `n translates to a newline. starship.sprint_posix()?
let script = format!( ),
"Invoke-Expression (@(&\"{}\" init powershell --print-full-init) -join \"`n\")",
starship.sprint()?
);
Some(script)
}
"ion" => {
let script = format!("eval $({} init ion --print-full-init)", starship.sprint()?);
Some(script)
}
"elvish" => {
let script = format!(
"eval (\"{}\" init elvish --print-full-init | slurp)",
starship.sprint_posix()?
);
Some(script)
}
"tcsh" => {
let script = format!(
r#"eval "`("{}" init tcsh --print-full-init)`""#,
starship.sprint_posix()?
);
Some(script)
}
_ => { _ => {
let quoted_arg = shell_words::quote(shell_basename); let quoted_arg = shell_words::quote(shell_basename);
println!( println!(
@ -199,13 +175,9 @@ fi"#,
Please open an issue in the starship repo if you would like to \ Please open an issue in the starship repo if you would like to \
see support for %s:\\nhttps://github.com/starship/starship/issues/new\\n\\n\" {0} {0}", see support for %s:\\nhttps://github.com/starship/starship/issues/new\\n\\n\" {0} {0}",
quoted_arg quoted_arg
); )
None
} }
}; };
if let Some(script) = setup_stub {
print!("{}", script);
};
Ok(()) Ok(())
} }