Skip to content

Commit 095213d

Browse files
committed
add cat command
1 parent e44434d commit 095213d

File tree

6 files changed

+123
-4
lines changed

6 files changed

+123
-4
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,10 @@ Opens a given note in your `$EDITOR`. Name can be an absolute path, or a relativ
123123

124124
Removes the given note if it exists. If `-r` or `--recursive` is given, deletes the folders/notes recursively.
125125

126+
### `notes cat <note-name>`
127+
128+
Displays the note
129+
126130
### `notes grep/find <pattern> | notes open`
127131

128132
Combine these together! This opens each matching note in your `$EDITOR` in turn.

_notes

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ __notes_cmd ()
2424
search:'[pattern] Search notes by filename or content'
2525
open:'<name> Open a notes for editing by full name'
2626
rm:'[-r | --recursive] <name> Remove note, or folder if -r or --recursive is given]'
27+
cat:'<name> Display a note by name'
2728
--help:'Show usage'
2829
)
2930
_describe -t sub-commands 'sub commands' list && _ret=0
@@ -38,7 +39,7 @@ _notes ()
3839
_alternative 'sub-commands:files:__notes_cmd' && _ret=0
3940
elif (($CURRENT == 3)); then
4041
case $words[2] in
41-
open|o|rm)
42+
open|o|rm|cat)
4243
_path_files -W "${note_dir}" && _ret=0;;
4344
new|n|ls)
4445
_path_files -W "${note_dir}" -/ && _ret=0;;

notes

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,33 @@ open_note() {
167167
$EDITOR "$note_path" < /dev/tty
168168
}
169169

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
182+
}
183+
184+
cat_note() {
185+
local note_path=$1
186+
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"
192+
fi
193+
194+
cat "$note_path"
195+
}
196+
170197
usage() {
171198
cat <<EOF
172199
notes is a command line note taking tool.
@@ -180,7 +207,9 @@ Usage:
180207
notes open|o # Open your notes directory
181208
notes open|o <name> # Open a note for editing by full name
182209
notes rm [-r | --recursive] <name> # Remove note, or folder if -r or --recursive is given
210+
notes cat <name> # Display note
183211
echo <name> | notes open|o # Open all note filenames piped in
212+
echo <name> | notes cat # Display all note filenames piped in
184213
notes --help # Print this usage information
185214
186215
'command|c' means you can use 'command' or the equivalent shorthand alias 'c'
@@ -222,6 +251,9 @@ main() {
222251
"rm" )
223252
cmd="remove_note"
224253
;;
254+
"cat" )
255+
cmd="cat_something"
256+
;;
225257
--help | -help | -h )
226258
cmd="usage"
227259
;;

notes.1

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ is a simple command line tool that makes note taking simpler.
1010
When run without a command, the help screen will appear and will offer a brief
1111
description of availible commands.
1212

13-
\fBnotes\fR will run the default editor for your distribution when opening a
13+
\fBnotes\fR will run the default editor for your distribution when opening a
1414
note. You can set $EDITOR in your shell to override it.
1515
\fBnotes\fR uses a configuration file located at \fB~/.config/notes/config\fR.
1616
This file overrides any settings in your rc file.
@@ -32,7 +32,7 @@ Searches the contents of all notes for the given pattern. Returns all matches.
3232
Combination of find and grep.
3333
.TP
3434
.BR ls " " \fR[\fIDIRECTORY\fR]
35-
Lists all notes within \fIDIRECTORY\fR. Lists contents of $NOTES_DIRECTORY if
35+
Lists all notes within \fIDIRECTORY\fR. Lists contents of $NOTES_DIRECTORY if
3636
no path is provided.
3737
.TP
3838
.BR open ", " o
@@ -44,6 +44,10 @@ absolute path or relative to $NOTES_DIRECTORY.
4444
.TP
4545
.BR rm " "\fR[\fB\-r\fR | \fB\-\-recursive\fR] " "\fINAME\fR
4646
Removes \fINAME\fR. If \-r or \-\-recursive is given, folders will be removed.
47+
.TP
48+
.BR cat " " \fINAME\fR
49+
Display note \fINAME\fR. \fINAME\fR can either be an absolute path or relative
50+
to $NOTES_DIRECTORY.
4751
.SH AUTHORS
4852
Notes was created by Tim Perry, who is still the maintainer. Numerous
4953
contributions have been submitted via pull requests on github.

notes.bash_completion

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ _notes_complete_notes() {
1616
}
1717

1818
_notes_complete_commands() {
19-
local valid_commands="new find grep open ls rm"
19+
local valid_commands="new find grep open ls rm cat"
2020
COMPREPLY=($(compgen -W "${valid_commands}" -- "${1}"))
2121
}
2222

@@ -40,6 +40,9 @@ _notes()
4040
open)
4141
_notes_complete_notes "$cur"
4242
;;
43+
cat)
44+
_notes_complete_notes "$cur"
45+
;;
4346
esac
4447
else
4548
compopt +o nospace

test/test-cat.bats

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#!./test/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 show created note" {
18+
echo line1 >> "$NOTES_DIRECTORY/note.md"
19+
echo line2 >> "$NOTES_DIRECTORY/note.md"
20+
run $notes cat note.md
21+
22+
assert_success
23+
assert_output $'line1\nline2'
24+
}
25+
26+
@test "Accepts names without .md to show" {
27+
echo line1 >> "$NOTES_DIRECTORY/note.md"
28+
echo line2 >> "$NOTES_DIRECTORY/note.md"
29+
run $notes cat note
30+
31+
assert_success
32+
assert_output $'line1\nline2'
33+
}
34+
35+
@test "Should fail to show non-existent note" {
36+
run $notes cat note
37+
38+
assert_failure
39+
}
40+
41+
@test "Accepts relative notes paths to show" {
42+
echo line1 >> "$NOTES_DIRECTORY/note.md"
43+
echo line2 >> "$NOTES_DIRECTORY/note.md"
44+
run $notes cat $NOTES_DIRECTORY/note.md
45+
46+
assert_success
47+
assert_output $'line1\nline2'
48+
}
49+
50+
@test "Show a file passed by pipe from find" {
51+
echo line1 >> "$NOTES_DIRECTORY/note.md"
52+
echo line2 >> "$NOTES_DIRECTORY/note.md"
53+
54+
run bash -c "$notes find | $notes cat"
55+
56+
assert_success
57+
assert_output $'line1\nline2'
58+
}
59+
60+
@test "Show multiple files passed by pipe from find" {
61+
echo line1 >> "$NOTES_DIRECTORY/note.md"
62+
echo line2 >> "$NOTES_DIRECTORY/note2.md"
63+
64+
run bash -c "$notes find | $notes cat"
65+
66+
assert_success
67+
assert_output $'line1\nline2'
68+
}
69+
70+
@test "Should complain and ask for a name if one is not provided to show" {
71+
run $notes cat
72+
73+
assert_failure
74+
assert_line "Cat requires a name, but none was provided."
75+
}

0 commit comments

Comments
 (0)