Skip to content

Commit 5b7bdca

Browse files
committed
Added quicknotes
1 parent d7ff4f3 commit 5b7bdca

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 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
@@ -68,15 +71,30 @@ grep_notes() {
6871
return 2
6972
fi
7073
}
74+
generate_name() {
75+
local append_num=0
76+
local format_string="`date +$QUICKNOTE_FORMAT`"
77+
# Initial test has no append
78+
resolved_name=$format_string
79+
while [[ -e "$notes_dir/$resolved_name.md" ]]
80+
do
81+
append_num=$[$append_num+1]
82+
resolved_name=$format_string.$append_num
83+
done
84+
}
7185

7286
new_note() {
7387
local note_name="$*"
88+
if [[ $note_name == "" ]]; then
89+
generate_name
90+
note_name=$resolved_name
91+
fi
7492
mkdir -p "$(dirname "$notes_dir/$note_name")"
7593
open_note "$note_name.md"
7694
}
7795

7896
remove_note() {
79-
local rm_args=()
97+
local rm_arga=()
8098
if [[ "$1" == "-r" || "$1" == "--recursive" ]]; then
8199
rm_args+=("--recursive")
82100
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)