blob: d0ebbba4f50d5efc41d666eac8c5f1c9d0edd9ce [file] [log] [blame]
Nils Winklerbdc57292013-09-09 08:48:15 +02001# Bash completion script for homesick
2#
3# The homebrew bash completion script was used as inspiration.
4# Originally from https://github.com/liborw/homesick-completion
5
6_homesick_complete()
7{
8 local cur="${COMP_WORDS[COMP_CWORD]}"
9 local options="--skip --force --pretend --quiet"
10 local actions="cd clone commit destroy diff generate help list open pull push rc show_path status symlink track unlink version"
11 local repos=$(\ls ~/.homesick/repos)
12
13 # Subcommand list
14 [[ ${COMP_CWORD} -eq 1 ]] && {
15 COMPREPLY=( $(compgen -W "${options} ${actions}" -- ${cur}) )
16 return
17 }
18
19 # Find the first non-switch word
20 local prev_index=1
21 local prev="${COMP_WORDS[prev_index]}"
22 while [[ $prev == -* ]]; do
23 prev_index=$((++prev_index))
24 prev="${COMP_WORDS[prev_index]}"
25 done
26
27 # Find the number of non-"--" commands
28 local num=0
29 for word in ${COMP_WORDS[@]}
30 do
31 if [[ $word != -* ]]; then
32 num=$((++num))
33 fi
34 done
35
36 case "$prev" in
37 # Commands that take a castle
38 cd|commit|destroy|diff|open|pull|push|rc|show_path|status|symlink|unlink)
39 COMPREPLY=( $(compgen -W "${repos}" -- ${cur}) )
40 return
41 ;;
42 # Commands that take command
43 help)
44 COMPREPLY=( $(compgen -W "${actions}" -- ${cur}) )
45 return
46 ;;
47 # Track command take file and repo
48 track)
49 if [[ "$num" -eq 2 ]]; then
50 COMPREPLY=( $(compgen -X -f ${cur}) )
51 elif [[ "$num" -ge 3 ]]; then
52 COMPREPLY=( $(compgen -W "${repos}" -- ${cur}) )
53 fi
54 return
55 ;;
56 esac
57}
58
59complete -o bashdefault -o default -F _homesick_complete homesick
60