diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..1431bfa --- /dev/null +++ b/.editorconfig @@ -0,0 +1,8 @@ +root = true + +[*] +indent_style = space +indent_size = 4 + +[*.bats] +indent_size = 2 diff --git a/README.md b/README.md index 2493acb..7c631cc 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ This demo uses zsh, vim and dropbox, but don't panic, that's just me. `notes` wi You already have a tool that backs up and syncs your data (be it Dropbox, iCloud, Seafile or whatever). You already have a text editor on your desktop, your laptops, your phone and that tablet you've forgotten about. -You want to take notes. +You want to take notes. You could use a web X.0 note taking app that reimplements all of that from scratch (poorly). You could tie yourself to a tool that holds all your data for you in its own brand-new format, locks you into its (often bloated) UI, and then steadily removes features unless you start paying (hey Evernote). You don't have to. @@ -47,6 +47,10 @@ curl https://cdn.rawgit.com/pimterry/notes/v0.1.1/notes.bash_completion > /usr/s Opens your `$EDITOR` of choice for a new note, with the given name. The name can include slashes, if you want to put your note in a subfolder. Shorthand alias also available with `notes n`. +### `notes ls ` + +Lists notes and subdirectories non-recursively. + ### `notes find ` Searches note filenames and paths for the given string, and returns all the matches. Shorthand alias also available with `notes f`. diff --git a/notes b/notes index f066de8..ac5c24f 100755 --- a/notes +++ b/notes @@ -13,6 +13,18 @@ without_notes_dir() { cat | sed -e "s/^$escaped_notes_dir//g" | sed -E "s/^\/+//g" } +ls_notes() { + ls_output=$(ls -p "$notes_dir/$*" 2>&1) + ls_result=$? + + if [[ $ls_result == 0 && "$ls_output" ]]; then + printf "$ls_output\n" + return 0 + else + return 2 + fi +} + find_notes() { find_output=$(find "$notes_dir" -ipath "$notes_dir/*$**" -type f 2>&1) find_result=$? @@ -102,6 +114,7 @@ notes is a command line note taking tool. Usage: notes new|n # Create a new note + notes ls # List notes by path notes find|f [pattern] # Search notes by filename and path notes grep|g # Search notes by content notes open|o # Open your notes directory @@ -131,6 +144,9 @@ main() { "new"|"n" ) cmd="new_note" ;; + "ls" ) + cmd="ls_notes" + ;; "find"|"f" ) cmd="find_notes" ;; diff --git a/test/test-ls.bats b/test/test-ls.bats new file mode 100644 index 0000000..c02bbda --- /dev/null +++ b/test/test-ls.bats @@ -0,0 +1,70 @@ +#!./libs/bats/bin/bats + +load 'libs/bats-support/load' +load 'libs/bats-assert/load' +load 'helpers' + +setup() { + setupNotesEnv +} + +teardown() { + teardownNotesEnv +} + +notes="./notes" + +@test "Should output nothing and return non-zero if there are no notes to list" { + run $notes ls + + assert_failure + echo $output + assert_equal $(echo $output | wc -w) 0 +} + +@test "Should list all notes in notes directory if no pattern is provided to find" { + touch $NOTES_DIRECTORY/note1.md + touch $NOTES_DIRECTORY/note2.md + + run $notes ls + assert_success + assert_line "note1.md" + assert_line "note2.md" +} + +@test "Should list subdirectories with trailing slash" { + touch $NOTES_DIRECTORY/match-note1.md + mkdir $NOTES_DIRECTORY/match-dir + + run $notes ls + + assert_success + assert_line "match-note1.md" + assert_line "match-dir/" +} + +@test "Should not list contents of subdirectories without pattern" { + touch $NOTES_DIRECTORY/match-note1.md + mkdir $NOTES_DIRECTORY/match-dir + touch $NOTES_DIRECTORY/match-dir/hide-note.md + + run $notes ls + + assert_success + assert_line "match-note1.md" + assert_line "match-dir/" + refute_line "hide-note.md" +} + +@test "Should list contents of subdirectory given in pattern" { + touch $NOTES_DIRECTORY/hide-note.md + mkdir $NOTES_DIRECTORY/match-dir + touch $NOTES_DIRECTORY/match-dir/match-note1.md + + run $notes ls match-dir + + assert_success + refute_line "hide-note.md" + refute_line "match-dir/" + assert_line "match-note1.md" +}