Took some inspiration from git tab completion, and separated the completion of '--' commands from project directories

This commit is contained in:
Nathan Broadbent
2011-12-17 10:53:58 +08:00
parent cb50656cf0
commit 1ad7408d05

View File

@@ -239,20 +239,25 @@ function _git_index_tab_completion() {
local project=$(echo "$curw" | cut -d "/" -f1) local project=$(echo "$curw" | cut -d "/" -f1)
local base_path=$(grep "/$project$" "$GIT_REPO_DIR/.git_index" | sed 's/ /\\ /g') local base_path=$(grep "/$project$" "$GIT_REPO_DIR/.git_index" | sed 's/ /\\ /g')
# If matching path was found and curr string contains a /, then complete project sub-directories # If matching project path was found and curr string contains a /, then complete project sub-directories
if [[ -n "$base_path" && $curw == */* ]]; then if [[ -n "$base_path" && $curw == */* ]]; then
local search_path=$(echo "$curw" | sed "s:^${project/\\/\\\\\\}::") local search_path=$(echo "$curw" | sed "s:^${project/\\/\\\\\\}::")
COMPREPLY=($(compgen -d "$base_path$search_path" | grep -v "/.git" | sed -e "s:$base_path:$project:" -e "s:$:/:" )) COMPREPLY=($(compgen -d "$base_path$search_path" | grep -v "/.git" | sed -e "s:$base_path:$project:" -e "s:$:/:" ))
# If curr string starts with /, tab complete top-level directories in root project dir # If curr string starts with /, tab complete top-level directories in root project dir
elif ([ $shell = "bash" ] && [ "${curw:0:1}" = "/" ]) || \ elif ([ $shell = "bash" ] && [ "${curw:0:1}" = "/" ]) || \
([ $shell = "zsh" ] && [ "${curw[1]}" = "/" ]); then ([ $shell = "zsh" ] && [ "${curw[1,1]}" = "/" ]); then
COMPREPLY=($(compgen -d "$GIT_REPO_DIR$curw" | sed -e "s:$GIT_REPO_DIR/::" -e "s:^:/:")) COMPREPLY=($(compgen -d "$GIT_REPO_DIR$curw" | sed -e "s:$GIT_REPO_DIR/::" -e "s:^:/:"))
# Else, tab complete the entries in .git_index, plus '--' commands # If curr string starts with --, tab complete commands
else elif ([ $shell = "bash" ] && [ "${curw:0:2}" = "--" ]) || \
([ $shell = "zsh" ] && [ "${curw[1,2]}" = "--" ]); then
local commands="--list\n--rebuild\n--update-all\n--batch-cmd\n--count-by-host" local commands="--list\n--rebuild\n--update-all\n--batch-cmd\n--count-by-host"
COMPREPLY=($(compgen -W '$(sed -e "s:.*/::" -e "s:$:/:" "$GIT_REPO_DIR/.git_index" | sort)$(echo -e "\n"$commands)' -- $curw)) COMPREPLY=($(compgen -W '$(echo -e "\n"$commands)' -- $curw))
# Else, tab complete the entries in .git_index
else
COMPREPLY=($(compgen -W '$(sed -e "s:.*/::" -e "s:$:/:" "$GIT_REPO_DIR/.git_index" | sort)' -- $curw))
fi fi
unset IFS unset IFS
return 0 return 0