Skip to content

Commit 51a70ec

Browse files
authored
Merge pull request #32 from primis/master
Added quicknotes
2 parents 9f95f12 + a2f60e7 commit 51a70ec

File tree

5 files changed

+56
-3
lines changed

5 files changed

+56
-3
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ You'll need to open a new shell for this to take effect.
4949

5050
You can set your favourite text editor and your notes directory by setting the `$EDITOR` and `$NOTES_DIRECTORY` environmental variables.
5151

52+
You can also set `$QUICKNOTE_FORMAT` to change the way that quicknotes are generated. The string is formatted using the `date` command.
53+
5254
Most users shouldn't need to do any more than that. If you're doing anything more complicated though, you can configure `notes` config directly in "~/.config/notes/config", including EDITOR and NOTES_DIRECTORY. We've included an example in this repo for you ([config.example](config.example)) that you can copy. Any values set in the config file override values in environment variables.
5355

5456
Right now this mainly exists in case you want to use a different `EDITOR` for notes than the one you have set in your environment generally, but this is where all other config will be living in future.
@@ -59,6 +61,9 @@ Right now this mainly exists in case you want to use a different `EDITOR` for no
5961

6062
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. Shorthand alias also available with `notes n`.
6163

64+
### `notes new`
65+
Creates a quicknote with the name of the format `quicknote-YYYY-MM-DD.md`. This can be changed in the configuration file.
66+
6267
### `notes find <part-of-a-note-name>`
6368

6469
Searches note filenames and paths for the given string, and returns every single match. If no pattern is specified, this returns every single note. Shorthand alias also available with `notes f`.

config.example

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,6 @@
44

55
# Set the editor that notes are opened with
66
EDITOR=nano
7+
8+
# Change the quicknote format to get rid of the word "quicknote"
9+
QUICKNOTE_FORMAT="%Y-%m-%d"

notes

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#!/usr/bin/env bash
22

3+
# Default Date string before config
4+
QUICKNOTE_FORMAT="quicknote-%Y-%m-%d"
5+
36
# Look for configuration file at ~/.config/notes/config and use it
47
if [ -f ~/.config/notes/config ]; then
58
. ~/.config/notes/config
@@ -90,15 +93,30 @@ grep_notes() {
9093
return 2
9194
fi
9295
}
96+
generate_name() {
97+
local append_num=0
98+
local format_string="`date +$QUICKNOTE_FORMAT`"
99+
# Initial test has no append
100+
resolved_name=$format_string
101+
while [[ -e "$notes_dir/$resolved_name.md" ]]
102+
do
103+
append_num=$[$append_num+1]
104+
resolved_name=$format_string.$append_num
105+
done
106+
}
93107

94108
new_note() {
95109
local note_name="$*"
110+
if [[ $note_name == "" ]]; then
111+
generate_name
112+
note_name=$resolved_name
113+
fi
96114
mkdir -p "$(dirname "$notes_dir/$note_name")"
97115
open_note "$note_name.md"
98116
}
99117

100118
remove_note() {
101-
local rm_args=()
119+
local rm_arga=()
102120
if [[ "$1" == "-r" || "$1" == "--recursive" ]]; then
103121
rm_args+=("--recursive")
104122
shift

test/test-config.bats

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,23 @@ load 'helpers'
77
setup() {
88
setupNotesEnv
99
}
10-
1110
teardown() {
1211
teardownNotesEnv
1312
}
14-
13+
export EDITOR=touch
1514
notes="./notes"
1615

16+
17+
@test "Configuration should override QUICKNOTE_FORMAT" {
18+
mkdir -p $HOME/.config/notes
19+
echo "QUICKNOTE_FORMAT=test" > $HOME/.config/notes/config
20+
21+
run $notes new
22+
23+
assert_success
24+
assert_exists "$NOTES_DIRECTORY/test.md"
25+
}
26+
1727
@test "Configuration should override EDITOR" {
1828
mkdir -p $HOME/.config/notes
1929
echo "EDITOR=echo" > $HOME/.config/notes/config

test/test-new.bats

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,23 @@ notes="./notes"
2222
assert_exists "$NOTES_DIRECTORY/note.md"
2323
}
2424

25+
@test "Should create quicknote without a name given" {
26+
today=`date "+%Y-%m-%d"`
27+
run $notes new
28+
29+
assert_success
30+
assert_exists "$NOTES_DIRECTORY/quicknote-$today.md"
31+
}
32+
33+
@test "Should append to name if quicknote already exists" {
34+
today=`date "+%Y-%m-%d"`
35+
run $notes new
36+
run $notes new
37+
38+
assert_success
39+
assert_exists "$NOTES_DIRECTORY/quicknote-$today.1.md"
40+
}
41+
2542
@test "Should create new notes when using the shorthand alias" {
2643
run $notes n note
2744

0 commit comments

Comments
 (0)