forked from pimterry/notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request pimterry#5 from ecksun/bash_completion
Add bash completion support
- Loading branch information
Showing
3 changed files
with
119 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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[@]}" | ||
} |