Skip to content

Commit aaa0d33

Browse files
committed
Added append command
1 parent f7f42a5 commit aaa0d33

File tree

8 files changed

+123
-6
lines changed

8 files changed

+123
-6
lines changed

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,11 @@ By default your notes live in ~/notes, but you can change that to anywhere you l
8989

9090
To get started with you'll want to set `$EDITOR` to your favourite text editor, and probably `$NOTES_DIRECTORY` to the directory in which you'd like to use to store your notes (this defaults to `~/notes`). You'll typically want to set these as environment variables in your `.bashrc`, `.zshrc`, or similar. Remember to use `export` command when setting environment variables on the command line in Linux.
9191

92-
There are also more complex options available. You can set any configuration properties either in the environment, or in a config file (stored in `~/.config/notes/config`), with settings in config overriding those in your environment. This allows you to configure a different `$EDITOR` for notes to everything else, if you like. The config file is a good choice for more complex set ups, but probably not worth worrying about to start with. We've included an example config in this repo for you ([config](config)) that you can copy if you like.
92+
There are also more complex options available. You can set any configuration properties either in the environment, or in a config file (stored in `~/.config/notes/config`), with settings in config overriding those in your environment. This allows you to configure a different `$EDITOR` for notes to everything else, if you like. The config file is a good choice for more complex set ups, but probably not worth worrying about to start with. We've included an example config in this repo for you ([config](config)) that you can copy if you like.
9393

9494
### What are the configuration options?
9595

96-
* `QUICKNOTE_FORMAT` changes the way that quicknotes are generated. The string formatted using the `date` command.
96+
* `QUICKNOTE_FORMAT` changes the way that quicknotes are generated. The string formatted using the `date` command.
9797
* `NOTES_EXT` changes the default extension that notes are saved with.
9898
* `NOTES_DIRECTORY` changes the directory in which notes are stored.
9999
* `EDITOR` can also be overriden here, for `notes` only.
@@ -133,6 +133,12 @@ Opens a given note in your `$EDITOR`. Name can be an absolute path, or a relativ
133133

134134
If no file-suffix is given in `note-name`, the notes will attempt to open `note-name.md` (or whatever your default suffix is set to). However, if the note-name is given an suffix, the default suffix will not be appended (e.g. `notes open note-name.txt` will open `note-name.txt`; not `note-name.md` or `note-name.txt.md`).
135135

136+
### `notes append <note-name> [message]`
137+
138+
Appends a given note with the test `message` from the command line. If no note yet exists, a new note of <note-name> will be created. This command also accepts stdin
139+
via piping. An example would be `echo "hello" | notes append <note-name>`
140+
Shorthand alias also available with `notes a`.
141+
136142
### `notes mv <note-name> <destination>|<directory>`
137143

138144
Renames a given note to destination or moves the note to directory. Name can be an absolute path, or a relative path in your notes (.md suffix optional). Destination and directory have to be a relative path in your notes.

_notes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ __notes_cmd ()
2323
grep:'<pattern> Search notes by content'
2424
search:'[pattern] Search notes by filename or content'
2525
open:'<name> Open a notes for editing by full name'
26+
append:'<name> [message] '
2627
rm:'[-r | --recursive] <name> Remove note, or folder if -r or --recursive is given]'
2728
cat:'<name> Display a note by name'
2829
--version:'Show version'

notes

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,10 +211,39 @@ open_note() {
211211
$EDITOR "$note_path" < /dev/tty
212212
}
213213

214+
append_note() {
215+
local source_note_path=$( get_full_note_path "$1" )
216+
local to_append="${@:2}"
217+
218+
# If no note name was provided, exit
219+
if [[ -z "$1" ]]; then
220+
printf "Append requires a name, but none was provided.\n"
221+
exit 1
222+
fi
223+
224+
# If note doesn't exist, make sure the directory does
225+
if [[ ! -e "$source_note_path" ]]; then
226+
mkdir -p "$(dirname "$source_note_path")"
227+
fi
228+
229+
# if to_append is empty, check stdin
230+
if [[ -z "$to_append" ]] && [[ -p /dev/stdin ]]; then
231+
to_append=$(cat)
232+
fi
233+
234+
# If to_append is *still* empty, report an error
235+
if [[ -z "$to_append" ]]; then
236+
printf "No text was provided to append\n"
237+
exit 1
238+
fi
239+
240+
echo "$to_append" >> "$source_note_path"
241+
}
242+
214243
move_note() {
215244
local source_note_path=$( get_full_note_path "$1" )
216245
local dest_or_dir_path=$2
217-
246+
218247
if [[ ! -e "$source_note_path" ]]; then
219248
printf "mv requires a source note that exists\n"
220249
exit 1
@@ -224,7 +253,7 @@ move_note() {
224253
printf "mv requires a destination name or folder\n"
225254
exit 1
226255
fi
227-
256+
228257
dir_path="$notes_dir/$dest_or_dir_path"
229258
if [[ -d "$dir_path" ]]; then
230259
mv $source_note_path $dir_path
@@ -262,6 +291,7 @@ Usage:
262291
$name search|s [pattern] # Search notes by filename or content
263292
$name open|o # Open your notes directory
264293
$name open|o <name> # Open a note for editing by full name
294+
$name append|a <name> [message] # Appends a note. Will use stdin if no message is given
265295
$name mv <source> <dest>|<directory> # Rename a note, or move a note when a directory is given
266296
$name rm [-r | --recursive] <name> # Remove note, or folder if -r or --recursive is given
267297
$name cat <name> # Display note
@@ -312,6 +342,9 @@ main() {
312342
"open"|"o" )
313343
cmd="handle_multiple_notes open"
314344
;;
345+
"append"|"a" )
346+
cmd="append_note"
347+
;;
315348
"mv" )
316349
cmd="move_note"
317350
;;

notes.1

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ Opens $NOTES_DIRECTORY in your file explorer.
4242
Opens note \fINAME\fR using your configured editor. \fINAME\fR can either be an
4343
absolute path or relative to $NOTES_DIRECTORY.
4444
.TP
45+
.BR append ", " a " "\fINAME\fR " [" \fIMESSAGE\fR]
46+
Appends \fINAME\fR with text \fIMESSAGE\fR if given. If no \fIMESSAGE\fR is
47+
defined, it will be taken from stdin. Stdin input does not work
48+
in interactive mode (The input must be piped in).
49+
.TP
4550
.BR rm " "\fR[\fB\-r\fR | \fB\-\-recursive\fR] " "\fINAME\fR
4651
Removes \fINAME\fR. If \-r or \-\-recursive is given, folders will be removed.
4752
.TP

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 cat search"
24+
local valid_commands="new find grep open ls rm cat append search"
2525
COMPREPLY=($(compgen -W "${valid_commands}" -- "${1}"))
2626
}
2727

@@ -42,6 +42,9 @@ _notes()
4242
;;
4343
grep)
4444
;;
45+
append|a)
46+
_notes_complete_notes "$cur"
47+
;;
4548
open|o)
4649
_notes_complete_notes "$cur"
4750
;;

notes.fish

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
## lists of commands used by notes
66
set -l notes_list_commands o open mv rm cat
77
set -l notes_friendly_commands new open find ls rm cat mv grep
8-
set -l notes_commands n new ls find f grep g search s open o mv rm cat
8+
set -l notes_commands n new ls find f grep g search s open o mv rm cat append a
99
set -l notes_dir_commands n new grep ls
1010

1111
# NOTES_DIRECTORY

test/test-append.bats

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#!./test/libs/bats/bin/bats
2+
3+
load 'helpers'
4+
5+
setup() {
6+
setupNotesEnv
7+
}
8+
9+
teardown() {
10+
teardownNotesEnv
11+
}
12+
13+
notes="./notes"
14+
15+
@test "Appends a note with a message from commandline" {
16+
$notes append note.md Test
17+
run $notes cat note.md
18+
19+
assert_success
20+
assert_output $'Test'
21+
}
22+
23+
@test "Doesn't override a file, it actually appends" {
24+
$notes append note.md Test1
25+
$notes append note.md Test2
26+
run $notes cat note.md
27+
28+
assert_success
29+
assert_output $'Test1\nTest2'
30+
}
31+
32+
@test "Accepts input from stdin pipe" {
33+
echo "Echo Test" | $notes append note.md
34+
run $notes cat note.md
35+
36+
assert_success
37+
assert_output $'Echo Test'
38+
}
39+
40+
@test "Works with more than one word given via commandline" {
41+
$notes append note.md Multi word test
42+
run $notes cat note.md
43+
44+
assert_success
45+
assert_output $'Multi word test'
46+
}
47+
48+
@test "Fails when no message is given" {
49+
run $notes append note.md
50+
51+
assert_failure
52+
assert_output $'No text was provided to append'
53+
}
54+
55+
@test "Shortname works to invoke append" {
56+
$notes a note.md Test
57+
run $notes cat note.md
58+
59+
assert_success
60+
assert_output $'Test'
61+
}
62+
63+
@test "Should complain and ask for a name if one is not provided" {
64+
run $notes append
65+
66+
assert_failure
67+
assert_line "Append requires a name, but none was provided."
68+
}

test/test-bash-completion.bats

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ teardown() {
1919
assert_contains "find" "${COMPREPLY[@]}"
2020
assert_contains "grep" "${COMPREPLY[@]}"
2121
assert_contains "open" "${COMPREPLY[@]}"
22+
assert_contains "append" "${COMPREPLY[@]}"
2223
assert_contains "ls" "${COMPREPLY[@]}"
2324
assert_contains "rm" "${COMPREPLY[@]}"
2425
assert_contains "search" "${COMPREPLY[@]}"

0 commit comments

Comments
 (0)