Skip to content

Commit 66b2fa0

Browse files
committed
Clean notes program
1 parent fe395cc commit 66b2fa0

File tree

4 files changed

+28
-98
lines changed

4 files changed

+28
-98
lines changed

_notes

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,5 @@
11
#compdef notes
22
# ------------------------------------------------------------------------------
3-
# Copyright (c) 2016 Github zsh-users - http://github.com/zsh-users
4-
# All rights reserved.
5-
#
6-
# Redistribution and use in source and binary forms, with or without
7-
# modification, are permitted provided that the following conditions are met:
8-
# * Redistributions of source code must retain the above copyright
9-
# notice, this list of conditions and the following disclaimer.
10-
# * Redistributions in binary form must reproduce the above copyright
11-
# notice, this list of conditions and the following disclaimer in the
12-
# documentation and/or other materials provided with the distribution.
13-
# * Neither the name of the zsh-users nor the
14-
# names of its contributors may be used to endorse or promote products
15-
# derived from this software without specific prior written permission.
16-
#
17-
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18-
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19-
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20-
# DISCLAIMED. IN NO EVENT SHALL ZSH-USERS BE LIABLE FOR ANY
21-
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22-
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23-
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24-
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25-
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26-
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27-
# ------------------------------------------------------------------------------
283
# Description
294
# -----------
305
#
@@ -63,15 +38,9 @@ _notes ()
6338
_alternative 'sub-commands:files:__notes_cmd' && _ret=0
6439
elif (($CURRENT == 3)); then
6540
case $words[2] in
66-
<<<<<<< HEAD
67-
open|rm)
68-
_path_files -W "${note_dir}" && _ret=0;;
69-
new|ls)
70-
=======
7141
open|o|rm)
7242
_path_files -W "${note_dir}" && _ret=0;;
7343
new|n|ls)
74-
>>>>>>> develop
7544
_path_files -W "${note_dir}" -/ && _ret=0;;
7645
esac
7746
elif (($CURRENT >= 3)); then
@@ -81,11 +50,3 @@ _notes ()
8150
esac
8251
fi
8352
}
84-
85-
# Local Variables:
86-
# mode: Shell-Script
87-
# sh-indentation: 2
88-
# indent-tabs-mode: nil
89-
# sh-basic-offset: 2
90-
# End:
91-
# vim: ft=zsh sw=2 ts=2 et

notes

Lines changed: 24 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ if [ -f ~/.config/notes/config ]; then
88
. ~/.config/notes/config
99
fi
1010

11-
file_ext=$PREFERED_EXT
1211
configured_dir=${NOTES_DIRECTORY%/} # Remove trailing slashes
1312
notes_dir="${configured_dir:-$HOME/notes}"
1413
escaped_notes_dir="$(printf "$notes_dir" | sed -e 's/[]\/$*.^|[]/\\&/g')"
@@ -79,25 +78,11 @@ find_notes() {
7978

8079
grep_notes() {
8180
if [ ! "$#" -gt 0 ]; then
82-
printf "Grep requires a pattern, but none was provided.\n"
81+
printf "Grep requires a pattern, but none was provided."
8382
return 1
8483
fi
8584

86-
local grep_output
87-
if [[ -t 1 ]]; then
88-
matches=$(grep --color=$GREP_COLOR -r $NOTES_DIRECTORY -in -e "$*" 2>&1)
89-
OLDIFS=$IFS
90-
IFS=$'\n'
91-
for result in $matches; do
92-
len=${#result}
93-
result=$(echo $result|cut -d'/' -f2-)
94-
grep_output+="$(echo /${result})\n"
95-
done
96-
IFS=$OLDIFS
97-
else
98-
grep_output=$(grep -r "$notes_dir" -li -e "$*" 2>&1)
99-
fi
100-
85+
local grep_output=$(grep -r "$notes_dir" -li -e "$*" 2>&1)
10186
local grep_result=$?
10287
local formatted_output=$(printf "$grep_output" | without_notes_dir)
10388

@@ -128,38 +113,31 @@ new_note() {
128113
note_name="$(generate_name)"
129114
fi
130115
mkdir -p "$(dirname "$notes_dir/$note_name")"
131-
open_note "$note_name"
116+
open_note "$note_name.$NOTES_EXT"
132117
}
133118

134119
remove_note() {
135120
local rm_args=()
136-
local len=${#@}
137-
138121
if [[ "$1" == "-r" || "$1" == "--recursive" ]]; then
139122
rm_args+=("--recursive")
140123
shift
141124
fi
142125

143-
len=${#@}
144-
for i in $(seq 1 $len); do
145-
local note_name="$1"
146-
local to_remove="$notes_dir/$note_name"
126+
local note_name="$*"
127+
local to_remove="$notes_dir/$note_name"
147128

148-
if [ -f "$notes_dir/$note_name$file_ext" ]; then
149-
to_remove="$notes_dir/$note_name$file_ext"
150-
fi
151-
rm "${rm_args[@]}" "$to_remove"
152-
shift
153-
done
129+
if [ -f "$notes_dir/$note_name.$NOTES_EXT" ]; then
130+
to_remove="$notes_dir/$note_name.$NOTES_EXT"
131+
fi
132+
rm "${rm_args[@]}" "$to_remove"
154133
}
155134

156135
open_something() {
157136
if [[ -p /dev/stdin ]]; then
158-
read -d $"\n" note_names
137+
read -d'\n' note_names
159138
while read note_name; do
160-
buffer+="${note_name},"
139+
open_note "$note_name"
161140
done <<< "$note_names"
162-
open_note ${buffer[@]}
163141
elif [ $# -gt 0 ]; then
164142
open_note "$*"
165143
else
@@ -168,33 +146,24 @@ open_something() {
168146
}
169147

170148
open_note() {
171-
local note_path
172-
local ext_check
173-
local buffer=$@
174-
local files=()
149+
local note_path=$1
175150

176-
OLDIFS=$IFS; IFS=','
177-
for file in $buffer; do
178-
note_path=$file
179-
ext_check=$(echo ${note_path:1:-1} | grep -e '\.[a-z]')
180-
if [[ -z ${ext_check} ]]; then
181-
note_path="$note_path$file_ext"
182-
fi
183-
if [ ! -f "$note_path" ]; then
184-
note_path="$notes_dir/$note_path"
185-
fi
186-
if [ -z "$EDITOR" ]; then
187-
printf "Please set \$EDITOR to edit notes\n"
188-
exit 1
189-
fi
190-
files+=("${note_path}")
191-
done; IFS=$OLDIFS
151+
if [[ "$note_path" != *.$NOTES_EXT ]]; then
152+
note_path="$note_path.$NOTES_EXT"
153+
fi
154+
if [ ! -f "$note_path" ]; then
155+
note_path="$notes_dir/$note_path"
156+
fi
157+
if [ -z "$EDITOR" ]; then
158+
printf "Please set \$EDITOR to edit notes\n"
159+
exit 1
160+
fi
192161

193-
$EDITOR "${files[@]}" < /dev/tty
162+
$EDITOR "$note_path" < /dev/tty
194163
}
195164

196165
usage() {
197-
cat <<EOF
166+
cat <<EOF
198167
notes is a command line note taking tool.
199168
200169
Usage:

test/test-open.bats

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ notes="./notes"
7070
run bash -c "$notes find | $notes open"
7171

7272
assert_success
73-
assert_line "Editing $NOTES_DIRECTORY/note2.md $NOTES_DIRECTORY/note.md"
74-
# assert_line "Editing $NOTES_DIRECTORY/note2.md"
73+
assert_line "Editing $NOTES_DIRECTORY/note.md"
74+
assert_line "Editing $NOTES_DIRECTORY/note2.md"
7575
}
7676

7777
@test "Accepts relative notes paths to open" {

test/test-rm.bats

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ notes="./notes"
2525
@test "Should fail to delete non-existent note" {
2626
run $notes rm note
2727

28-
assert_success
28+
assert_failure
2929
}
3030

3131
@test "Should remove note in folder" {
@@ -41,7 +41,7 @@ notes="./notes"
4141
mkdir "$NOTES_DIRECTORY/folder"
4242
run $notes rm folder
4343

44-
assert_success
44+
assert_failure
4545
assert_exists "$NOTES_DIRECTORY/folder"
4646
}
4747

0 commit comments

Comments
 (0)