Skip to content

Commit d98c2ba

Browse files
authored
Merge pull request #5 from ecksun/bash_completion
Add bash completion support
2 parents 4c955ea + 6323faa commit d98c2ba

File tree

3 files changed

+119
-2
lines changed

3 files changed

+119
-2
lines changed

README.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,18 @@ curl https://cdn.rawgit.com/pimterry/notes/v0.1.2/notes > /usr/local/bin/notes &
2929

3030
By default your notes live in ~/notes, but you can change that to anywhere you like by setting the `$NOTES_DIRECTORY` environmental variable.
3131

32+
### Bash completion
33+
34+
If you want bash autocompletion copy the completion script into the bash completion directory. The bash completion directory is `/usr/share/bash-completion/completions/` on a typical debian jessie install, you can you can get the bash completion directory by running the following command:
35+
36+
pkg-config --variable=completionsdir bash-completion
37+
38+
Installing the completions might be as follows:
39+
40+
```bash
41+
curl https://cdn.rawgit.com/pimterry/notes/v0.1.1/notes.bash_completion > /usr/share/bash-completion/completions/notes
42+
```
43+
3244
## How do I use it?
3345

3446
### `notes new <note-name>`
@@ -66,8 +78,8 @@ All the above works. Here's what's coming next:
6678
- [ ] Combining find and grep, to match either one
6779
- [ ] More interesting and nicer looking file/grep search result formatting (...only when not piping?)
6880
- [ ] Make the file extension optional
69-
- [ ] Bash/zsh command autocompletion
70-
- [ ] Bash/zsh note name autocompletion
81+
- [ ] zsh command autocompletion
82+
- [ ] zsh note name autocompletion
7183
- [ ] Interactive mode? `notes` could open a scrollable list of notes, open your editor when you pick one, and reappear after you close it.
7284
- [ ] Tree view.
7385
- [ ] Easy way to see short notes snippets in find/grep/tree? Could be option (`--snippets`) or by piping to a command (`notes find | notes snippets`). Maybe call it `head`?

notes.bash_completion

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/bin/bash
2+
3+
_notes_complete_notes() {
4+
local configured_dir=${NOTES_DIRECTORY%/} # Remove trailing slashes
5+
local notes_dir="${configured_dir:-$HOME/notes}"
6+
local OLD_IFS="$IFS"
7+
IFS=$'\n'
8+
local items=($(compgen -f "$notes_dir/$1" | sort ))
9+
IFS="$OLD_IFS"
10+
for item in "${items[@]}"; do
11+
[[ $item =~ /\.[^/]*$ ]] && continue
12+
[[ -d $item ]] && item="$item/"
13+
local filename=${item#$notes_dir/}
14+
COMPREPLY+=("${filename%.md}")
15+
done
16+
}
17+
18+
_notes_complete_commands() {
19+
local valid_commands="new find grep open"
20+
COMPREPLY=($(compgen -W "${valid_commands}" -- "${1}"))
21+
}
22+
23+
_notes()
24+
{
25+
local cur
26+
27+
COMPREPLY=() # Array variable storing the possible completions.
28+
cur=${COMP_WORDS[COMP_CWORD]}
29+
30+
if [[ $COMP_CWORD -gt 1 ]]; then
31+
case "${COMP_WORDS[1]}" in
32+
new)
33+
_notes_complete_notes "$cur"
34+
;;
35+
find)
36+
_notes_complete_notes "$cur"
37+
;;
38+
grep)
39+
;;
40+
open)
41+
_notes_complete_notes "$cur"
42+
;;
43+
esac
44+
else
45+
compopt +o nospace
46+
_notes_complete_commands "$cur"
47+
fi
48+
return 0
49+
}
50+
51+
complete -o filenames -o nospace -F _notes notes

test/test-bash-completion.bats

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!./libs/bats/bin/bats
2+
3+
load 'libs/bats-support/load'
4+
load 'libs/bats-assert/load'
5+
load 'helpers'
6+
7+
setup() {
8+
setupNotesEnv
9+
source notes.bash_completion
10+
COMP_WORDS=()
11+
}
12+
13+
teardown() {
14+
teardownNotesEnv
15+
}
16+
17+
@test "Should list all commands" {
18+
touch $NOTES_DIRECTORY/note1.md
19+
_notes_complete_commands ""
20+
assert_equal "${COMPREPLY[0]}" 'new'
21+
assert_equal "${COMPREPLY[1]}" 'find'
22+
assert_equal "${COMPREPLY[2]}" 'grep'
23+
assert_equal "${COMPREPLY[3]}" 'open'
24+
}
25+
26+
@test "Should show matching note when found" {
27+
touch $NOTES_DIRECTORY/note1.md
28+
_notes_complete_notes "no"
29+
assert_equal "${COMPREPLY[@]}" "note1"
30+
}
31+
32+
@test "Should show multiple matching notes" {
33+
touch $NOTES_DIRECTORY/note1.md
34+
touch $NOTES_DIRECTORY/note2.md
35+
_notes_complete_notes "no"
36+
assert_equal "${COMPREPLY[0]}" 'note1'
37+
assert_equal "${COMPREPLY[1]}" 'note2'
38+
assert_equal 2 "${#COMPREPLY[@]}"
39+
}
40+
41+
@test "Should show one completion for note with space" {
42+
touch "$NOTES_DIRECTORY/my note.md"
43+
_notes_complete_notes ""
44+
assert_equal "${COMPREPLY[0]}" 'my note'
45+
assert_equal 1 "${#COMPREPLY[@]}"
46+
}
47+
48+
@test "Should ignore hidden files" {
49+
touch "$NOTES_DIRECTORY/note1.md"
50+
touch "$NOTES_DIRECTORY/.hiddennote.md"
51+
_notes_complete_notes ""
52+
assert_equal "${COMPREPLY[0]}" 'note1'
53+
assert_equal 1 "${#COMPREPLY[@]}"
54+
}

0 commit comments

Comments
 (0)