Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fetch-configlet: print instructions for installing completions #647

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions scripts/fetch-configlet
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -69,6 +107,8 @@ main() {
esac

rm -f "${output_path}"

emit_completion_command
}

main