style(init): Cleanup the unknown shell message (#2444)

* style(init): Cleanup the unknown shell message

Have make a small change to the message that is printed when an unknow
shell is used. This correct the placement of the trailing `"` so that
the two training new lines are correctly printed and updates the list of
supported shells.

* refactor(init): consolidate unknown shell errors

Have consolidated the two unknown shell errors

* refactor(init): Quote the shell name in the output

Quote the shell name in the script and combined the shell_name and
shell_basename to simplify the code a little.
This commit is contained in:
Thomas O'Donnell 2021-03-25 21:08:28 +01:00 committed by GitHub
parent ba40ad5ce6
commit e0da57df3f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 25 additions and 23 deletions

View File

@ -81,12 +81,15 @@ init code. The stub produces the main init script, then evaluates it with
pub fn init_stub(shell_name: &str) -> io::Result<()> { pub fn init_stub(shell_name: &str) -> io::Result<()> {
log::debug!("Shell name: {}", shell_name); log::debug!("Shell name: {}", shell_name);
let shell_basename = Path::new(shell_name).file_stem().and_then(OsStr::to_str); let shell_basename = Path::new(shell_name)
.file_stem()
.and_then(OsStr::to_str)
.unwrap_or(shell_name);
let starship = StarshipPath::init()?; let starship = StarshipPath::init()?;
let setup_stub = match shell_basename { let setup_stub = match shell_basename {
Some("bash") => { "bash" => {
/* /*
* The standard bash bootstrap is: * The standard bash bootstrap is:
* `source <(starship init bash --print-full-init)` * `source <(starship init bash --print-full-init)`
@ -132,14 +135,14 @@ fi"#,
Some(script) Some(script)
} }
Some("zsh") => { "zsh" => {
let script = format!( let script = format!(
"source <(\"{}\" init zsh --print-full-init)", "source <(\"{}\" init zsh --print-full-init)",
starship.sprint_posix()? starship.sprint_posix()?
); );
Some(script) Some(script)
} }
Some("fish") => { "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!( let script = format!(
"source (\"{}\" init fish --print-full-init | psub)", "source (\"{}\" init fish --print-full-init | psub)",
@ -147,7 +150,7 @@ fi"#,
); );
Some(script) Some(script)
} }
Some("powershell") => { "powershell" => {
// Explanation of syntax: // Explanation of syntax:
// &: Explicitly tells powershell to execute path with starship executable. // &: Explicitly tells powershell to execute path with starship executable.
// //
@ -162,41 +165,40 @@ fi"#,
); );
Some(script) Some(script)
} }
Some("ion") => { "ion" => {
let script = format!("eval $({} init ion --print-full-init)", starship.sprint()?); let script = format!("eval $({} init ion --print-full-init)", starship.sprint()?);
Some(script) Some(script)
} }
Some("elvish") => { "elvish" => {
let script = format!( let script = format!(
"eval (\"{}\" init elvish --print-full-init | slurp)", "eval (\"{}\" init elvish --print-full-init | slurp)",
starship.sprint_posix()? starship.sprint_posix()?
); );
Some(script) Some(script)
} }
Some("tcsh") => { "tcsh" => {
let script = format!( let script = format!(
r#"eval "`("{}" init tcsh --print-full-init)`""#, r#"eval "`("{}" init tcsh --print-full-init)`""#,
starship.sprint_posix()? starship.sprint_posix()?
); );
Some(script) Some(script)
} }
None => { _ => {
let quoted_arg = shell_words::quote(shell_basename);
println!( println!(
"Invalid shell name provided: {}\\n\ "printf \"\\n%s is not yet supported by starship.\\n\
If this issue persists, please open an \ For the time being, we support the following shells:\\n\
issue in the starship repo: \\n\ * bash\\n\
https://github.com/starship/starship/issues/new\\n\"", * elvish\\n\
shell_name * fish\\n\
); * ion\\n\
None * powershell\\n\
} * tcsh\\n\
Some(shell_basename) => { * zsh\\n\
println!( \\n\
"printf \"\\n{0} is not yet supported by starship.\\n\
For the time being, we support bash, zsh, fish, and ion.\\n\
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 {0}:\\nhttps://github.com/starship/starship/issues/new\"\\n\\n", see support for %s:\\nhttps://github.com/starship/starship/issues/new\\n\\n\" {0} {0}",
shell_basename quoted_arg
); );
None None
} }