diff --git a/notes b/notes index c4cf166..35ff64b 100755 --- a/notes +++ b/notes @@ -16,7 +16,25 @@ find_notes() { if [[ $find_result == 0 && $formatted_output ]]; then echo "$formatted_output" return 0 - else + else + return 2 + fi +} + +grep_notes() { + if [ ! "$#" -gt 0 ]; then + echo "Grep requires a pattern, but none was provided." + return 1 + fi + + grep_output=$(grep -r "$notes_dir" -e "$*" 2>&1) + grep_result=$? + formatted_output=$(echo "$grep_output" | without_notes_dir) + + if [[ $grep_result == 0 && $formatted_output ]]; then + echo "$formatted_output" + return 0 + else return 2 fi } @@ -32,6 +50,7 @@ notes is a command line note taking tool. Usage: notes (new|n) # Create a new note notes find [pattern] # Search notes by filename and path + notes grep # Search notes by content notes open # Open your notes directory notes --help # Print this usage information @@ -43,7 +62,12 @@ EOF main() { ret=0 cmd="" - [ -z "$1" ] && echo "No command specified\n" && usage && exit 1 + + if [ -z "$1" ]; then + echo "No command specified\n" + usage + exit 1 + fi case "$1" in "n" | "new" ) @@ -52,6 +76,9 @@ main() { "find" ) cmd="find_notes" ;; + "grep" ) + cmd="grep_notes" + ;; "open" ) cmd="open $notes_dir" ;; diff --git a/test/test-find.bats b/test/test-find.bats index 6eb53b9..26b034e 100755 --- a/test/test-find.bats +++ b/test/test-find.bats @@ -9,7 +9,7 @@ export NOTES_DIRECTORY="$TMP_DIRECTORY" notes="./notes" -@test "Should output nothing and return non-zero if there are no notes" { +@test "Should output nothing and return non-zero if there are no notes to find" { run $notes find assert_failure @@ -17,7 +17,7 @@ notes="./notes" assert_equal $(echo $output | wc -w) 0 } -@test "Should show all notes found if no pattern is provided" { +@test "Should show all notes found if no pattern is provided to find" { touch $NOTES_DIRECTORY/note1.md touch $NOTES_DIRECTORY/note2.md @@ -27,7 +27,7 @@ notes="./notes" assert_line "note2.md" } -@test "Should show matching notes only if a pattern is provided" { +@test "Should show matching notes only if a pattern is provided to find" { touch $NOTES_DIRECTORY/match-note1.md touch $NOTES_DIRECTORY/hide-note2.md @@ -38,7 +38,7 @@ notes="./notes" refute_line "hide-note2.md" } -@test "Should match notes case insensitively" { +@test "Should match notes case insensitively with find" { touch $NOTES_DIRECTORY/MATCH-note1.md touch $NOTES_DIRECTORY/hide-note2.md @@ -49,7 +49,7 @@ notes="./notes" refute_line "hide-note2.md" } -@test "Should match subdirectory or file names" { +@test "Should match subdirectory or file names with find" { touch "$NOTES_DIRECTORY/hide-note.md" mkdir "$NOTES_DIRECTORY/match-directory" touch "$NOTES_DIRECTORY/match-directory/note.md" diff --git a/test/test-grep.bats b/test/test-grep.bats new file mode 100755 index 0000000..b25344b --- /dev/null +++ b/test/test-grep.bats @@ -0,0 +1,28 @@ +#!./libs/bats/bin/bats + +load 'libs/bats-support/load' +load 'libs/bats-assert/load' + +# Set up a directory for our notes +TMP_DIRECTORY=$(mktemp -d) +export NOTES_DIRECTORY="$TMP_DIRECTORY" + +notes="./notes" + +@test "Should complain and ask for a pattern if not is provided to grep" { + run $notes grep + + assert_failure + assert_line "Grep requires a pattern, but none was provided." +} + +@test "Should match only the files containing the given pattern when grepping" { + echo "my-pattern" > $NOTES_DIRECTORY/matching-node.md + echo "some-other-pattern" > $NOTES_DIRECTORY/non-matching-node.md + + run $notes grep my-pattern + + assert_success + assert_line "matching-node.md:my-pattern" + refute_line "non-matching-node.md" +} \ No newline at end of file