diff options
author | Kevin Song <chipbuster@users.noreply.github.com> | 2019-07-20 10:45:41 -0700 |
---|---|---|
committer | Matan Kushner <hello@matchai.me> | 2019-07-20 13:45:41 -0400 |
commit | aceda0052f7e946d9c43afad5ae440af3e6cd43e (patch) | |
tree | 595ef49ea617ab90c41b90d226cd9b98c8aba7f3 | |
parent | 0f76d46d95a5aaba3700a8341e3811657c41549a (diff) |
fix: handle path to shell in starship init (#106)
Add support for paths to init function
This adds support for qualified paths (e.g. using
`/usr/local/bin/zsh` instead of `zsh`) to init.rs.
The function now converts the shell name into an
OsStr, then to a Path, then gets the file stem,
and unwraps back into a str. While this process can
fail (yielding a None), it's highly unlikely to unless
the user has messed with their shells or there's an
issue in Starship--therefore, the failure message in
this case simply asks the user to file a bug report.
-rw-r--r-- | src/init.rs | 28 |
1 files changed, 23 insertions, 5 deletions
diff --git a/src/init.rs b/src/init.rs index 5dc854d9a..4a237ac88 100644 --- a/src/init.rs +++ b/src/init.rs @@ -1,25 +1,43 @@ +use std::ffi::OsStr; +use std::path::Path; + pub fn init(shell_name: &str) { log::debug!("Shell name: {}", shell_name); - let setup_script = match shell_name { - "bash" => { + + let shell_basename = Path::new(shell_name).file_stem().and_then(OsStr::to_str); + + let setup_script = match shell_basename { + Some("bash") => { let script = "PS1=\"$(starship prompt --status=$?)\""; Some(script) } - "zsh" => { + Some("zsh") => { let script = "PROMPT=\"$(starship prompt --status=$?)\""; Some(script) } - "fish" => { + Some("fish") => { let script = "function fish_prompt; starship prompt --status=$status; end"; Some(script) } + None => { + println!( + "Invalid shell name provided: {}\\n\ + If this issue persists, please open an \ + issue in the starship repo: \\n\ + https://github.com/starship/starship/issues/new\\n\"", + shell_name + ); + None + } _ => { + /* Calling unwrap() here is fine because the None case will have + already matched on the previous arm */ println!( "printf \"\\n{0} is not yet supported by starship.\\n\ For the time being, we support bash, zsh, and fish.\\n\ 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", - shell_name + shell_basename.unwrap() ); None } |