Skip to content

Commit

Permalink
feat(starkliup): support Fish shell
Browse files Browse the repository at this point in the history
Since Fish is not POSIX-compliant, neither the `install` script nor the
`starkliup` command itself actually supports being run by Fish. The
"support" here means that `install` and `starkliup` can now properly set
up correct env file and shell completion for Fish shell users.
  • Loading branch information
xJonathanLEI committed Jul 30, 2024
1 parent 56fcee1 commit bb2bc2c
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
1 change: 1 addition & 0 deletions book/src/shell-completions.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Automatic shell completions are critical to good user experience for CLI tools.
When [installing with `starkliup`](./installation.md#using-starkliup), shell completions are automatically set up for the following shells:

- _bash_
- _fish_
- _zsh_

and you don't need to configure them yourself.
Expand Down
36 changes: 33 additions & 3 deletions starkliup/install
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,26 @@ STARKLI_BIN_DIR="$STARKLI_DIR/bin"
STARKLI_MAN_DIR="$STARKLI_DIR/share/man/man1"

STARKLI_BASH_COMPLETIONS_DIR="$STARKLI_DIR/share/bash-completions"
STARKLI_FISH_COMPLETIONS_DIR="$STARKLI_DIR/share/fish-completions"
STARKLI_ZSH_COMPLETIONS_DIR="$STARKLI_DIR/share/zsh-completions"

BIN_URL="https://raw.githubusercontent.com/xJonathanLEI/starkli/master/starkliup/starkliup"
BIN_PATH="$STARKLI_BIN_DIR/starkliup"

ENV_PATH="$STARKLI_DIR/env"
ENV_FISH_PATH="$STARKLI_DIR/env-fish"


mkdir -p $STARKLI_BIN_DIR
mkdir -p $STARKLI_MAN_DIR
mkdir -p $STARKLI_BASH_COMPLETIONS_DIR
mkdir -p $STARKLI_FISH_COMPLETIONS_DIR
mkdir -p $STARKLI_ZSH_COMPLETIONS_DIR

curl -# -L $BIN_URL -o $BIN_PATH
chmod +x $BIN_PATH

# Generates the env file on the fly
# Generates the env file on the fly for shells other than Fish
cat > $ENV_PATH <<EOF
#!/bin/sh
Expand Down Expand Up @@ -89,17 +92,34 @@ fi
EOF
chmod +x $ENV_PATH

# Generates the env file on the fly for Fish
cat > $ENV_FISH_PATH <<EOF
# Adds binary directory to PATH
if not contains ${STARKLI_BIN_DIR} $PATH
fish_add_path ${STARKLI_BIN_DIR}
end
. $STARKLI_FISH_COMPLETIONS_DIR/starkli
EOF
chmod +x $ENV_FISH_PATH

# This detection here is just for showing the help message at the end.
IS_SUPPORTED_SHELL=""
IS_FISH_SHELL=""
if [ -n "$ZSH_NAME" ]; then
IS_SUPPORTED_SHELL="1"
fi
if [ -n "$FISH_VERSION" ]; then
IS_SUPPORTED_SHELL="1"
IS_FISH_SHELL="1"
fi
case $SHELL in
*/bash)
IS_SUPPORTED_SHELL="1"
;;
*/fish)
IS_SUPPORTED_SHELL="1"
IS_FISH_SHELL="1"
;;
*/ash)
IS_SUPPORTED_SHELL="1"
Expand All @@ -113,6 +133,7 @@ esac
echo
echo "${cyan}Shell detection variables (for debugging use):${normal}"
echo "${cyan}- ZSH_NAME = $ZSH_NAME${normal}"
echo "${cyan}- FISH_VERSION = $FISH_VERSION${normal}"
echo "${cyan}- SHELL = $SHELL${normal}"

# Inserts this line into whatever shell profile we find, regardless of what the active shell is.
Expand All @@ -122,13 +143,22 @@ insert_env_line "$HOME/.bashrc"
insert_env_line "$HOME/.bash_profile"
insert_env_line "${ZDOTDIR-"$HOME"}/.zshenv"
insert_env_line "${ZDOTDIR-"$HOME"}/.zshrc"

ENV_LINE=". \"${ENV_FISH_PATH}\""
insert_env_line "$HOME/.config/fish/config.fish"

echo

if [ -n "$IS_SUPPORTED_SHELL" ]; then
echo "Run '${yellow}. ${ENV_PATH}${normal}' or start a new terminal session to use starkliup."
if [ -n "$IS_FISH_SHELL" ]; then
# We somehow know for sure it's a Fish shell. Not sure if this can actually happen though.
echo "Run '${yellow}. ${ENV_FISH_PATH}${normal}' or start a new terminal session to use starkliup."
else
# Fish shell is hard to detect and this script is likely run with another shell. So there's
# a chance the user still uses Fish.
echo "Run '${yellow}. ${ENV_PATH}${normal}' (or '${yellow}. ${ENV_FISH_PATH}${normal}' if you're using Fish) or start a new terminal session to use starkliup."
fi
echo "Then, simply run ${yellow}starkliup${normal} to install starkli."
else
echo "starkliup: could not detect shell. Add '${yellow}. ${ENV_PATH}${normal}' to your shell profile, or manually add '${yellow}${STARKLI_BIN_DIR}${normal}' to your PATH environment variable."
echo "starkliup: could not detect shell. Add '${yellow}. ${ENV_PATH}${normal}' to your shell profile (or '${yellow}. ${ENV_FISH_PATH}${normal}' if you're using Fish), or manually add '${yellow}${STARKLI_BIN_DIR}${normal}' to your PATH environment variable."
fi
7 changes: 6 additions & 1 deletion starkliup/starkliup
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ STARKLI_BIN_DIR="$STARKLI_DIR/bin"
STARKLI_MAN_DIR="$STARKLI_DIR/share/man/man1"

STARKLI_BASH_COMPLETIONS_DIR="$STARKLI_DIR/share/bash-completions"
STARKLI_FISH_COMPLETIONS_DIR="$STARKLI_DIR/share/fish-completions"
STARKLI_ZSH_COMPLETIONS_DIR="$STARKLI_DIR/share/zsh-completions"

STARKLI_BIN_PATH="${STARKLI_BIN_DIR}/starkli"

# This MUST be updated whenever this file is changed.
# TODO: add CI check to ensure this.
STARKLIUP_VERSION="2023-08-30"
STARKLIUP_VERSION="2024-07-30"

# Fancy color setup:
# https://unix.stackexchange.com/questions/9957/how-to-check-if-bash-can-print-colors
Expand Down Expand Up @@ -135,6 +136,10 @@ completions() {
$STARKLI_BIN_PATH completions bash > "${STARKLI_BASH_COMPLETIONS_DIR}/starkli"
echo " ${cyan}Done${normal}"

printf -- "- Fish ..."
$STARKLI_BIN_PATH completions fish > "${STARKLI_FISH_COMPLETIONS_DIR}/starkli"
echo " ${cyan}Done${normal}"

printf -- "- Zsh ..."
$STARKLI_BIN_PATH completions zsh > "${STARKLI_ZSH_COMPLETIONS_DIR}/_starkli"
echo " ${cyan}Done${normal}"
Expand Down

0 comments on commit bb2bc2c

Please sign in to comment.