diff --git a/scripts/fetch-configlet b/scripts/fetch-configlet index 827088ab..bc1dd6e5 100755 --- a/scripts/fetch-configlet +++ b/scripts/fetch-configlet @@ -49,6 +49,44 @@ get_download_url() { cut -d'"' -f4 } +# get the user's shell (as portably as possible) +# and print out the command to enable tab completion +emit_completion_command() { + local shell file + + if command -v getent > /dev/null; then + shell=$(getent passwd "${USER}" | cut -d: -f7) + elif command -v finger > /dev/null; then + shell=$(finger "${USER}" | sed -nE '/.*Shell: / { s///; s/ .*//; p; }') + elif [[ -f /etc/passwd ]]; then + shell=$(awk -F: -v u="${USER}" '$1 == u {print $7}' /etc/passwd) + fi + + if [[ -z "${shell}" ]]; then + # could not detect the shell + return + fi + + file="bin/completions/configlet.$(basename "${shell}")" + if [[ -f "${file}" && -r "${file}" ]]; then + case "${shell}" in + bash) + echo "To enable tab completions, run:" + echo " configlet completion -s ${shell} | source" + ;; + fish) + echo "To enable tab completions for the current session, run:" + echo " configlet completion -s ${shell} | source" + echo "To enable tab completions for every session, run:" + echo " configlet completion -s ${shell} > ~/.config/fish/completions/configlet.fish" + ;; + zsh) + : # is there more to do here than the above? + ;; + esac + fi +} + main() { if [[ -d ./bin ]]; then output_dir="./bin" @@ -69,6 +107,8 @@ main() { esac rm -f "${output_path}" + + emit_completion_command } main