add bash completion

This commit is contained in:
zawz 2021-08-04 22:10:47 +02:00
parent 4b4c398ee7
commit 06a994ba4b
2 changed files with 102 additions and 1 deletions

View file

@ -1,6 +1,6 @@
var_exclude = ZPASS_.* XDG_.* REMOTE_.* DISPLAY CONFIGFILE TMPDIR DEBUG
fct_exclude = _tty_on sftp_cmd ftps_cmd upload download list delete create
fct_exclude = _stop sftp_cmd ftps_cmd upload download list delete create
zpass: src/*
lxsh -o zpass -M --exclude-var "$(var_exclude)" --exclude-fct "$(fct_exclude)" src/main.sh
@ -8,13 +8,18 @@ zpass: src/*
debug: src/*
lxsh -o zpass src/main.sh
bash: src/*
lxsh --bash -o zpass src/main.sh
build: zpass
install: build
mv zpass /usr/local/bin
cp completion/zpass.bash /etc/bash_completion.d
uninstall:
rm /usr/local/bin/zpass
rm /etc/bash_completion.d/zpass.bash
clear:
rm zpass

96
completion/zpass.bash Normal file
View file

@ -0,0 +1,96 @@
#/usr/bin/env bash
# $1 = input arg , $@ = completions
_completion_check_expands() {
local arg=$1
shift 1
local matching=()
local N=0
for I ; do
[ "$I" != "${I#"$arg"}" ] && matching[N++]=$I
done
local superset=${matching[0]:0:${#arg}}
[ "${superset#"$arg"}" = "$superset" ] && return 1
for N in "${matching[@]}" ; do
[ "${N#"$superset"}" = "$N" ] && return 1
done
return 0
}
_zpass_completion()
{
local _cw1="list-files cache-clear help rm-file ls tree create get copy set file add new rm mv exec cached rm-cache"
local _cw1_val_all="l ls g get a add n new r rm m mv"
local _cw1_val1="s set f file x copy"
local _cw1_files="rmf rm-file"
local N=0
local _compwords=
local WORDS=()
local cur=$2
COMPREPLY=()
if { [ "$COMP_CWORD" -eq "2" ] && echo "$_cw1_val1" | grep -qw -- "${COMP_WORDS[1]}" ; } ||
{ [ "$COMP_CWORD" -gt "1" ] && echo "$_cw1_val_all" | grep -qw -- "${COMP_WORDS[1]}"; } ; then
zpass cached || return 0
local dir=$2
[ "${dir}" = "${dir%/}" ] && dir=$(dirname "$2")
N=0
for j in $(zpass ls "$dir") ; do
[ "$j" = "${j%/}" ] && j="$j "
WORDS[N++]=$j
done
cur=$(basename "$cur")
[ "$2" != "${2%/}" ] && cur=""
if _completion_check_expands "$cur" "${WORDS[@]}" ; then
N=0
if [ -n "$cur" ] ; then
for I in "${WORDS[@]}" ; do
[ "$I" != "${I#"$cur"}" ] && COMPREPLY[N++]=$I
done
else
COMPREPLY=("${WORDS[@]}")
fi
N=0
for I in "${COMPREPLY[@]}" ; do
local tt="$dir/$I"
COMPREPLY[N++]="${tt#./}"
done
else
if [ -n "$cur" ] ; then
N=0
for I in "${WORDS[@]}" ; do
[ "$I" != "${I#"$cur"}" ] && COMPREPLY[N++]=$I
done
else
COMPREPLY=("${WORDS[@]}")
fi
fi
else
if [ "$COMP_CWORD" = "1" ] ; then
_compwords="$_cw1"
elif [ "$COMP_CWORD" -gt "1" ] && echo " $_cw1_files " | grep -qF -- " ${COMP_WORDS[1]} " ; then
_compwords=$(zpass lsf)
fi
for I in $_compwords ; do
WORDS[N++]="$I "
done
N=0
if [ -n "$cur" ] ; then
for I in "${WORDS[@]}" ; do
[ "$I" != "${I#"$cur"}" ] && COMPREPLY[N++]=$I
done
else
COMPREPLY=("${WORDS[@]}")
fi
fi
}
complete -o nospace -F _zpass_completion -o dirnames zpass