forked from pimterry/notes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
notes.bash_completion
65 lines (58 loc) · 1.67 KB
/
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
#!/bin/bash
_notes_complete_notes() {
# Look for configuration file at ~/.config/notes/config and use it
if [ -f ~/.config/notes/config ]; then
. ~/.config/notes/config
fi
local configured_dir=${NOTES_DIRECTORY%/} # Remove trailing slashes
local notes_dir="${configured_dir:-$HOME/notes}"
local OLD_IFS="$IFS"
IFS=$'\n'
local items=($(compgen -f "$notes_dir/$1" | sort ))
IFS="$OLD_IFS"
for item in "${items[@]}"; do
[[ $item =~ /\.[^/]*$ ]] && continue
[[ -d $item ]] && item="$item/"
local filename=${item#$notes_dir/}
COMPREPLY+=("${filename%.md}")
done
}
_notes_complete_commands() {
local valid_commands="new find grep open ls rm cat append search"
COMPREPLY=($(compgen -W "${valid_commands}" -- "${1}"))
}
_notes()
{
local cur
COMPREPLY=() # Array variable storing the possible completions.
cur=${COMP_WORDS[COMP_CWORD]}
if [[ $COMP_CWORD -gt 1 ]]; then
case "${COMP_WORDS[1]}" in
new|n)
_notes_complete_notes "$cur"
;;
find|f)
_notes_complete_notes "$cur"
;;
grep)
;;
append|a)
_notes_complete_notes "$cur"
;;
open|o)
_notes_complete_notes "$cur"
;;
cat|c)
_notes_complete_notes "$cur"
;;
ls)
_notes_complete_notes "$cur"
;;
esac
else
compopt +o nospace
_notes_complete_commands "$cur"
fi
return 0
}
complete -o filenames -o nospace -F _notes notes