forked from mgoral/gogo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gogo.compl
91 lines (74 loc) · 2.7 KB
/
gogo.compl
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
# gogo completion file
# source it to use it.
_gogoAliases()
{
cur=$1
CONF_FILE="${HOME}/.config/gogo/gogo.conf"
# read aliases from $CONF_FILE
if [[ -f ${CONF_FILE} ]]; then
IFS=' ='
CONFALIASES=
while read -r confalias directory
do
# Recognise comments
if [[ ${confalias} =~ '^#' ]]; then
continue
fi
# When <tab> is pressed when full alias is typed in, completion will add "/" to it. This
# way path completion will be enabled.
if [[ ${cur} != "" && (${cur} == ${confalias}) ]]; then
COMPREPLY=( $(compgen -W "${confalias}/" -- ${cur}) )
return
fi
# Path completion
# Basically changes alias to real directory name, fetches all directories inside it and
# changes real directory name back to alias.
if [[ ${cur} == ${confalias}/* ]]; then
unset IFS
if [[ ${directory} != *:https://* ]]; then
# Use this instead of $directory which may contain a tilde interpreted as string
# literal (eval enforces '~' expansion)
eval realdirectory=${directory}
# Replace the first occurence of alias in cur with a real directory
currealdir=${cur/${confalias}/${realdirectory}}
# List directories matching an expression
compdirs="$(echo ${currealdir}*/)"
# compdirs contain this silly string (like /home/user/*/) when there's no
# directory inside
if [[ ${compdirs} != "${currealdir}*/" ]]; then
aliasdirs=${compdirs//${realdirectory}/${confalias}}
COMPREPLY=( $(compgen -W "${aliasdirs}" -- ${cur}) )
fi
fi
return
fi
# don't display the default target
if [[ ${confalias} == "default" ]]; then
continue
fi
CONFALIASES="${CONFALIASES} ${confalias}"
done < ${CONF_FILE}
unset IFS
COMPREPLY=( $(compgen -W "${CONFALIASES}" -- ${cur}) )
fi
}
_gogo()
{
local cur prev opts
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
if [[ ${COMP_CWORD} -ge 2 ]]; then
return 0
fi
opts="-a -l --ls -e --edit -h --help -v --version"
case "${cur}" in
-*)
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
;;
*)
_gogoAliases ${cur}
;;
esac
}
complete -o nospace -F _gogo gogo