Skip to content

Commit b2297dc

Browse files
authored
Merge pull request #45 from tardypad/show_note
Add cat command
2 parents 959ebfe + 0729673 commit b2297dc

File tree

6 files changed

+130
-11
lines changed

6 files changed

+130
-11
lines changed

README.md

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

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

128+
### `notes cat <note-name>`
129+
130+
Displays the note
131+
128132
### `notes grep/find <pattern> | notes open`
129133

130134
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
@@ -43,7 +44,7 @@ _notes ()
4344
_alternative 'sub-commands:files:__notes_cmd' && _ret=0
4445
elif (($CURRENT == 3)); then
4546
case $words[2] in
46-
open|o|rm)
47+
open|o|rm|cat)
4748
_path_files -W "${note_dir}" && _ret=0;;
4849
new|n|ls)
4950
_path_files -W "${note_dir}" -/ && _ret=0;;

notes

Lines changed: 39 additions & 7 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,14 +159,41 @@ 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

179+
note_path=$( get_full_note_path "$note_path" )
180+
167181
$EDITOR "$note_path" < /dev/tty
168182
}
169183

184+
cat_note() {
185+
local note_path=$1
186+
187+
if [[ -z "$note_path" ]]; then
188+
printf "Cat requires a name, but none was provided."
189+
exit 1
190+
fi
191+
192+
note_path=$( get_full_note_path "$note_path" )
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'
@@ -217,11 +246,14 @@ main() {
217246
cmd="grep_notes"
218247
;;
219248
"open"|"o" )
220-
cmd="open_something"
249+
cmd="handle_multiple_notes open"
221250
;;
222251
"rm" )
223252
cmd="remove_note"
224253
;;
254+
"cat" )
255+
cmd="handle_multiple_notes cat"
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
@@ -21,7 +21,7 @@ _notes_complete_notes() {
2121
}
2222

2323
_notes_complete_commands() {
24-
local valid_commands="new find grep open ls rm"
24+
local valid_commands="new find grep open ls rm cat"
2525
COMPREPLY=($(compgen -W "${valid_commands}" -- "${1}"))
2626
}
2727

@@ -45,6 +45,9 @@ _notes()
4545
open)
4646
_notes_complete_notes "$cur"
4747
;;
48+
cat)
49+
_notes_complete_notes "$cur"
50+
;;
4851
esac
4952
else
5053
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)