Skip to content

Commit 4ff6a8c

Browse files
committed
Include subdirectories in notes ls output
1 parent 301305c commit 4ff6a8c

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

notes

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,19 @@ without_notes_dir() {
1919
}
2020

2121
ls_notes() {
22-
ls_output=$(ls -p "$notes_dir/$*" 2>&1)
23-
ls_result=$?
22+
local ls_output=$(ls -p "$notes_dir/$*" 2>&1)
23+
local ls_result=$?
24+
local formatted_output
2425

25-
if [[ $ls_result == 0 && "$ls_output" ]]; then
26-
printf "$ls_output\n"
26+
if [ $# -gt 0 ]; then
27+
local path_prefix=$(printf "${*%/}" | sed -e 's/[]\/$*.^|[]/\\&/g')
28+
formatted_output=$(printf "$ls_output" | sed -E "s/^/$path_prefix\//")
29+
else
30+
formatted_output=$ls_output
31+
fi
32+
33+
if [[ $ls_result == 0 && "$formatted_output" ]]; then
34+
printf "$formatted_output\n"
2735
return 0
2836
else
2937
return 2

test/test-ls.bats

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,5 +66,16 @@ notes="./notes"
6666
assert_success
6767
refute_line "hide-note.md"
6868
refute_line "match-dir/"
69-
assert_line "match-note1.md"
69+
assert_line "match-dir/match-note1.md"
7070
}
71+
72+
@test "Should list contents of a subdirectory if the pattern includes slashes" {
73+
mkdir -p $NOTES_DIRECTORY/match-dir/child-dir
74+
touch $NOTES_DIRECTORY/match-dir/child-dir/match-note1.md
75+
76+
run $notes ls match-dir/child-dir/
77+
78+
assert_success
79+
assert_line "match-dir/child-dir/match-note1.md"
80+
}
81+

0 commit comments

Comments
 (0)