Skip to content

Commit e297817

Browse files
authored
Merge pull request #19 from jacobmischka/ls-2
Add better `ls` command
2 parents d98c2ba + 90d3f1a commit e297817

File tree

3 files changed

+91
-1
lines changed

3 files changed

+91
-1
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ This demo uses zsh, vim and dropbox, but don't panic, that's just me. `notes` wi
99

1010
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.
1111

12-
You want to take notes.
12+
You want to take notes.
1313

1414
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.
1515

@@ -47,6 +47,10 @@ curl https://cdn.rawgit.com/pimterry/notes/v0.1.1/notes.bash_completion > /usr/s
4747

4848
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`.
4949

50+
### `notes ls <directory-pattern>`
51+
52+
Lists notes and subdirectories non-recursively.
53+
5054
### `notes find <part-of-a-note-name>`
5155

5256
Searches note filenames and paths for the given string, and returns all the matches. Shorthand alias also available with `notes f`.

notes

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,18 @@ without_notes_dir() {
1313
cat | sed -e "s/^$escaped_notes_dir//g" | sed -E "s/^\/+//g"
1414
}
1515

16+
ls_notes() {
17+
ls_output=$(ls -p "$notes_dir/$*" 2>&1)
18+
ls_result=$?
19+
20+
if [[ $ls_result == 0 && "$ls_output" ]]; then
21+
printf "$ls_output\n"
22+
return 0
23+
else
24+
return 2
25+
fi
26+
}
27+
1628
find_notes() {
1729
find_output=$(find "$notes_dir" -ipath "$notes_dir/*$**" -type f 2>&1)
1830
find_result=$?
@@ -102,6 +114,7 @@ notes is a command line note taking tool.
102114
103115
Usage:
104116
notes new|n <name> # Create a new note
117+
notes ls <pattern> # List notes by path
105118
notes find|f [pattern] # Search notes by filename and path
106119
notes grep|g <pattern> # Search notes by content
107120
notes open|o # Open your notes directory
@@ -131,6 +144,9 @@ main() {
131144
"new"|"n" )
132145
cmd="new_note"
133146
;;
147+
"ls" )
148+
cmd="ls_notes"
149+
;;
134150
"find"|"f" )
135151
cmd="find_notes"
136152
;;

test/test-ls.bats

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#!./libs/bats/bin/bats
2+
3+
load 'libs/bats-support/load'
4+
load 'libs/bats-assert/load'
5+
load 'helpers'
6+
7+
setup() {
8+
setupNotesEnv
9+
}
10+
11+
teardown() {
12+
teardownNotesEnv
13+
}
14+
15+
notes="./notes"
16+
17+
@test "Should output nothing and return non-zero if there are no notes to list" {
18+
run $notes ls
19+
20+
assert_failure
21+
echo $output
22+
assert_equal $(echo $output | wc -w) 0
23+
}
24+
25+
@test "Should list all notes in notes directory if no pattern is provided to find" {
26+
touch $NOTES_DIRECTORY/note1.md
27+
touch $NOTES_DIRECTORY/note2.md
28+
29+
run $notes ls
30+
assert_success
31+
assert_line "note1.md"
32+
assert_line "note2.md"
33+
}
34+
35+
@test "Should list subdirectories with trailing slash" {
36+
touch $NOTES_DIRECTORY/match-note1.md
37+
mkdir $NOTES_DIRECTORY/match-dir
38+
39+
run $notes ls
40+
41+
assert_success
42+
assert_line "match-note1.md"
43+
assert_line "match-dir/"
44+
}
45+
46+
@test "Should not list contents of subdirectories without pattern" {
47+
touch $NOTES_DIRECTORY/match-note1.md
48+
mkdir $NOTES_DIRECTORY/match-dir
49+
touch $NOTES_DIRECTORY/match-dir/hide-note.md
50+
51+
run $notes ls
52+
53+
assert_success
54+
assert_line "match-note1.md"
55+
assert_line "match-dir/"
56+
refute_line "hide-note.md"
57+
}
58+
59+
@test "Should list contents of subdirectory given in pattern" {
60+
touch $NOTES_DIRECTORY/hide-note.md
61+
mkdir $NOTES_DIRECTORY/match-dir
62+
touch $NOTES_DIRECTORY/match-dir/match-note1.md
63+
64+
run $notes ls match-dir
65+
66+
assert_success
67+
refute_line "hide-note.md"
68+
refute_line "match-dir/"
69+
assert_line "match-note1.md"
70+
}

0 commit comments

Comments
 (0)