-
Notifications
You must be signed in to change notification settings - Fork 0
/
branch_notes.bash-completion
73 lines (65 loc) · 2.06 KB
/
branch_notes.bash-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
# branch-notes completion
# vim: filetype=sh
# The name for the notes directory if the NOTES_DIR_VARIABLE is not set.
DEFAULT_NOTES_DIR_NAME="branch-notes"
_branch_notes_complete()
{
local cur prev opts
allopts="open list archive --help --toplevel --editor"
listopts="--help --toplevel"
openopts="--help --toplevel --editor"
archiveopts="--help --toplevel"
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
if [ -z "$NOTES_DIR" ]; then
NOTES_DIR="$HOME/$DEFAULT_NOTES_DIR_NAME"
fi
if [ -d "$NOTES_DIR" ]; then
case "${prev}" in
--toplevel | -t)
local toplevels
toplevels=$(find "$NOTES_DIR/" -maxdepth 1 -type d -print \
-name "[^\.]\*" | awk -F/ '{print $NF}' | \
sed -e 's/[[:space:]]/\\\ /g')
COMPREPLY=($(compgen -W "${toplevels}" -- "${cur}"))
return 0
;;
list)
COMPREPLY=( $(compgen -W "${listopts}" -- "${cur}") )
return 0
;;
open | archive)
local notes
notes=$(find "$NOTES_DIR/" -maxdepth 2 \( -type f -or -type l \) -print \
-name "*.txt" | awk -F/ '{print $NF}' | cut -d "." -f 1)
COMPREPLY=($(compgen -W "${notes}" -- "${cur}"))
return 0
;;
esac
fi
# Depending on the action in the command line we autocomplete with the
# appropriate options.
local opts
opts=$allopts
for w in "${COMP_WORDS[@]}"
do
case "${w}" in
open)
opts=$openopts
break
;;
list)
opts=$listopts
break
;;
archive)
opts=$archiveopts
break
;;
esac
done
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
}
complete -F _branch_notes_complete branch-notes