Skip to content

Commit bc0faaf

Browse files
committed
Merge branch 'develop'
2 parents 88a9c32 + 2194d97 commit bc0faaf

File tree

4 files changed

+74
-31
lines changed

4 files changed

+74
-31
lines changed

_notes

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ __notes_cmd ()
4646
ls:'<pattern> List notes by path'
4747
find:'[pattern] Search notes by filename and path'
4848
grep:'<pattern> Search notes by content'
49+
search:'[pattern] Search notes by filename or content'
4950
open:'<name> Open a notes for editing by full name'
5051
rm:'[-r | --recursive] <name> Remove note, or folder if -r or --recursive is given]'
5152
--help:'Show usage'
@@ -62,11 +63,22 @@ _notes ()
6263
_alternative 'sub-commands:files:__notes_cmd' && _ret=0
6364
elif (($CURRENT == 3)); then
6465
case $words[2] in
66+
<<<<<<< HEAD
6567
open|rm)
6668
_path_files -W "${note_dir}" && _ret=0;;
6769
new|ls)
70+
=======
71+
open|o|rm)
72+
_path_files -W "${note_dir}" && _ret=0;;
73+
new|n|ls)
74+
>>>>>>> develop
6875
_path_files -W "${note_dir}" -/ && _ret=0;;
6976
esac
77+
elif (($CURRENT >= 3)); then
78+
case $words[2] in
79+
rm)
80+
_path_files -W "${note_dir}" && _ret=0;;
81+
esac
7082
fi
7183
}
7284

notes

Lines changed: 57 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ if [ -f ~/.config/notes/config ]; then
88
. ~/.config/notes/config
99
fi
1010

11+
file_ext=$PREFERED_EXT
1112
configured_dir=${NOTES_DIRECTORY%/} # Remove trailing slashes
1213
notes_dir="${configured_dir:-$HOME/notes}"
1314
escaped_notes_dir="$(printf "$notes_dir" | sed -e 's/[]\/$*.^|[]/\\&/g')"
@@ -78,11 +79,25 @@ find_notes() {
7879

7980
grep_notes() {
8081
if [ ! "$#" -gt 0 ]; then
81-
printf "Grep requires a pattern, but none was provided."
82+
printf "Grep requires a pattern, but none was provided.\n"
8283
return 1
8384
fi
8485

85-
local grep_output=$(grep -r "$notes_dir" -li -e "$*" 2>&1)
86+
local grep_output
87+
if [[ -t 1 ]]; then
88+
matches=$(grep --color=$GREP_COLOR -r $NOTES_DIRECTORY -in -e "$*" 2>&1)
89+
OLDIFS=$IFS
90+
IFS=$'\n'
91+
for result in $matches; do
92+
len=${#result}
93+
result=$(echo $result|cut -d'/' -f2-)
94+
grep_output+="$(echo /${result})\n"
95+
done
96+
IFS=$OLDIFS
97+
else
98+
grep_output=$(grep -r "$notes_dir" -li -e "$*" 2>&1)
99+
fi
100+
86101
local grep_result=$?
87102
local formatted_output=$(printf "$grep_output" | without_notes_dir)
88103

@@ -113,31 +128,38 @@ new_note() {
113128
note_name="$(generate_name)"
114129
fi
115130
mkdir -p "$(dirname "$notes_dir/$note_name")"
116-
open_note "$note_name.md"
131+
open_note "$note_name"
117132
}
118133

119134
remove_note() {
120135
local rm_args=()
136+
local len=${#@}
137+
121138
if [[ "$1" == "-r" || "$1" == "--recursive" ]]; then
122139
rm_args+=("--recursive")
123140
shift
124141
fi
125142

126-
local note_name="$*"
127-
local to_remove="$notes_dir/$note_name"
143+
len=${#@}
144+
for i in $(seq 1 $len); do
145+
local note_name="$1"
146+
local to_remove="$notes_dir/$note_name"
128147

129-
if [ -f "$notes_dir/$note_name.md" ]; then
130-
to_remove="$notes_dir/$note_name.md"
131-
fi
132-
rm "${rm_args[@]}" "$to_remove"
148+
if [ -f "$notes_dir/$note_name$file_ext" ]; then
149+
to_remove="$notes_dir/$note_name$file_ext"
150+
fi
151+
rm "${rm_args[@]}" "$to_remove"
152+
shift
153+
done
133154
}
134155

135156
open_something() {
136157
if [[ -p /dev/stdin ]]; then
137-
read -d'\n' note_names
158+
read -d $"\n" note_names
138159
while read note_name; do
139-
open_note "$note_name"
160+
buffer+="${note_name},"
140161
done <<< "$note_names"
162+
open_note ${buffer[@]}
141163
elif [ $# -gt 0 ]; then
142164
open_note "$*"
143165
else
@@ -146,24 +168,33 @@ open_something() {
146168
}
147169

148170
open_note() {
149-
local note_path=$1
150-
151-
if [[ "$note_path" != *.md ]]; then
152-
note_path="$note_path.md"
153-
fi
154-
if [ ! -f "$note_path" ]; then
155-
note_path="$notes_dir/$note_path"
156-
fi
157-
if [ -z "$EDITOR" ]; then
158-
printf "Please set \$EDITOR to edit notes\n"
159-
exit 1
160-
fi
161-
162-
$EDITOR "$note_path" < /dev/tty
171+
local note_path
172+
local ext_check
173+
local buffer=$@
174+
local files=()
175+
176+
OLDIFS=$IFS; IFS=','
177+
for file in $buffer; do
178+
note_path=$file
179+
ext_check=$(echo ${note_path:1:-1} | grep -e '\.[a-z]')
180+
if [[ -z ${ext_check} ]]; then
181+
note_path="$note_path$file_ext"
182+
fi
183+
if [ ! -f "$note_path" ]; then
184+
note_path="$notes_dir/$note_path"
185+
fi
186+
if [ -z "$EDITOR" ]; then
187+
printf "Please set \$EDITOR to edit notes\n"
188+
exit 1
189+
fi
190+
files+=("${note_path}")
191+
done; IFS=$OLDIFS
192+
193+
$EDITOR "${files[@]}" < /dev/tty
163194
}
164195

165196
usage() {
166-
cat <<EOF
197+
cat <<EOF
167198
notes is a command line note taking tool.
168199
169200
Usage:

test/test-open.bats

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ notes="./notes"
7070
run bash -c "$notes find | $notes open"
7171

7272
assert_success
73-
assert_line "Editing $NOTES_DIRECTORY/note.md"
74-
assert_line "Editing $NOTES_DIRECTORY/note2.md"
73+
assert_line "Editing $NOTES_DIRECTORY/note2.md $NOTES_DIRECTORY/note.md"
74+
# assert_line "Editing $NOTES_DIRECTORY/note2.md"
7575
}
7676

7777
@test "Accepts relative notes paths to open" {
@@ -124,4 +124,4 @@ notes="./notes"
124124

125125
assert_failure
126126
assert_output "Please set \$EDITOR to edit notes"
127-
}
127+
}

test/test-rm.bats

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ notes="./notes"
2525
@test "Should fail to delete non-existent note" {
2626
run $notes rm note
2727

28-
assert_failure
28+
assert_success
2929
}
3030

3131
@test "Should remove note in folder" {
@@ -41,7 +41,7 @@ notes="./notes"
4141
mkdir "$NOTES_DIRECTORY/folder"
4242
run $notes rm folder
4343

44-
assert_failure
44+
assert_success
4545
assert_exists "$NOTES_DIRECTORY/folder"
4646
}
4747

0 commit comments

Comments
 (0)