Skip to content

Commit

Permalink
update: create notes from templates
Browse files Browse the repository at this point in the history
  • Loading branch information
Rooyca committed Sep 4, 2023
1 parent 30e5b36 commit e4cd218
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 10 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,12 @@ There are also more complex options available. You can set any configuration pro

## How do I use it?

### `notes new <note-name>`
### `notes new [-t] <template-name> <note-name>`

Opens your `$EDITOR` of choice for a new note, with the given name. The name can include slashes, if you want to put your note in a subfolder. Leave out the name if you want one to be generated for you (e.g. `quicknote-2016-12-21.md` - format configurable with `$QUICKNOTE_FORMAT`). If you want to place a quicknote in a subfolder, use a trailing slash: `notes new subfolder/`. Shorthand alias also available with `notes n`.

If you pass the `-t` flag to `notes new`, the note will be created from a template. The template is a file in your notes directory, with the same name as the template name you pass in. For example, if you have a template called `meeting-notes` in your notes directory, you can create a new note from that template with `notes new -t meeting-notes new-file-name`. This will open your `$EDITOR` with the contents of that template file, and you can edit it and save it as a new note.

If you do not supply an extension in `note-name`, it will be automatically appended with the default file extension (e.g. "newnote" will become "newnote.md"). However, if you include a one-to-four-letter file extension, notes will use that extension when creating the file (e.g. "newnote.tex" is created as "newnote.tex"; not "newnote.md", or "newnote.tex.md").

### `notes find <part-of-a-note-name>`
Expand Down
2 changes: 1 addition & 1 deletion _notes
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ __notes_cmd ()
{
local -a list
list=(
new:'Create new file'
new:'Create new file, if -t is given, use template'
ls:'<pattern> List notes by path'
find:'[pattern] Search notes by filename and path'
grep:'<pattern> Search notes by content'
Expand Down
51 changes: 49 additions & 2 deletions notes
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env bash

# Version
notes_version="1.2.0"
notes_version="1.3.0"

# Default Date string before config
QUICKNOTE_FORMAT="quicknote-%Y-%m-%d"
Expand All @@ -21,6 +21,12 @@ if ! $(mkdir -p "$notes_dir"); then
exit 1
fi

# Make 'template' directory if it doesn't exist
if ! $(mkdir -p "$notes_dir/templates"); then
echo "Could not create directory $notes_dir/templates, please update your \$NOTES_DIRECTORY" >&2
exit 1
fi

# If no $EDITOR, look for `editor` (symlink on debian/ubuntu/etc)
if [ -z "$EDITOR" ] && type editor &>/dev/null; then
EDITOR=editor
Expand Down Expand Up @@ -117,6 +123,12 @@ generate_name() {
}

new_note() {
if [[ "$1" == "-t" ]]; then
shift
note_from_template "$@"
return
fi

local note_name="$*"
if [[ $note_name == "" ]]; then
note_name="$(generate_name)"
Expand Down Expand Up @@ -278,13 +290,48 @@ cat_note() {
cat "$note_path"
}

note_from_template() {
local tn=$1

if [[ ! "$tn" == *.$NOTES_EXT ]]; then
tn="$1.$NOTES_EXT"
fi

local template="$notes_dir/templates/$tn"
local note_name="$2"

if [[ ! "$note_name" == *.$NOTES_EXT ]]; then
note_name="$note_name.$NOTES_EXT"
fi

local note_path="$notes_dir/$note_name"

if [[ ! -f "$template" ]]; then
printf "Template not found: $template\n"
exit 1
fi

if [[ -z "$note_name" ]]; then
printf "New note requires a name, but none was provided.\n"
exit 1
fi

if [[ -e "$note_path" ]]; then
printf "Note already exists: $note_path\n"
exit 1
fi

cp "$template" "$note_path"
open_note "$note_name"
}

usage() {
local name=$(basename $0)
cat <<EOF
$name is a command line note taking tool.
Usage:
$name new|n <name> # Create a new note
$name new|n [-t] <template> <name> # Create a new note, if -t is given, use template
$name ls <pattern> # List notes by path
$name find|f [pattern] # Search notes by filename and path
$name grep|g <pattern> # Search notes by content
Expand Down
6 changes: 3 additions & 3 deletions notes.1
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ note. You can set $EDITOR in your shell to override it.
This file overrides any settings in your rc file.
.SH COMMANDS
.TP
.BR new ", " n " " \fR[\fINAME\fR]
Open your text editor with a new note, with given name.
.BR new ", " n " " "\fR[\fB\-t\fR] " \fR[\fBTEMPLATE\fR] \fR[\fINAME\fR]
Open your text editor with a new note, with the given name.
If no name is given, a quick note will be created with an auto-generated
name.
name. If the \-t option is provided, the specified TEMPLATE will be used for the new note.
.TP
.BR find ", " f " " \fR[\fIPATTERN\fR]
Searches for notes using the given pattern, returning all matches. If no
Expand Down
6 changes: 3 additions & 3 deletions test/test-ls.bats
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ teardown() {

notes="./notes"

@test "Should output nothing and return non-zero if there are no notes to list" {
@test "Should display the templates folder when no notes are present" {
run $notes ls

assert_failure
assert_success
echo $output
assert_equal $(echo $output | wc -w) 0
assert_equal $(echo $output | wc -w) 1
}

@test "Should list all notes in notes directory if no pattern is provided to find" {
Expand Down
15 changes: 15 additions & 0 deletions test/test-new.bats
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,18 @@ notes="./notes"
assert_success
assert_exists "$NOTES_DIRECTORY/explicit-ext.zzz"
}

@test "Should create a new template note" {
run $notes new templates/basic

assert_success
assert_exists "$NOTES_DIRECTORY/templates/basic.md"
}

@test "Should create a new note with the given template" {
run $notes new templates/basic
run $notes new -t basic note_with_template

assert_success
assert_exists "$NOTES_DIRECTORY/note_with_template.md"
}

0 comments on commit e4cd218

Please sign in to comment.