Skip to content

Instantly share code, notes, and snippets.

@kou1okada
Last active January 16, 2023 13:04
Show Gist options
  • Save kou1okada/010b8df3c35ac6047812ee142c8ab004 to your computer and use it in GitHub Desktop.
Save kou1okada/010b8df3c35ac6047812ee142c8ab004 to your computer and use it in GitHub Desktop.
dump_comp_vars.bash - Dump completion variables for bash

dump_comp_vars.bash - Dump completion variables for bash

Usage

1. Make symbolic link into $BINDIR as:

BINDIR=~/.local/bin/
ln -sv "$PWD/dump_comp_vars.bash" "${BINDIR:-/usr/local/bin/}"

2. Include dump_comp_vars.bash at your completion script as:

. dump_comp_vars.bash

3. Call dump_comp_vars function at your completion function as:

function __mycomp ()
{
  local cur prev
  _get_comp_words_by_ref -n : cur prev
  # ...
  readarray -t COMPREPLY < <(compgen -W "${COMPREPLY[*]@Q}" -- "$cur")
  # ...
  dump_comp_vars
}

License

MIT license.

# dump_comp_vars.bash - Dump completion variables for bash
# Copyright 2022 (c) Koichi OKADA. All right reserved.
# This script is distributed under the MIT license.
function dump_comp_vars () #
# Dump completion variables.
{
local -a COMP_TYPE_DESC=(
[ 9]=" 9 # HT : normal completion"
[33]="33 # ! : listing alternatives on partial word completion"
[37]="37 # % : menu completion"
[63]="63 # ? : listing completions after successive tabs"
[64]="64 # @ : list completions if the word is not unmodified"
)
# Dump completion variables
echo
echo "prev : $prev"
echo "cur : $cur"
echo "COMP_CWORD : $COMP_CWORD"
echo "COMP_KEY : $COMP_KEY"
echo "COMP_LINE : $COMP_LINE"
echo "COMP_POINT : $COMP_POINT"
echo "COMP_TYPE : ${COMP_TYPE_DESC[COMP_TYPE]}"
echo "COMP_WORDBREAKS : ${COMP_WORDBREAKS@Q})"
echo "#COMP_WORDS[@] : ${#COMP_WORDS[@]}"
local i
for i in "${!COMP_WORDS[@]}"; do
echo "COMP_WORDS[$i] : ${COMP_WORDS[i]}"
done
for i in "${!COMPREPLY[@]}"; do
echo "COMPREPLY[$i] : ${COMPREPLY[i]}"
done
# Redisplay the prompt
if (( COMP_TYPE == 9 )); then
echo -n "${PS1@P}$COMP_LINE"
(( ${#COMP_LINE} > ${COMP_POINT} )) \
&& echo -en "\e[$((${#COMP_LINE}-${COMP_POINT}))D"
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment