Skip to content

Commit e11c7df

Browse files
feat: Add shorthand cat alias c
1 parent bf2c6ad commit e11c7df

File tree

3 files changed

+86
-80
lines changed

3 files changed

+86
-80
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ Renames a given note to destination or moves the note to directory. Name can be
147147
Removes the given note if it exists. If `-r` or `--recursive` is given, deletes the folders/notes recursively.
148148

149149
### `notes cat <note-name>`
150-
150+
Shorthand alias also available with `notes c`.
151151
Displays the note
152152

153153
### `notes grep/find <pattern> | notes open`

notes

Lines changed: 66 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@ search_filenames_and_contents() {
5656
"shopt -s nocasematch
5757
grep -il \"$*\" \"{}\" || if [[ \"{}\" =~ \"$*\" ]]; then
5858
echo \"{}\";
59-
fi" \;\
60-
)
59+
fi" \;)
6160
else
6261
find_output=$(find "$notes_dir" -type f)
6362
fi
@@ -105,12 +104,11 @@ grep_notes() {
105104

106105
generate_name() {
107106
local append_num=0
108-
local format_string="`date +$QUICKNOTE_FORMAT`"
107+
local format_string="$(date +$QUICKNOTE_FORMAT)"
109108
# Initial test has no append
110109
local resolved_name=$format_string
111-
while [[ -e "$notes_dir/$resolved_name.$NOTES_EXT" ]]
112-
do
113-
append_num=$[$append_num+1]
110+
while [[ -e "$notes_dir/$resolved_name.$NOTES_EXT" ]]; do
111+
append_num=$(($append_num + 1))
114112
resolved_name=$format_string.$append_num
115113
done
116114
printf $resolved_name
@@ -119,10 +117,10 @@ generate_name() {
119117
new_note() {
120118
local note_name="$*"
121119
if [[ $note_name == "" ]]; then
122-
note_name="$(generate_name)"
120+
note_name="$(generate_name)"
123121
fi
124122

125-
if echo "$note_name" | grep "/$" &> /dev/null; then
123+
if echo "$note_name" | grep "/$" &>/dev/null; then
126124
note_name="${note_name}/$(generate_name)"
127125
fi
128126

@@ -161,7 +159,7 @@ handle_multiple_notes() {
161159
read -d'\n' note_names
162160
while read note_name; do
163161
${cmd}_note "$note_name"
164-
done <<< "$note_names"
162+
done <<<"$note_names"
165163
else
166164
${cmd}_note "${@:2}"
167165
fi
@@ -202,7 +200,7 @@ open_note() {
202200
exit 1
203201
fi
204202

205-
note_path=$( get_full_note_path "$note_path" )
203+
note_path=$(get_full_note_path "$note_path")
206204

207205
if bash -c ": >/dev/tty" >/dev/null 2>/dev/null; then
208206
$EDITOR "$note_path" </dev/tty
@@ -212,7 +210,7 @@ open_note() {
212210
}
213211

214212
append_note() {
215-
local source_note_path=$( get_full_note_path "$1" )
213+
local source_note_path=$(get_full_note_path "$1")
216214
local to_append="${@:2}"
217215

218216
# If no note name was provided, exit
@@ -237,11 +235,11 @@ append_note() {
237235
exit 1
238236
fi
239237

240-
echo "$to_append" >> "$source_note_path"
238+
echo "$to_append" >>"$source_note_path"
241239
}
242240

243241
move_note() {
244-
local source_note_path=$( get_full_note_path "$1" )
242+
local source_note_path=$(get_full_note_path "$1")
245243
local dest_or_dir_path=$2
246244

247245
if [[ ! -e "$source_note_path" ]]; then
@@ -260,8 +258,8 @@ move_note() {
260258
return
261259
fi
262260

263-
local dest_path=$( get_full_note_path "$dest_or_dir_path" )
264-
mkdir -p "$( dirname $dest_path)"
261+
local dest_path=$(get_full_note_path "$dest_or_dir_path")
262+
mkdir -p "$(dirname $dest_path)"
265263
mv $source_note_path $dest_path
266264
}
267265

@@ -273,14 +271,14 @@ cat_note() {
273271
exit 1
274272
fi
275273

276-
note_path=$( get_full_note_path "$note_path" )
274+
note_path=$(get_full_note_path "$note_path")
277275

278276
cat "$note_path"
279277
}
280278

281279
usage() {
282-
local name=$(basename $0)
283-
cat <<EOF
280+
local name=$(basename $0)
281+
cat <<EOF
284282
$name is a command line note taking tool.
285283
286284
Usage:
@@ -294,7 +292,7 @@ Usage:
294292
$name append|a <name> [message] # Appends a note. Will use stdin if no message is given
295293
$name mv <source> <dest>|<directory> # Rename a note, or move a note when a directory is given
296294
$name rm [-r | --recursive] <name> # Remove note, or folder if -r or --recursive is given
297-
$name cat <name> # Display note
295+
$name cat|c <name> # Display note
298296
echo <name> | $name open|o # Open all note filenames piped in
299297
echo <name> | $name cat # Display all note filenames piped in
300298
$name --help # Print this usage information
@@ -307,8 +305,8 @@ EOF
307305
}
308306

309307
version() {
310-
local name=$(basename $0)
311-
cat <<EOF
308+
local name=$(basename $0)
309+
cat <<EOF
312310
$name $notes_version
313311
EOF
314312
}
@@ -326,57 +324,57 @@ main() {
326324
fi
327325

328326
case "$1" in
329-
"new"|"n" )
330-
cmd="new_note"
331-
modified=1
332-
;;
333-
"ls" )
334-
cmd="ls_notes"
335-
;;
336-
"search"|"s" )
337-
cmd="search_filenames_and_contents"
338-
;;
339-
"find"|"f" )
340-
cmd="find_notes"
341-
;;
342-
"grep"|"g" )
343-
cmd="grep_notes"
344-
;;
345-
"open"|"o" )
346-
cmd="handle_multiple_notes open"
347-
modified=1
348-
;;
349-
"append"|"a" )
350-
cmd="append_note"
351-
modified=1
352-
;;
353-
"mv" )
354-
cmd="move_note"
355-
modified=1
356-
;;
357-
"rm" )
358-
cmd="remove_note"
359-
modified=1
360-
;;
361-
"cat" )
362-
cmd="handle_multiple_notes cat"
363-
;;
364-
--help | -help | -h )
365-
cmd="usage"
366-
;;
367-
--version | -version )
368-
cmd="version"
369-
;;
370-
* )
371-
printf "$1 is not a recognized notes command.\n\n"
372-
cmd="usage"
373-
ret=1
374-
;;
327+
"new" | "n")
328+
cmd="new_note"
329+
modified=1
330+
;;
331+
"ls")
332+
cmd="ls_notes"
333+
;;
334+
"search" | "s")
335+
cmd="search_filenames_and_contents"
336+
;;
337+
"find" | "f")
338+
cmd="find_notes"
339+
;;
340+
"grep" | "g")
341+
cmd="grep_notes"
342+
;;
343+
"open" | "o")
344+
cmd="handle_multiple_notes open"
345+
modified=1
346+
;;
347+
"append" | "a")
348+
cmd="append_note"
349+
modified=1
350+
;;
351+
"mv")
352+
cmd="move_note"
353+
modified=1
354+
;;
355+
"rm")
356+
cmd="remove_note"
357+
modified=1
358+
;;
359+
"cat" | "c")
360+
cmd="handle_multiple_notes cat"
361+
;;
362+
--help | -help | -h)
363+
cmd="usage"
364+
;;
365+
--version | -version)
366+
cmd="version"
367+
;;
368+
*)
369+
printf "$1 is not a recognized notes command.\n\n"
370+
cmd="usage"
371+
ret=1
372+
;;
375373
esac
376374
shift
377375

378376
$cmd "$@"
379-
ret=$[$ret+$?]
377+
ret=$(($ret + $?))
380378

381379
# run POST_COMMAND hook when modification cmd succeeds
382380
if [ $ret -eq 0 ] && [ $modified -eq 1 ] && [ -n "$POST_COMMAND" ]; then
@@ -386,4 +384,3 @@ main() {
386384
exit $ret
387385
}
388386
main "$@"
389-

test/test-cat.bats

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,26 @@ teardown() {
1313
notes="./notes"
1414

1515
@test "Should show created note" {
16-
echo line1 >> "$NOTES_DIRECTORY/note.md"
17-
echo line2 >> "$NOTES_DIRECTORY/note.md"
16+
echo line1 >>"$NOTES_DIRECTORY/note.md"
17+
echo line2 >>"$NOTES_DIRECTORY/note.md"
1818
run $notes cat note.md
1919

2020
assert_success
2121
assert_output $'line1\nline2'
2222
}
2323

24+
@test "Should show created note when using the cat shorthand alias" {
25+
echo line1 >>"$NOTES_DIRECTORY/note.md"
26+
echo line2 >>"$NOTES_DIRECTORY/note.md"
27+
run $notes c note.md
28+
29+
assert_success
30+
assert_output $'line1\nline2'
31+
}
32+
2433
@test "Accepts names without .md to show" {
25-
echo line1 >> "$NOTES_DIRECTORY/note.md"
26-
echo line2 >> "$NOTES_DIRECTORY/note.md"
34+
echo line1 >>"$NOTES_DIRECTORY/note.md"
35+
echo line2 >>"$NOTES_DIRECTORY/note.md"
2736
run $notes cat note
2837

2938
assert_success
@@ -37,17 +46,17 @@ notes="./notes"
3746
}
3847

3948
@test "Accepts relative notes paths to show" {
40-
echo line1 >> "$NOTES_DIRECTORY/note.md"
41-
echo line2 >> "$NOTES_DIRECTORY/note.md"
49+
echo line1 >>"$NOTES_DIRECTORY/note.md"
50+
echo line2 >>"$NOTES_DIRECTORY/note.md"
4251
run $notes cat $NOTES_DIRECTORY/note.md
4352

4453
assert_success
4554
assert_output $'line1\nline2'
4655
}
4756

4857
@test "Show a file passed by pipe from find" {
49-
echo line1 >> "$NOTES_DIRECTORY/note.md"
50-
echo line2 >> "$NOTES_DIRECTORY/note.md"
58+
echo line1 >>"$NOTES_DIRECTORY/note.md"
59+
echo line2 >>"$NOTES_DIRECTORY/note.md"
5160

5261
run bash -c "$notes find | $notes cat"
5362

@@ -56,8 +65,8 @@ notes="./notes"
5665
}
5766

5867
@test "Show multiple files passed by pipe from find" {
59-
echo line1 >> "$NOTES_DIRECTORY/note1.md"
60-
echo line2 >> "$NOTES_DIRECTORY/note2.md"
68+
echo line1 >>"$NOTES_DIRECTORY/note1.md"
69+
echo line2 >>"$NOTES_DIRECTORY/note2.md"
6170

6271
run bash -c "$notes find | $notes cat"
6372

0 commit comments

Comments
 (0)