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

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]}}"
if ((major > 4)) || {{ ((major == 4)) && ((minor >= 1)); }}; then
source <("{0}" init bash --print-full-init) source <("{0}" init bash --print-full-init)
else else
source /dev/stdin <<<"$("{0}" init bash --print-full-init)" source /dev/stdin <<<"$("{0}" init bash --print-full-init)"
fi"#, fi
}}
__main
unset -f __main
"#,
starship.sprint_posix()? starship.sprint_posix()?
) ),
}; "zsh" => print!(
r#"source <("{}" init zsh --print-full-init)"#,
Some(script)
}
"zsh" => {
let script = format!(
"source <(\"{}\" init zsh --print-full-init)",
starship.sprint_posix()? starship.sprint_posix()?
); ),
Some(script) "fish" => print!(
}
"fish" => {
// 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()?
); ),
Some(script) "powershell" => print!(
} r#"Invoke-Expression (& "{}" init powershell --print-full-init | Out-String)"#,
"powershell" => {
// Explanation of syntax:
// &: Explicitly tells powershell to execute path with starship executable.
//
// @: multi-line stdout is returned as an array, but a single line or no lines
// are returned as-is. @ ensures it's always an array.
//
// -join "`n": Joins the stdout array together as a string with newlines.
// Powershell escapes with ` instead of \ thus `n translates to a newline.
let script = format!(
"Invoke-Expression (@(&\"{}\" init powershell --print-full-init) -join \"`n\")",
starship.sprint()? starship.sprint()?
); ),
Some(script) "ion" => print!("eval $({} init ion --print-full-init)", starship.sprint()?),
} "elvish" => print!(
"ion" => { r#"eval ("{}" init elvish --print-full-init | slurp)"#,
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()? starship.sprint_posix()?
); ),
Some(script) "tcsh" => print!(
} r#"eval `("{}" init tcsh --print-full-init)`"#,
"tcsh" => {
let script = format!(
r#"eval "`("{}" init tcsh --print-full-init)`""#,
starship.sprint_posix()? 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(())
} }