Skip to content

Commit

Permalink
Merge pull request #85 from primis/append
Browse files Browse the repository at this point in the history
  • Loading branch information
pimterry authored Jun 24, 2021
2 parents f7f42a5 + 4f3cdf6 commit da441f3
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 7 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -133,6 +133,10 @@ 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 <note-name> [message]`

Appends `message` to the `note-name` note. If this note does not exist, a new note of <note-name> will be created. This command also accepts stdin via piping. An example would be `echo "hello" | notes append <note-name>` Shorthand alias also available with `notes a`.

### `notes mv <note-name> <destination>|<directory>`

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.
Expand Down
1 change: 1 addition & 0 deletions _notes
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ __notes_cmd ()
grep:'<pattern> Search notes by content'
search:'[pattern] Search notes by filename or content'
open:'<name> Open a notes for editing by full name'
append:'<name> [message] Appends a note. Will use stdin if no message is given'
rm:'[-r | --recursive] <name> Remove note, or folder if -r or --recursive is given]'
cat:'<name> Display a note by name'
--version:'Show version'
Expand Down
37 changes: 35 additions & 2 deletions notes
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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 <name> # Open a note for editing by full name
$name append|a <name> [message] # Appends a note. Will use stdin if no message is given
$name mv <source> <dest>|<directory> # Rename a note, or move a note when a directory is given
$name rm [-r | --recursive] <name> # Remove note, or folder if -r or --recursive is given
$name cat <name> # Display note
Expand Down Expand Up @@ -312,6 +342,9 @@ main() {
"open"|"o" )
cmd="handle_multiple_notes open"
;;
"append"|"a" )
cmd="append_note"
;;
"mv" )
cmd="move_note"
;;
Expand Down
5 changes: 5 additions & 0 deletions notes.1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion notes.bash_completion
Original file line number Diff line number Diff line change
Expand Up @@ -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}"))
}

Expand All @@ -42,6 +42,9 @@ _notes()
;;
grep)
;;
append|a)
_notes_complete_notes "$cur"
;;
open|o)
_notes_complete_notes "$cur"
;;
Expand Down
2 changes: 1 addition & 1 deletion notes.fish
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# Run this file to run all the tests, once
./test/libs/bats/bin/bats test/*.bats
./test/libs/bats/bin/bats test/*.bats
68 changes: 68 additions & 0 deletions test/test-append.bats
Original file line number Diff line number Diff line change
@@ -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."
}
1 change: 1 addition & 0 deletions test/test-bash-completion.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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[@]}"
Expand Down

0 comments on commit da441f3

Please sign in to comment.