Skip to content

Commit

Permalink
Add better ls command
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobmischka committed Dec 6, 2016
1 parent d98c2ba commit 00f49f9
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 1 deletion.
8 changes: 8 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
root = true

[*]
indent_style = space
indent_size = 4

[*.bats]
indent_size = 2
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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 <directory-pattern>`

Lists notes and subdirectories non-recursively.

### `notes find <part-of-a-note-name>`

Searches note filenames and paths for the given string, and returns all the matches. Shorthand alias also available with `notes f`.
Expand Down
16 changes: 16 additions & 0 deletions notes
Original file line number Diff line number Diff line change
Expand Up @@ -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=$?
Expand Down Expand Up @@ -102,6 +114,7 @@ notes is a command line note taking tool.
Usage:
notes new|n <name> # Create a new note
notes ls <pattern> # List notes by path
notes find|f [pattern] # Search notes by filename and path
notes grep|g <pattern> # Search notes by content
notes open|o # Open your notes directory
Expand Down Expand Up @@ -131,6 +144,9 @@ main() {
"new"|"n" )
cmd="new_note"
;;
"ls" )
cmd="ls_notes"
;;
"find"|"f" )
cmd="find_notes"
;;
Expand Down
70 changes: 70 additions & 0 deletions test/test-ls.bats
Original file line number Diff line number Diff line change
@@ -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"
}

0 comments on commit 00f49f9

Please sign in to comment.