Skip to content

Commit b49347c

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

File tree

6 files changed

+64
-58
lines changed

6 files changed

+64
-58
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,15 @@ There are also more complex options available. You can set any configuration pro
9595

9696
* `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.
98+
* `TEMPLATES_DIR` changes the directory in which templates are stored.
9899
* `NOTES_DIRECTORY` changes the directory in which notes are stored.
99100
* `EDITOR` can also be overriden here, for `notes` only.
100101
* `POST_COMMAND` sets the command to run after any modification command (e.g. `open`, `mv`, ...) succeeds
101102

102103

103104
## How do I use it?
104105

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

107108
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`.
108109

_notes

Lines changed: 1 addition & 1 deletion
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, if -t is given, use template'
20+
new:'Create new file'
2121
ls:'<pattern> List notes by path'
2222
find:'[pattern] Search notes by filename and path'
2323
grep:'<pattern> Search notes by content'

config

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,8 @@
1414
# Define the directory where notes are stored
1515
# NOTES_DIRECTORY=~/notes
1616

17+
# Define the directory where templates are stored
18+
# TEMPLATES_DIR=.templates
19+
1720
# Define command to run after modification command
1821
# POST_COMMAND="/path/to/custom_script.sh"

notes

Lines changed: 34 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
#!/usr/bin/env bash
22

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

66
# Default Date string before config
77
QUICKNOTE_FORMAT="quicknote-%Y-%m-%d"
88
NOTES_EXT="md"
9+
TEMPLATES_DIR=".templates"
910
# Look for configuration file at ~/.config/notes/config and use it
1011
if [ -f ~/.config/notes/config ]; then
1112
. ~/.config/notes/config
@@ -21,12 +22,6 @@ if ! $(mkdir -p "$notes_dir"); then
2122
exit 1
2223
fi
2324

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-
3025
# If no $EDITOR, look for `editor` (symlink on debian/ubuntu/etc)
3126
if [ -z "$EDITOR" ] && type editor &>/dev/null; then
3227
EDITOR=editor
@@ -123,22 +118,47 @@ generate_name() {
123118
}
124119

125120
new_note() {
121+
local template_used=false
122+
local note_name=""
123+
126124
if [[ "$1" == "-t" ]]; then
125+
template_used=true
127126
shift
128-
note_from_template "$@"
129-
return
130127
fi
131128

132-
local note_name="$*"
133-
if [[ $note_name == "" ]]; then
134-
note_name="$(generate_name)"
129+
if [[ "$#" -eq 0 ]]; then
130+
note_name="$(generate_name).$NOTES_EXT"
131+
elif [[ "$#" -eq 1 ]]; then
132+
note_name="$1"
133+
else
134+
note_name="$2.$NOTES_EXT"
135135
fi
136136

137137
if echo "$note_name" | grep "/$" &> /dev/null; then
138138
note_name="${note_name}/$(generate_name)"
139139
fi
140140

141-
mkdir -p "$(dirname "$notes_dir/$note_name")"
141+
local note_dir="$(dirname "$notes_dir/$note_name")"
142+
mkdir -p "$note_dir"
143+
144+
if "$template_used"; then
145+
local template_folder="$notes_dir/$TEMPLATES_DIR"
146+
147+
if [[ ! -d "$template_folder" ]]; then
148+
printf "Template folder not found: $template_folder\n"
149+
exit 1
150+
fi
151+
152+
local template_name="$1"
153+
154+
if [[ ! "$template_name" == *.$NOTES_EXT ]]; then
155+
template_name="$template_name.$NOTES_EXT"
156+
fi
157+
158+
local template_path="$template_folder/$template_name"
159+
local note_path="$notes_dir/$note_name"
160+
cp "$template_path" "$note_path"
161+
fi
142162

143163
open_note "$note_name"
144164
}
@@ -290,48 +310,13 @@ cat_note() {
290310
cat "$note_path"
291311
}
292312

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-
328313
usage() {
329314
local name=$(basename $0)
330315
cat <<EOF
331316
$name is a command line note taking tool.
332317
333318
Usage:
334-
$name new|n [-t] <template> <name> # Create a new note, if -t is given, use template
319+
$name new|n [-t <template>] <name> # Create a new note, if -t is given, use template
335320
$name ls <pattern> # List notes by path
336321
$name find|f [pattern] # Search notes by filename and path
337322
$name grep|g <pattern> # Search notes by content

test/test-ls.bats

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ teardown() {
1212

1313
notes="./notes"
1414

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

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

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

test/test-new.bats

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,16 +88,33 @@ notes="./notes"
8888
}
8989

9090
@test "Should create a new template note" {
91-
run $notes new templates/basic
91+
run $notes new .templates/basic
9292

9393
assert_success
94-
assert_exists "$NOTES_DIRECTORY/templates/basic.md"
94+
assert_exists "$NOTES_DIRECTORY/.templates/basic.md"
9595
}
9696

9797
@test "Should create a new note with the given template" {
98-
run $notes new templates/basic
98+
run $notes new .templates/basic
9999
run $notes new -t basic note_with_template
100100

101101
assert_success
102102
assert_exists "$NOTES_DIRECTORY/note_with_template.md"
103-
}
103+
}
104+
105+
@test "Should create new note from template within subfolders" {
106+
run $notes new .templates/basic
107+
run $notes new -t basic subfolder/note_with_template
108+
109+
assert_success
110+
assert_exists "$NOTES_DIRECTORY/subfolder/note_with_template.md"
111+
}
112+
113+
@test "Check that the template is used" {
114+
run $notes append .templates/basic "This is a basic template"
115+
run $notes new -t basic note_with_template
116+
run $notes cat note_with_template
117+
118+
assert_success
119+
assert_output "This is a basic template"
120+
}

0 commit comments

Comments
 (0)