Skip to content

Commit 0729673

Browse files
committed
put handling of stdin and note_path in common functions
1 parent 095213d commit 0729673

File tree

1 file changed

+27
-27
lines changed

1 file changed

+27
-27
lines changed

notes

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -137,20 +137,20 @@ remove_note() {
137137
rm "${rm_args[@]}" "$to_remove"
138138
}
139139

140-
open_something() {
140+
handle_multiple_notes() {
141+
local cmd=$1
142+
141143
if [[ -p /dev/stdin ]]; then
142144
read -d'\n' note_names
143145
while read note_name; do
144-
open_note "$note_name"
146+
${cmd}_note "$note_name"
145147
done <<< "$note_names"
146-
elif [ $# -gt 0 ]; then
147-
open_note "$*"
148148
else
149-
open $notes_dir
149+
${cmd}_note "${@:2}"
150150
fi
151151
}
152152

153-
open_note() {
153+
get_full_note_path() {
154154
local note_path=$1
155155

156156
if [[ "$note_path" != *.$NOTES_EXT ]]; then
@@ -159,38 +159,38 @@ open_note() {
159159
if [ ! -f "$note_path" ]; then
160160
note_path="$notes_dir/$note_path"
161161
fi
162+
163+
echo "$note_path"
164+
}
165+
166+
open_note() {
167+
local note_path=$1
168+
169+
if [[ -z "$note_path" ]]; then
170+
open $notes_dir
171+
return
172+
fi
173+
162174
if [ -z "$EDITOR" ]; then
163175
printf "Please set \$EDITOR to edit notes\n"
164176
exit 1
165177
fi
166178

167-
$EDITOR "$note_path" < /dev/tty
168-
}
179+
note_path=$( get_full_note_path "$note_path" )
169180

170-
cat_something() {
171-
if [[ -p /dev/stdin ]]; then
172-
read -d'\n' note_names
173-
while read note_name; do
174-
cat_note "$note_name"
175-
done <<< "$note_names"
176-
elif [ $# -gt 0 ]; then
177-
cat_note "$*"
178-
else
179-
printf "Cat requires a name, but none was provided."
180-
return 1
181-
fi
181+
$EDITOR "$note_path" < /dev/tty
182182
}
183183

184184
cat_note() {
185185
local note_path=$1
186186

187-
if [[ "$note_path" != *.$NOTES_EXT ]]; then
188-
note_path="$note_path.$NOTES_EXT"
189-
fi
190-
if [ ! -f "$note_path" ]; then
191-
note_path="$notes_dir/$note_path"
187+
if [[ -z "$note_path" ]]; then
188+
printf "Cat requires a name, but none was provided."
189+
exit 1
192190
fi
193191

192+
note_path=$( get_full_note_path "$note_path" )
193+
194194
cat "$note_path"
195195
}
196196

@@ -246,13 +246,13 @@ main() {
246246
cmd="grep_notes"
247247
;;
248248
"open"|"o" )
249-
cmd="open_something"
249+
cmd="handle_multiple_notes open"
250250
;;
251251
"rm" )
252252
cmd="remove_note"
253253
;;
254254
"cat" )
255-
cmd="cat_something"
255+
cmd="handle_multiple_notes cat"
256256
;;
257257
--help | -help | -h )
258258
cmd="usage"

0 commit comments

Comments
 (0)