From 500646685cee334f4a22eb52fe2e34ae166b31eb Mon Sep 17 00:00:00 2001 From: Anatoly Laskaris Date: Sun, 4 Jul 2021 00:29:36 +0300 Subject: [PATCH] Update bash completions (#65) --- completion/smug.bash | 75 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 69 insertions(+), 6 deletions(-) diff --git a/completion/smug.bash b/completion/smug.bash index c2f1c2c..11d081e 100644 --- a/completion/smug.bash +++ b/completion/smug.bash @@ -1,8 +1,71 @@ -_smug_list_projects () { - if [ "${#COMP_WORDS[@]}" != "2" ]; then - return - fi - COMPREPLY=($(ls ~/.config/smug | grep -v "smug\.log" | sed -e 's/\..*//')) +#! /usr/bin/env bash +_smug() { + local ISF=$'\n' + local reply + + local cur="${COMP_WORDS[COMP_CWORD]}" + local prev="${COMP_WORDS[COMP_CWORD-1]}" + + # if command is 'list' or 'print' do not suggest more + for word in ${COMP_WORDS[@]}; do + case $word in + list|print) return + esac + done + + # commands + if (( "${#COMP_WORDS[@]}" == 2 )); then + reply=($(compgen -W "list print start stop" -- "${cur}")) + fi + + # projects + if (( "${#COMP_WORDS[@]}" == 3 )); then + case ${prev} in + start|stop) + reply=($(compgen -W "$(smug list | grep -F -v smug)" -- "${cur}")) + esac + fi + + # options + if (( "${#COMP_WORDS[@]}" > 3 )); then + local options=( "--file" "--windows" "--attach" "--debug" ) + + # --windows waits for a list + case $prev in + -w|--windows) return + esac + + # suggest options that were not specified already + for word in "${COMP_WORDS[@]}"; do + case $word in + -f|--file) options=( "${options[@]/--file}" ) ;; + -w|--windows) options=( "${options[@]/--windows}" ) ;; + -a|--attach) options=( "${options[@]/--attach}" ) ;; + -d|--debug) options=( "${options[@]/--debug}" ) ;; + esac + done + + # array to string + local options="$(echo ${options[@]})" + + reply=($(compgen -W "${options}" -- "${cur}")) + fi + + + # if only one match proceed with autocompletion + if (( "${#reply[@]}" == 1 )); then + COMPREPLY=( "${reply[0]}" ) + else + # when 'TAB TAB' is pressed + if (( COMP_TYPE == 63 )); then + # print suggestions as list with padding + for i in "${!reply[@]}"; do + reply[$i]="$(printf '%*s' "-$COLUMNS" "${reply[$i]}")" + done + fi + # print suggestions + COMPREPLY=( "${reply[@]}" ) + fi } -complete -F _smug_list_projects smug +complete -F _smug smug