From aaa0d33b4fe7c7a045365fc7c578256c7c5486c2 Mon Sep 17 00:00:00 2001 From: Allison Sargente Date: Wed, 9 Jun 2021 18:21:41 -0400 Subject: [PATCH] Added append command --- README.md | 10 ++++- _notes | 1 + notes | 37 +++++++++++++++++- notes.1 | 5 +++ notes.bash_completion | 5 ++- notes.fish | 2 +- test/test-append.bats | 68 ++++++++++++++++++++++++++++++++++ test/test-bash-completion.bats | 1 + 8 files changed, 123 insertions(+), 6 deletions(-) create mode 100755 test/test-append.bats diff --git a/README.md b/README.md index 06ce661..105bf60 100644 --- a/README.md +++ b/README.md @@ -89,11 +89,11 @@ By default your notes live in ~/notes, but you can change that to anywhere you l 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. -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. +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. ### What are the configuration options? -* `QUICKNOTE_FORMAT` changes the way that quicknotes are generated. The string formatted using the `date` command. +* `QUICKNOTE_FORMAT` changes the way that quicknotes are generated. The string formatted using the `date` command. * `NOTES_EXT` changes the default extension that notes are saved with. * `NOTES_DIRECTORY` changes the directory in which notes are stored. * `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 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`). +### `notes append [message]` + +Appends a given note with the test `message` from the command line. If no note yet exists, a new note of will be created. This command also accepts stdin +via piping. An example would be `echo "hello" | notes append ` +Shorthand alias also available with `notes a`. + ### `notes mv |` 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. diff --git a/_notes b/_notes index 80c7d63..979836c 100644 --- a/_notes +++ b/_notes @@ -23,6 +23,7 @@ __notes_cmd () grep:' Search notes by content' search:'[pattern] Search notes by filename or content' open:' Open a notes for editing by full name' + append:' [message] ' rm:'[-r | --recursive] Remove note, or folder if -r or --recursive is given]' cat:' Display a note by name' --version:'Show version' diff --git a/notes b/notes index 911e045..49c2d8f 100755 --- a/notes +++ b/notes @@ -211,10 +211,39 @@ open_note() { $EDITOR "$note_path" < /dev/tty } +append_note() { + local source_note_path=$( get_full_note_path "$1" ) + local to_append="${@:2}" + + # If no note name was provided, exit + if [[ -z "$1" ]]; then + printf "Append requires a name, but none was provided.\n" + exit 1 + fi + + # If note doesn't exist, make sure the directory does + if [[ ! -e "$source_note_path" ]]; then + mkdir -p "$(dirname "$source_note_path")" + fi + + # if to_append is empty, check stdin + if [[ -z "$to_append" ]] && [[ -p /dev/stdin ]]; then + to_append=$(cat) + fi + + # If to_append is *still* empty, report an error + if [[ -z "$to_append" ]]; then + printf "No text was provided to append\n" + exit 1 + fi + + echo "$to_append" >> "$source_note_path" +} + move_note() { local source_note_path=$( get_full_note_path "$1" ) local dest_or_dir_path=$2 - + if [[ ! -e "$source_note_path" ]]; then printf "mv requires a source note that exists\n" exit 1 @@ -224,7 +253,7 @@ move_note() { printf "mv requires a destination name or folder\n" exit 1 fi - + dir_path="$notes_dir/$dest_or_dir_path" if [[ -d "$dir_path" ]]; then mv $source_note_path $dir_path @@ -262,6 +291,7 @@ Usage: $name search|s [pattern] # Search notes by filename or content $name open|o # Open your notes directory $name open|o # Open a note for editing by full name + $name append|a [message] # Appends a note. Will use stdin if no message is given $name mv | # Rename a note, or move a note when a directory is given $name rm [-r | --recursive] # Remove note, or folder if -r or --recursive is given $name cat # Display note @@ -312,6 +342,9 @@ main() { "open"|"o" ) cmd="handle_multiple_notes open" ;; + "append"|"a" ) + cmd="append_note" + ;; "mv" ) cmd="move_note" ;; diff --git a/notes.1 b/notes.1 index 396fe18..12352df 100644 --- a/notes.1 +++ b/notes.1 @@ -42,6 +42,11 @@ Opens $NOTES_DIRECTORY in your file explorer. Opens note \fINAME\fR using your configured editor. \fINAME\fR can either be an absolute path or relative to $NOTES_DIRECTORY. .TP +.BR append ", " a " "\fINAME\fR " [" \fIMESSAGE\fR] +Appends \fINAME\fR with text \fIMESSAGE\fR if given. If no \fIMESSAGE\fR is +defined, it will be taken from stdin. Stdin input does not work +in interactive mode (The input must be piped in). +.TP .BR rm " "\fR[\fB\-r\fR | \fB\-\-recursive\fR] " "\fINAME\fR Removes \fINAME\fR. If \-r or \-\-recursive is given, folders will be removed. .TP diff --git a/notes.bash_completion b/notes.bash_completion index 72fdca8..dbf118f 100644 --- a/notes.bash_completion +++ b/notes.bash_completion @@ -21,7 +21,7 @@ _notes_complete_notes() { } _notes_complete_commands() { - local valid_commands="new find grep open ls rm cat search" + local valid_commands="new find grep open ls rm cat append search" COMPREPLY=($(compgen -W "${valid_commands}" -- "${1}")) } @@ -42,6 +42,9 @@ _notes() ;; grep) ;; + append|a) + _notes_complete_notes "$cur" + ;; open|o) _notes_complete_notes "$cur" ;; diff --git a/notes.fish b/notes.fish index 0b0c236..f895c4e 100644 --- a/notes.fish +++ b/notes.fish @@ -5,7 +5,7 @@ ## lists of commands used by notes set -l notes_list_commands o open mv rm cat set -l notes_friendly_commands new open find ls rm cat mv grep -set -l notes_commands n new ls find f grep g search s open o mv rm cat +set -l notes_commands n new ls find f grep g search s open o mv rm cat append a set -l notes_dir_commands n new grep ls # NOTES_DIRECTORY diff --git a/test/test-append.bats b/test/test-append.bats new file mode 100755 index 0000000..8411a94 --- /dev/null +++ b/test/test-append.bats @@ -0,0 +1,68 @@ +#!./test/libs/bats/bin/bats + +load 'helpers' + +setup() { + setupNotesEnv +} + +teardown() { + teardownNotesEnv +} + +notes="./notes" + +@test "Appends a note with a message from commandline" { + $notes append note.md Test + run $notes cat note.md + + assert_success + assert_output $'Test' +} + +@test "Doesn't override a file, it actually appends" { + $notes append note.md Test1 + $notes append note.md Test2 + run $notes cat note.md + + assert_success + assert_output $'Test1\nTest2' +} + +@test "Accepts input from stdin pipe" { + echo "Echo Test" | $notes append note.md + run $notes cat note.md + + assert_success + assert_output $'Echo Test' +} + +@test "Works with more than one word given via commandline" { + $notes append note.md Multi word test + run $notes cat note.md + + assert_success + assert_output $'Multi word test' +} + +@test "Fails when no message is given" { + run $notes append note.md + + assert_failure + assert_output $'No text was provided to append' +} + +@test "Shortname works to invoke append" { + $notes a note.md Test + run $notes cat note.md + + assert_success + assert_output $'Test' +} + +@test "Should complain and ask for a name if one is not provided" { + run $notes append + + assert_failure + assert_line "Append requires a name, but none was provided." +} diff --git a/test/test-bash-completion.bats b/test/test-bash-completion.bats index 4ba209b..71a2b90 100755 --- a/test/test-bash-completion.bats +++ b/test/test-bash-completion.bats @@ -19,6 +19,7 @@ teardown() { assert_contains "find" "${COMPREPLY[@]}" assert_contains "grep" "${COMPREPLY[@]}" assert_contains "open" "${COMPREPLY[@]}" + assert_contains "append" "${COMPREPLY[@]}" assert_contains "ls" "${COMPREPLY[@]}" assert_contains "rm" "${COMPREPLY[@]}" assert_contains "search" "${COMPREPLY[@]}"