forked from githubnemo/goto-tool
-
Notifications
You must be signed in to change notification settings - Fork 1
/
goto.completion
126 lines (97 loc) · 3 KB
/
goto.completion
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#have goto-tool &&
_goto-tool() {
COMPREPLY=()
local cur prev entries
_get_comp_words_by_ref cur prev
if [[ "$prev" == "list" ]]; then
return 0;
fi
case $prev in
get|add|rm|mv|use)
entries=$( goto-tool list | cut -d ":" -f 1 )
COMPREPLY=( $( compgen -W "$entries" -- "$cur" ) )
return 0
;;
esac
_filedir
} &&
complete -F _goto-tool goto-tool
# Escape whitespaces and such in each reply given in $1.
# Store the result in COMPREPLY.
_quoted_godir_comreply() {
local i=0
IFS=$'\n'
for reply in $1; do
_quote_readline_by_ref "$reply" qreply
COMPREPLY[$i]="$qreply"
i=$(($i + 1))
done
}
# Set COMPREPLY according to the given current needle ($1) on the
# list of possible goto targets.
_complete_goto_targets() {
cur="$1"
entries=$( goto-tool list | cut -d ":" -f 1 )
compopt +o nospace &>/dev/null
COMPREPLY=( $( compgen -W "$entries" -- "$cur" ) )
}
#have goget &&
_goget() {
COMPREPLY=()
local cur prev entries
_get_comp_words_by_ref -n : cur prev words cword
if [[ "$prev" == "goget" ]]; then
_complete_goto_targets "$cur"
elif [ "${#words[@]}" -eq 3 ]; then
# completion for the optional subdir parameter
subdir=${cur/:-/}
dir=$( goto-tool get "$prev" )
# Get dirs and files in the resolved path + appended path.
# Append a / to every completion target if it's a directory.
dirs=$( compgen -o default "$dir/$cur" | while read comp; do
x="";
if [ -d "$comp" ]; then x="/"; fi;
echo ${comp#$dir/}$x; done )
completions=$( compgen -W "$dirs" -- "$cur" )
_quoted_godir_comreply "$completions"
fi
return 0
} &&
complete -o nospace -F _goget goget
#have goto &&
_goto() {
COMPREPLY=()
local cur prev entries
_get_comp_words_by_ref -n : cur prev words cword
if [[ "$prev" == "goto" ]]; then
# completion for goto directories
_complete_goto_targets "$cur"
elif [ "${#words[@]}" -eq 3 ]; then
# completion for the optional subdir parameter
# Determine the subdir according to the given goto dir (prev param)
subdir=${cur/:-/}
dir=$( goto-tool get "$prev" )
# Find all directories in the target directory and generate
# possible matches.
dirs=$(
compgen -o dirnames "$dir/$cur" | while read comp; do
echo ${comp#$dir/}/
done
)
# Gather all directory matches according to the current needle.
# Be careful only to slice by newline to not truncate those
# elements which have whitespaces in them.
completions=$( IFS=$'\n' compgen -W "$dirs" -- "$cur" )
# Quote each result found
_quoted_godir_comreply "$completions"
fi
return 0
} &&
complete -o nospace -F _goto goto
# Local variables:
# mode: shell-script
# sh-basic-offset: 4
# sh-indent-comment: t
# indent-tabs-mode: nil
# End:
# ex: ts=4 sw=4 et filetype=sh