forked from pimterry/notes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
notes.bash_completion
59 lines (52 loc) · 1.49 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
#!/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"
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)
_notes_complete_notes "$cur"
;;
find)
_notes_complete_notes "$cur"
;;
grep)
;;
open)
_notes_complete_notes "$cur"
;;
cat)
_notes_complete_notes "$cur"
;;
esac
else
compopt +o nospace
_notes_complete_commands "$cur"
fi
return 0
}
complete -o filenames -o nospace -F _notes notes