m/fzf
1
0
mirror of https://github.com/junegunn/fzf.git synced 2025-11-18 08:13:40 -05:00

Improve bash completion: [DIRECTORY/][FUZZY_PATTERN]**<TAB>

This commit is contained in:
Junegunn Choi
2013-11-23 20:09:56 +09:00
parent c61738ae43
commit f660ad35b2
2 changed files with 38 additions and 26 deletions

View File

@@ -31,31 +31,37 @@ _fzf_opts_completion() {
}
_fzf_generic_completion() {
local cur prev opts base matches
local cur prev opts base dir leftover matches
COMPREPLY=()
FZF_COMPLETION_TRIGGER=${FZF_COMPLETION_TRIGGER:-**}
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
if [[ ${cur} == *"$FZF_COMPLETION_TRIGGER" ]]; then
base=${cur:0:${#cur}-${#FZF_COMPLETION_TRIGGER}}
base=${base%/}
eval base=$base
find_opts="-name .git -prune -o -name .svn -prune -o"
if [ -z "$base" -o -d "$base" ]; then
matches=$(find ${base:-*} $1 2> /dev/null | fzf $FZF_COMPLETION_OPTS $2 | while read item; do
if [[ ${item} =~ \ ]]; then
echo -n "\"$item\" "
else
echo -n "$item "
dir="$base"
while [ 1 ]; do
if [ -z "$dir" -o -d "$dir" ]; then
leftover=${base/#"$dir"}
leftover=${leftover/#\/}
[ "$dir" = '.' ] && dir=''
matches=$(find "$dir"* $1 2> /dev/null | fzf $FZF_COMPLETION_OPTS $2 -q "$leftover" | while read item; do
if [[ ${item} =~ \ ]]; then
echo -n "\"$item\" "
else
echo -n "$item "
fi
done)
matches=${matches% }
if [ -n "$matches" ]; then
COMPREPLY=( "$matches" )
return 0
fi
done)
matches=${matches% }
if [ -n "$matches" ]; then
COMPREPLY=( "$matches" )
return 0
return 1
fi
fi
dir=$(dirname "$dir")
done
fi
}