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 setup_stub = match shell_basename {
"bash" => {
match shell_basename {
"bash" => print!(
/*
* The standard bash bootstrap is:
* `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/278
*/
let script = {
format!(
r#"if [ "${{BASH_VERSINFO[0]}}" -gt 4 ] || ([ "${{BASH_VERSINFO[0]}}" -eq 4 ] && [ "${{BASH_VERSINFO[1]}}" -ge 1 ])
then
source <("{0}" init bash --print-full-init)
else
source /dev/stdin <<<"$("{0}" init bash --print-full-init)"
fi"#,
starship.sprint_posix()?
)
};
r#"
__main() {{
local major="${{BASH_VERSINFO[0]}}"
local minor="${{BASH_VERSINFO[1]}}"
Some(script)
}
"zsh" => {
let script = format!(
"source <(\"{}\" init zsh --print-full-init)",
starship.sprint_posix()?
);
Some(script)
}
"fish" => {
if ((major > 4)) || {{ ((major == 4)) && ((minor >= 1)); }}; then
source <("{0}" init bash --print-full-init)
else
source /dev/stdin <<<"$("{0}" init bash --print-full-init)"
fi
}}
__main
unset -f __main
"#,
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
let script = format!(
"source (\"{}\" init fish --print-full-init | psub)",
starship.sprint_posix()?
);
Some(script)
}
"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()?
);
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)
}
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()?
),
"ion" => print!("eval $({} init ion --print-full-init)", starship.sprint()?),
"elvish" => print!(
r#"eval ("{}" init elvish --print-full-init | slurp)"#,
starship.sprint_posix()?
),
"tcsh" => print!(
r#"eval `("{}" init tcsh --print-full-init)`"#,
starship.sprint_posix()?
),
_ => {
let quoted_arg = shell_words::quote(shell_basename);
println!(
@ -199,13 +175,9 @@ fi"#,
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}",
quoted_arg
);
None
)
}
};
if let Some(script) = setup_stub {
print!("{}", script);
};
Ok(())
}