Skip to content

Commit c680383

Browse files
committed
Merge remote-tracking branch 'upstream/master'
Signed-off-by: primis <nick@primis.org>
2 parents 0503945 + 87a96e7 commit c680383

File tree

4 files changed

+157
-17
lines changed

4 files changed

+157
-17
lines changed

README.md

Lines changed: 8 additions & 11 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 Bash completion
3333

@@ -47,12 +47,9 @@ You'll need to open a new shell for this to take effect.
4747

4848
## How do I configure this?
4949

50-
You can set your favourite text editor and your notes directory by setting the `$EDITOR` and `$NOTES_DIRECTORY` environmental variables.
50+
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.
5151

52-
53-
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.
54-
55-
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.
52+
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.
5653

5754
### What are the configuration options?
5855

@@ -66,10 +63,7 @@ Right now this mainly exists in case you want to use a different `EDITOR` for no
6663

6764
### `notes new <note-name>`
6865

69-
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`.
70-
71-
### `notes new`
72-
Creates a quicknote with the name of the format `quicknote-YYYY-MM-DD.md`. This can be changed in the configuration file.
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`.
7367

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

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

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

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+
8280
### `notes ls <directory>`
8381

8482
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.
@@ -103,7 +101,6 @@ Combine these together! This opens each matching note in your `$EDITOR` in turn.
103101

104102
All the above works. Here's what's coming next:
105103

106-
- [ ] Combining find and grep, to match either one (https://github.com/pimterry/notes/issues/16)
107104
- [ ] More interesting and nicer looking file/grep search result formatting, perhaps only when not piping? (https://github.com/pimterry/notes/issues/27)
108105
- [ ] Make the file extension optional (https://github.com/pimterry/notes/issues/24)
109106
- [ ] zsh command and note name autocompletion (https://github.com/pimterry/notes/issues/25)

notes

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
QUICKNOTE_FORMAT="quicknote-%Y-%m-%d"
55
NOTES_EXT="md"
66
# Look for configuration file at ~/.config/notes/config and use it
7-
if [ -f ~/.config/notes/config ]; then
7+
if [ -f ~/.config/notes/config ]; then
88
. ~/.config/notes/config
99
fi
1010

@@ -41,6 +41,28 @@ ls_notes() {
4141
fi
4242
}
4343

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+
4466
find_notes() {
4567
local find_output=$(find "$notes_dir" -ipath "$notes_dir/*$**" -type f 2>&1)
4668
local find_result=$?
@@ -71,30 +93,31 @@ grep_notes() {
7193
return 2
7294
fi
7395
}
96+
7497
generate_name() {
7598
local append_num=0
7699
local format_string="`date +$QUICKNOTE_FORMAT`"
77100
# Initial test has no append
78-
resolved_name=$format_string
101+
local resolved_name=$format_string
79102
while [[ -e "$notes_dir/$resolved_name.$NOTES_EXT" ]]
80103
do
81104
append_num=$[$append_num+1]
82105
resolved_name=$format_string.$append_num
83106
done
107+
printf $resolved_name
84108
}
85109

86110
new_note() {
87111
local note_name="$*"
88112
if [[ $note_name == "" ]]; then
89-
generate_name
90-
note_name=$resolved_name
113+
note_name="$(generate_name)"
91114
fi
92115
mkdir -p "$(dirname "$notes_dir/$note_name")"
93116
open_note "$note_name.$NOTES_EXT"
94117
}
95118

96119
remove_note() {
97-
local rm_arga=()
120+
local rm_args=()
98121
if [[ "$1" == "-r" || "$1" == "--recursive" ]]; then
99122
rm_args+=("--recursive")
100123
shift
@@ -148,6 +171,7 @@ Usage:
148171
notes ls <pattern> # List notes by path
149172
notes find|f [pattern] # Search notes by filename and path
150173
notes grep|g <pattern> # Search notes by content
174+
notes search|s [pattern] # Search notes by filename or content
151175
notes open|o # Open your notes directory
152176
notes open|o <name> # Open a note for editing by full name
153177
notes rm [-r | --recursive] <name> # Remove note, or folder if -r or --recursive is given
@@ -178,6 +202,9 @@ main() {
178202
"ls" )
179203
cmd="ls_notes"
180204
;;
205+
"search"|"s" )
206+
cmd="search_filenames_and_contents"
207+
;;
181208
"find"|"f" )
182209
cmd="find_notes"
183210
;;

test/test-config.bats

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@ load 'helpers'
77
setup() {
88
setupNotesEnv
99
}
10+
1011
teardown() {
1112
teardownNotesEnv
1213
}
14+
1315
export EDITOR=touch
1416
notes="./notes"
1517

16-
1718
@test "Configuration should override QUICKNOTE_FORMAT" {
1819
mkdir -p $HOME/.config/notes
1920
echo "QUICKNOTE_FORMAT=test" > $HOME/.config/notes/config

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)