From e55d7757efca6371be0fcd0541d3416d4e7f9104 Mon Sep 17 00:00:00 2001 From: Glenn Jackman Date: Tue, 12 Jul 2022 08:59:37 -0400 Subject: [PATCH] Detect user's shell and print instructions for tab completion --- scripts/fetch-configlet | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/scripts/fetch-configlet b/scripts/fetch-configlet index 827088ab..226d6738 100755 --- a/scripts/fetch-configlet +++ b/scripts/fetch-configlet @@ -49,6 +49,35 @@ 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 | fish) echo "Enable tab completions: source ${file}" ;; + zsh) + : # is there more to do here than the above? + ;; + esac + fi +} + main() { if [[ -d ./bin ]]; then output_dir="./bin" @@ -69,6 +98,8 @@ main() { esac rm -f "${output_path}" + + emit_completion_command } main