Skip to content

Commit

Permalink
+ completions: enable incus completion and aliases for faster cli usage
Browse files Browse the repository at this point in the history
been using incus a lot lately, definitely the completions help.  had to
import some internal bash completion functions, either that or source
the monster bash completion script that comes from bash-completion
project, which is totally unnecessary and probably kill shell init time
badly by installing functions for so many things and doing a lot of
parsing every time

this is only temporary, it does itself add some to shell init just to
have this small subset, we will eventually make this conditional on the
host or maybe have to "activate" it with a shell function before it
tries to run incus or a bunch of other shell functions
  • Loading branch information
smemsh committed Sep 22, 2024
1 parent 7a5351f commit 0b7eaba
Show file tree
Hide file tree
Showing 2 changed files with 219 additions and 0 deletions.
2 changes: 2 additions & 0 deletions init.d/aliases
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,8 @@ alias akdolast "akdolast 1"
alias s "sudo"
alias si "sudo -i"
alias svim "sudo -e"
alias inc "incus"
alias sincus "sudo incus"
alias permsave "(cd /etc; sudo metastore -s /etc; cd \$OLDPWD)"
alias permrestore "(cd /etc; sudo metastore -ae /etc; cd \$OLDPWD)"
alias permchk "(cd /etc; sudo metastore -c /etc; cd \$OLDPWD)"
Expand Down
217 changes: 217 additions & 0 deletions init.d/completions
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,222 @@ shell_init_completions_file ()
for cmd; do complete -A file $cmd; done
}

shell_init_completions_incus ()
{
if [[ $(type -P incus) ]]
then
source <(incus completion bash)
complete -o default -F __start_incus incus sincus inc
fi
}

##############################################################################

# add these functions to our shell environment, incus completions expect it.
# from /usr/share/bash-completion/bash_completion
#
# TODO fix upstream, these should not be required when incus completions are
# otherwise standalone, there should be barebones versions within incus itself
#
_upvars()
{
if ! (($#)); then
echo "bash_completion: $FUNCNAME: usage: $FUNCNAME" \
"[-v varname value] | [-aN varname [value ...]] ..." >&2
return 2
fi
while (($#)); do
case $1 in
-a*)
# Error checking
[[ ${1#-a} ]] || {
echo "bash_completion: $FUNCNAME:" \
"\`$1': missing number specifier" >&2
return 1
}
printf %d "${1#-a}" &>/dev/null || {
echo bash_completion: \
"$FUNCNAME: \`$1': invalid number specifier" >&2
return 1
}
# Assign array of -aN elements
[[ "$2" ]] && unset -v "$2" && eval $2=\(\"\$"{@:3:${1#-a}}"\"\) &&
shift $((${1#-a} + 2)) || {
echo bash_completion: \
"$FUNCNAME: \`$1${2+ }$2': missing argument(s)" \
>&2
return 1
}
;;
-v)
# Assign single value
[[ "$2" ]] && unset -v "$2" && eval $2=\"\$3\" &&
shift 3 || {
echo "bash_completion: $FUNCNAME: $1:" \
"missing argument(s)" >&2
return 1
}
;;
*)
echo "bash_completion: $FUNCNAME: $1: invalid option" >&2
return 1
;;
esac
done
}

__reassemble_comp_words_by_ref()
{
local exclude i j line ref
# Exclude word separator characters?
if [[ $1 ]]; then
# Yes, exclude word separator characters;
# Exclude only those characters, which were really included
exclude="[${1//[^$COMP_WORDBREAKS]/}]"
fi

# Default to cword unchanged
printf -v "$3" %s "$COMP_CWORD"
# Are characters excluded which were former included?
if [[ -v exclude ]]; then
# Yes, list of word completion separators has shrunk;
line=$COMP_LINE
# Re-assemble words to complete
for ((i = 0, j = 0; i < ${#COMP_WORDS[@]}; i++, j++)); do
# Is current word not word 0 (the command itself) and is word not
# empty and is word made up of just word separator characters to
# be excluded and is current word not preceded by whitespace in
# original line?
while [[ $i -gt 0 && ${COMP_WORDS[i]} == +($exclude) ]]; do
# Is word separator not preceded by whitespace in original line
# and are we not going to append to word 0 (the command
# itself), then append to current word.
[[ $line != [[:blank:]]* ]] && ((j >= 2)) && ((j--))
# Append word separator to current or new word
ref="$2[$j]"
printf -v "$ref" %s "${!ref-}${COMP_WORDS[i]}"
# Indicate new cword
((i == COMP_CWORD)) && printf -v "$3" %s "$j"
# Remove optional whitespace + word separator from line copy
line=${line#*"${COMP_WORDS[i]}"}
# Start new word if word separator in original line is
# followed by whitespace.
[[ $line == [[:blank:]]* ]] && ((j++))
# Indicate next word if available, else end *both* while and
# for loop
((i < ${#COMP_WORDS[@]} - 1)) && ((i++)) || break 2
done
# Append word to current word
ref="$2[$j]"
printf -v "$ref" %s "${!ref-}${COMP_WORDS[i]}"
# Remove optional whitespace + word from line copy
line=${line#*"${COMP_WORDS[i]}"}
# Indicate new cword
((i == COMP_CWORD)) && printf -v "$3" %s "$j"
done
((i == COMP_CWORD)) && printf -v "$3" %s "$j"
else
# No, list of word completions separators hasn't changed;
for i in "${!COMP_WORDS[@]}"; do
printf -v "$2[i]" %s "${COMP_WORDS[i]}"
done
fi
} # __reassemble_comp_words_by_ref()

__get_cword_at_cursor_by_ref()
{
local cword words=()
__reassemble_comp_words_by_ref "$1" words cword

local i cur="" index=$COMP_POINT lead=${COMP_LINE:0:COMP_POINT}
# Cursor not at position 0 and not leaded by just space(s)?
if [[ $index -gt 0 && ($lead && ${lead//[[:space:]]/}) ]]; then
cur=$COMP_LINE
for ((i = 0; i <= cword; ++i)); do
# Current word fits in $cur, and $cur doesn't match cword?
while [[ ${#cur} -ge ${#words[i]} && \
${cur:0:${#words[i]}} != "${words[i]-}" ]]; do
# Strip first character
cur="${cur:1}"
# Decrease cursor position, staying >= 0
((index > 0)) && ((index--))
done

# Does found word match cword?
if ((i < cword)); then
# No, cword lies further;
local old_size=${#cur}
cur="${cur#"${words[i]}"}"
local new_size=${#cur}
((index -= old_size - new_size))
fi
done
# Clear $cur if just space(s)
[[ $cur && ! ${cur//[[:space:]]/} ]] && cur=
# Zero $index if negative
((index < 0)) && index=0
fi

local "$2" "$3" "$4" && _upvars -a${#words[@]} $2 ${words+"${words[@]}"} \
-v $3 "$cword" -v $4 "${cur:0:index}"
}

_get_comp_words_by_ref()
{
local exclude flag i OPTIND=1
local cur cword words=()
local upargs=() upvars=() vcur vcword vprev vwords

while getopts "c:i:n:p:w:" flag "$@"; do
case $flag in
c) vcur=$OPTARG ;;
i) vcword=$OPTARG ;;
n) exclude=$OPTARG ;;
p) vprev=$OPTARG ;;
w) vwords=$OPTARG ;;
*)
echo "bash_completion: $FUNCNAME: usage error" >&2
return 1
;;
esac
done
while [[ $# -ge $OPTIND ]]; do
case ${!OPTIND} in
cur) vcur=cur ;;
prev) vprev=prev ;;
cword) vcword=cword ;;
words) vwords=words ;;
*)
echo "bash_completion: $FUNCNAME: \`${!OPTIND}':" \
"unknown argument" >&2
return 1
;;
esac
((OPTIND += 1))
done

__get_cword_at_cursor_by_ref "${exclude-}" words cword cur

[[ -v vcur ]] && {
upvars+=("$vcur")
upargs+=(-v $vcur "$cur")
}
[[ -v vcword ]] && {
upvars+=("$vcword")
upargs+=(-v $vcword "$cword")
}
[[ -v vprev && $cword -ge 1 ]] && {
upvars+=("$vprev")
upargs+=(-v $vprev "${words[cword - 1]}")
}
[[ -v vwords ]] && {
upvars+=("$vwords")
upargs+=(-a${#words[@]} $vwords ${words+"${words[@]}"})
}

((${#upvars[@]})) && local "${upvars[@]}" && _upvars "${upargs[@]}"
}

##############################################################################
#
# these are retained after we exit, used by the completion
Expand Down Expand Up @@ -105,3 +321,4 @@ complete -c -A file s # complete builtin is not alias-aware
# about it, leaving commented for now 20171230134138
#
#complete -D -o nospace -o bashdefault -o default
#

0 comments on commit 0b7eaba

Please sign in to comment.