Skip to content

Commit

Permalink
Merge pull request pimterry#5 from ecksun/bash_completion
Browse files Browse the repository at this point in the history
Add bash completion support
  • Loading branch information
pimterry authored Dec 5, 2016
2 parents 4c955ea + 6323faa commit d98c2ba
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 2 deletions.
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,18 @@ curl https://cdn.rawgit.com/pimterry/notes/v0.1.2/notes > /usr/local/bin/notes &

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

### Bash completion

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:

pkg-config --variable=completionsdir bash-completion

Installing the completions might be as follows:

```bash
curl https://cdn.rawgit.com/pimterry/notes/v0.1.1/notes.bash_completion > /usr/share/bash-completion/completions/notes
```

## How do I use it?

### `notes new <note-name>`
Expand Down Expand Up @@ -66,8 +78,8 @@ All the above works. Here's what's coming next:
- [ ] Combining find and grep, to match either one
- [ ] More interesting and nicer looking file/grep search result formatting (...only when not piping?)
- [ ] Make the file extension optional
- [ ] Bash/zsh command autocompletion
- [ ] Bash/zsh note name autocompletion
- [ ] zsh command autocompletion
- [ ] zsh note name autocompletion
- [ ] Interactive mode? `notes` could open a scrollable list of notes, open your editor when you pick one, and reappear after you close it.
- [ ] Tree view.
- [ ] 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`?
Expand Down
51 changes: 51 additions & 0 deletions notes.bash_completion
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/bin/bash

_notes_complete_notes() {
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"
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"
;;
esac
else
compopt +o nospace
_notes_complete_commands "$cur"
fi
return 0
}

complete -o filenames -o nospace -F _notes notes
54 changes: 54 additions & 0 deletions test/test-bash-completion.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!./libs/bats/bin/bats

load 'libs/bats-support/load'
load 'libs/bats-assert/load'
load 'helpers'

setup() {
setupNotesEnv
source notes.bash_completion
COMP_WORDS=()
}

teardown() {
teardownNotesEnv
}

@test "Should list all commands" {
touch $NOTES_DIRECTORY/note1.md
_notes_complete_commands ""
assert_equal "${COMPREPLY[0]}" 'new'
assert_equal "${COMPREPLY[1]}" 'find'
assert_equal "${COMPREPLY[2]}" 'grep'
assert_equal "${COMPREPLY[3]}" 'open'
}

@test "Should show matching note when found" {
touch $NOTES_DIRECTORY/note1.md
_notes_complete_notes "no"
assert_equal "${COMPREPLY[@]}" "note1"
}

@test "Should show multiple matching notes" {
touch $NOTES_DIRECTORY/note1.md
touch $NOTES_DIRECTORY/note2.md
_notes_complete_notes "no"
assert_equal "${COMPREPLY[0]}" 'note1'
assert_equal "${COMPREPLY[1]}" 'note2'
assert_equal 2 "${#COMPREPLY[@]}"
}

@test "Should show one completion for note with space" {
touch "$NOTES_DIRECTORY/my note.md"
_notes_complete_notes ""
assert_equal "${COMPREPLY[0]}" 'my note'
assert_equal 1 "${#COMPREPLY[@]}"
}

@test "Should ignore hidden files" {
touch "$NOTES_DIRECTORY/note1.md"
touch "$NOTES_DIRECTORY/.hiddennote.md"
_notes_complete_notes ""
assert_equal "${COMPREPLY[0]}" 'note1'
assert_equal 1 "${#COMPREPLY[@]}"
}

0 comments on commit d98c2ba

Please sign in to comment.