Skip to content

Commit 88a9c32

Browse files
committed
2 parents bb75085 + 87a96e7 commit 88a9c32

File tree

6 files changed

+201
-7
lines changed

6 files changed

+201
-7
lines changed

README.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Download `notes`, `chmod +x`, put it in your `$path`. This will probably do it:
2727
curl https://cdn.rawgit.com/pimterry/notes/v0.2.0/notes > /usr/local/bin/notes && chmod +x /usr/local/bin/notes
2828
```
2929

30-
By default your notes live in ~/notes, but you can change that to anywhere you like by setting the `$NOTES_DIRECTORY` environmental variable.
30+
By default your notes live in ~/notes, but you can change that to anywhere you like by setting the `$NOTES_DIRECTORY` environmental variable. See [how do I configure this?](#how-do-i-configure-this) for more details.
3131

3232
#### Installing auto completion
3333

@@ -53,17 +53,17 @@ You'll need to open a new shell for this to take effect.
5353

5454
## How do I configure this?
5555

56-
You can set your favourite text editor and your notes directory by setting the `$EDITOR` and `$NOTES_DIRECTORY` environmental variables.
56+
To get started with you'll want to set `$EDITOR` to your favourite text editor, and probably `$NOTES_DIRECTORY` to the directory in which you'd like to use to store your notes (this defaults to `~/notes`). You'll typically want to set these as environment variables in your `.bashrc`, `.zshrc`, or similar.
5757

58-
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.
58+
There are also more complex options available. You can set any configuration properties either in the environment, or in a config file (stored in `~/.config/notes/config`), with settings in config overriding those in your environment. This allows you to configure a different `$EDITOR` for notes to everything else, if you like. The config file is a good choice for more complex set ups, but probably not worth worrying about to start with. We've included an example config in this repo for you ([config.example](config.example)) that you can copy if you like.
5959

60-
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.
60+
The only other configuration property right now is `$QUICKNOTE_FORMAT`, which changes the way that quicknote filenames are generated. The string is formatted by passing it to the `date` command, and defaults to `quicknote-%Y-%m-%d`.
6161

6262
## How do I use it?
6363

6464
### `notes new <note-name>`
6565

66-
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`.
66+
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`). Shorthand alias also available with `notes n`.
6767

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

@@ -73,6 +73,10 @@ Searches note filenames and paths for the given string, and returns every single
7373

7474
Searches all note content for the given string and returns all the matches. Shorthand alias also available with `notes g`.
7575

76+
### `notes search <part-of-a-note-name-or-note-content>`
77+
78+
Searches all note content and note filenames for the given string and returns all the matches. Shorthand alias also available with `notes s`.
79+
7680
### `notes ls <directory>`
7781

7882
Lists note names and note directories at a single level. Lists all top level notes and directories if no path is provided, or the top-level contents of a directory if one is provided.
@@ -97,7 +101,6 @@ Combine these together! This opens each matching note in your `$EDITOR` in turn.
97101

98102
All the above works. Here's what's coming next:
99103

100-
- [ ] Combining find and grep, to match either one (https://github.com/pimterry/notes/issues/16)
101104
- [ ] More interesting and nicer looking file/grep search result formatting, perhaps only when not piping? (https://github.com/pimterry/notes/issues/27)
102105
- [ ] Make the file extension optional (https://github.com/pimterry/notes/issues/24)
103106
- [ ] Interactive mode? `notes` could open a scrollable list of notes, open your editor when you pick one, and reappear after you close it. (https://github.com/pimterry/notes/issues/17)

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: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
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
4-
if [ -f ~/.config/notes/config ]; then
7+
if [ -f ~/.config/notes/config ]; then
58
. ~/.config/notes/config
69
fi
710

@@ -38,6 +41,28 @@ ls_notes() {
3841
fi
3942
}
4043

44+
search_filenames_and_contents() {
45+
if [ "$#" -gt 0 ]; then
46+
find_output=$(find "$notes_dir" -type f -exec bash -c \
47+
"shopt -s nocasematch
48+
grep -il \"$*\" \"{}\" || if [[ \"{}\" =~ \"$*\" ]]; then
49+
echo \"{}\";
50+
fi" \;\
51+
)
52+
else
53+
find_output=$(find "$notes_dir" -type f)
54+
fi
55+
find_result=$?
56+
formatted_output=$(printf "$find_output" | without_notes_dir)
57+
58+
if [[ $find_result == 0 && "$formatted_output" ]]; then
59+
printf "$formatted_output\n"
60+
return 0
61+
else
62+
return 2
63+
fi
64+
}
65+
4166
find_notes() {
4267
local find_output=$(find "$notes_dir" -ipath "$notes_dir/*$**" -type f 2>&1)
4368
local find_result=$?
@@ -69,8 +94,24 @@ grep_notes() {
6994
fi
7095
}
7196

97+
generate_name() {
98+
local append_num=0
99+
local format_string="`date +$QUICKNOTE_FORMAT`"
100+
# Initial test has no append
101+
local resolved_name=$format_string
102+
while [[ -e "$notes_dir/$resolved_name.md" ]]
103+
do
104+
append_num=$[$append_num+1]
105+
resolved_name=$format_string.$append_num
106+
done
107+
printf $resolved_name
108+
}
109+
72110
new_note() {
73111
local note_name="$*"
112+
if [[ $note_name == "" ]]; then
113+
note_name="$(generate_name)"
114+
fi
74115
mkdir -p "$(dirname "$notes_dir/$note_name")"
75116
open_note "$note_name.md"
76117
}
@@ -130,6 +171,7 @@ Usage:
130171
notes ls <pattern> # List notes by path
131172
notes find|f [pattern] # Search notes by filename and path
132173
notes grep|g <pattern> # Search notes by content
174+
notes search|s [pattern] # Search notes by filename or content
133175
notes open|o # Open your notes directory
134176
notes open|o <name> # Open a note for editing by full name
135177
notes rm [-r | --recursive] <name> # Remove note, or folder if -r or --recursive is given
@@ -160,6 +202,9 @@ main() {
160202
"ls" )
161203
cmd="ls_notes"
162204
;;
205+
"search"|"s" )
206+
cmd="search_filenames_and_contents"
207+
;;
163208
"find"|"f" )
164209
cmd="find_notes"
165210
;;

test/test-config.bats

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,19 @@ teardown() {
1212
teardownNotesEnv
1313
}
1414

15+
export EDITOR=touch
1516
notes="./notes"
1617

18+
@test "Configuration should override QUICKNOTE_FORMAT" {
19+
mkdir -p $HOME/.config/notes
20+
echo "QUICKNOTE_FORMAT=test" > $HOME/.config/notes/config
21+
22+
run $notes new
23+
24+
assert_success
25+
assert_exists "$NOTES_DIRECTORY/test.md"
26+
}
27+
1728
@test "Configuration should override EDITOR" {
1829
mkdir -p $HOME/.config/notes
1930
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

test/test-search.bats

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
#!./libs/bats/bin/bats
2+
3+
load 'libs/bats-support/load'
4+
load 'libs/bats-assert/load'
5+
load 'helpers'
6+
7+
setup() {
8+
setupNotesEnv
9+
}
10+
11+
teardown() {
12+
teardownNotesEnv
13+
}
14+
15+
notes="./notes"
16+
17+
@test "Should output nothing and return non-zero if there are no notes to search" {
18+
run $notes search
19+
20+
assert_failure
21+
echo $output
22+
assert_equal $(echo $output | wc -w) 0
23+
}
24+
25+
@test "Should show all notes found if no pattern is provided to search" {
26+
touch $NOTES_DIRECTORY/note1.md
27+
touch $NOTES_DIRECTORY/note2.md
28+
29+
run $notes search
30+
assert_success
31+
assert_line "note1.md"
32+
assert_line "note2.md"
33+
}
34+
35+
@test "Should search notes when using the search shorthand alias" {
36+
touch $NOTES_DIRECTORY/note.md
37+
38+
run $notes s
39+
assert_success
40+
assert_line "note.md"
41+
}
42+
43+
@test "Should show matching notes only if a pattern is provided to search" {
44+
touch $NOTES_DIRECTORY/match-note1.md
45+
touch $NOTES_DIRECTORY/hide-note2.md
46+
47+
run $notes search "match"
48+
49+
assert_success
50+
assert_line "match-note1.md"
51+
refute_line "hide-note2.md"
52+
}
53+
54+
@test "Should match notes case insensitively with search" {
55+
touch $NOTES_DIRECTORY/MATCH-note1.md
56+
touch $NOTES_DIRECTORY/hide-note2.md
57+
58+
run $notes search "match"
59+
60+
assert_success
61+
assert_line "MATCH-note1.md"
62+
refute_line "hide-note2.md"
63+
}
64+
65+
@test "Should match subdirectory or file names with search" {
66+
touch "$NOTES_DIRECTORY/hide-note.md"
67+
mkdir "$NOTES_DIRECTORY/match-directory"
68+
touch "$NOTES_DIRECTORY/match-directory/note.md"
69+
70+
run $notes search "match"
71+
72+
assert_success
73+
assert_output "match-directory/note.md"
74+
}
75+
76+
# These are the 'grep' tests.
77+
78+
@test "Should match only the files containing the given pattern when searching" {
79+
echo "my-pattern" > $NOTES_DIRECTORY/matching-note.md
80+
echo "some-other-pattern" > $NOTES_DIRECTORY/non-matching-note.md
81+
82+
run $notes search my-pattern
83+
84+
assert_success
85+
assert_line "matching-note.md"
86+
refute_line "non-matching-note.md"
87+
}
88+
89+
@test "Should search notes when using the search shorthand alias" {
90+
echo "my-pattern" > $NOTES_DIRECTORY/matching-note.md
91+
92+
run $notes s my-pattern
93+
94+
assert_success
95+
assert_line "matching-note.md"
96+
}
97+
98+
@test "Should search case-insensitively" {
99+
echo "LETTERS" > $NOTES_DIRECTORY/matching-note.md
100+
101+
run $notes search letter
102+
103+
assert_success
104+
assert_line "matching-note.md"
105+
}
106+
107+
@test "Should search files with paths including spaces" {
108+
mkdir "$NOTES_DIRECTORY/path with spaces"
109+
echo 'match' > "$NOTES_DIRECTORY/path with spaces/note file.md"
110+
111+
run $notes search match
112+
113+
assert_success
114+
assert_output "path with spaces/note file.md"
115+
}

0 commit comments

Comments
 (0)