Skip to content

Commit e4cd218

Browse files
committed
update: create notes from templates
1 parent 30e5b36 commit e4cd218

File tree

6 files changed

+74
-10
lines changed

6 files changed

+74
-10
lines changed

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,12 @@ There are also more complex options available. You can set any configuration pro
102102

103103
## How do I use it?
104104

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

107107
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`.
108108

109+
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.
110+
109111
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").
110112

111113
### `notes find <part-of-a-note-name>`

_notes

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ __notes_cmd ()
1717
{
1818
local -a list
1919
list=(
20-
new:'Create new file'
20+
new:'Create new file, if -t is given, use template'
2121
ls:'<pattern> List notes by path'
2222
find:'[pattern] Search notes by filename and path'
2323
grep:'<pattern> Search notes by content'

notes

+49-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env bash
22

33
# Version
4-
notes_version="1.2.0"
4+
notes_version="1.3.0"
55

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

24+
# Make 'template' directory if it doesn't exist
25+
if ! $(mkdir -p "$notes_dir/templates"); then
26+
echo "Could not create directory $notes_dir/templates, please update your \$NOTES_DIRECTORY" >&2
27+
exit 1
28+
fi
29+
2430
# If no $EDITOR, look for `editor` (symlink on debian/ubuntu/etc)
2531
if [ -z "$EDITOR" ] && type editor &>/dev/null; then
2632
EDITOR=editor
@@ -117,6 +123,12 @@ generate_name() {
117123
}
118124

119125
new_note() {
126+
if [[ "$1" == "-t" ]]; then
127+
shift
128+
note_from_template "$@"
129+
return
130+
fi
131+
120132
local note_name="$*"
121133
if [[ $note_name == "" ]]; then
122134
note_name="$(generate_name)"
@@ -278,13 +290,48 @@ cat_note() {
278290
cat "$note_path"
279291
}
280292

293+
note_from_template() {
294+
local tn=$1
295+
296+
if [[ ! "$tn" == *.$NOTES_EXT ]]; then
297+
tn="$1.$NOTES_EXT"
298+
fi
299+
300+
local template="$notes_dir/templates/$tn"
301+
local note_name="$2"
302+
303+
if [[ ! "$note_name" == *.$NOTES_EXT ]]; then
304+
note_name="$note_name.$NOTES_EXT"
305+
fi
306+
307+
local note_path="$notes_dir/$note_name"
308+
309+
if [[ ! -f "$template" ]]; then
310+
printf "Template not found: $template\n"
311+
exit 1
312+
fi
313+
314+
if [[ -z "$note_name" ]]; then
315+
printf "New note requires a name, but none was provided.\n"
316+
exit 1
317+
fi
318+
319+
if [[ -e "$note_path" ]]; then
320+
printf "Note already exists: $note_path\n"
321+
exit 1
322+
fi
323+
324+
cp "$template" "$note_path"
325+
open_note "$note_name"
326+
}
327+
281328
usage() {
282329
local name=$(basename $0)
283330
cat <<EOF
284331
$name is a command line note taking tool.
285332
286333
Usage:
287-
$name new|n <name> # Create a new note
334+
$name new|n [-t] <template> <name> # Create a new note, if -t is given, use template
288335
$name ls <pattern> # List notes by path
289336
$name find|f [pattern] # Search notes by filename and path
290337
$name grep|g <pattern> # Search notes by content

notes.1

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ note. You can set $EDITOR in your shell to override it.
1616
This file overrides any settings in your rc file.
1717
.SH COMMANDS
1818
.TP
19-
.BR new ", " n " " \fR[\fINAME\fR]
20-
Open your text editor with a new note, with given name.
19+
.BR new ", " n " " "\fR[\fB\-t\fR] " \fR[\fBTEMPLATE\fR] \fR[\fINAME\fR]
20+
Open your text editor with a new note, with the given name.
2121
If no name is given, a quick note will be created with an auto-generated
22-
name.
22+
name. If the \-t option is provided, the specified TEMPLATE will be used for the new note.
2323
.TP
2424
.BR find ", " f " " \fR[\fIPATTERN\fR]
2525
Searches for notes using the given pattern, returning all matches. If no

test/test-ls.bats

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ teardown() {
1212

1313
notes="./notes"
1414

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

18-
assert_failure
18+
assert_success
1919
echo $output
20-
assert_equal $(echo $output | wc -w) 0
20+
assert_equal $(echo $output | wc -w) 1
2121
}
2222

2323
@test "Should list all notes in notes directory if no pattern is provided to find" {

test/test-new.bats

+15
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,18 @@ notes="./notes"
8686
assert_success
8787
assert_exists "$NOTES_DIRECTORY/explicit-ext.zzz"
8888
}
89+
90+
@test "Should create a new template note" {
91+
run $notes new templates/basic
92+
93+
assert_success
94+
assert_exists "$NOTES_DIRECTORY/templates/basic.md"
95+
}
96+
97+
@test "Should create a new note with the given template" {
98+
run $notes new templates/basic
99+
run $notes new -t basic note_with_template
100+
101+
assert_success
102+
assert_exists "$NOTES_DIRECTORY/note_with_template.md"
103+
}

0 commit comments

Comments
 (0)