Skip to content

Commit 0c62c70

Browse files
committed
Update open and remove commands
1 parent 5fd2928 commit 0c62c70

File tree

1 file changed

+53
-37
lines changed

1 file changed

+53
-37
lines changed

notes

Lines changed: 53 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
#!/usr/bin/env bash
22

3+
QUICKNOTE_FORMAT="quicknote-%Y-%m-%d"
4+
PREFERED_EXT=.md
35
GREP_COLOR=always
46

57
# Look for configuration file at ~/.config/notes/config and use it
68
if [ -f ~/.config/notes/config ]; then
79
. ~/.config/notes/config
810
fi
911

12+
file_ext=$PREFERED_EXT
1013
configured_dir=${NOTES_DIRECTORY%/} # Remove trailing slashes
1114
notes_dir="${configured_dir:-$HOME/notes}"
1215
escaped_notes_dir="$(printf "$notes_dir" | sed -e 's/[]\/$*.^|[]/\\&/g')"
@@ -59,17 +62,17 @@ grep_notes() {
5962
return 1
6063
fi
6164

62-
local grep_output
63-
if [[ -t 1 ]]; then
64-
matches=$(grep --color=$GREP_COLOR -r $NOTES_DIRECTORY -in -e "$*" 2>&1)
65-
IFS=$'\n'
66-
for result in $matches; do
67-
len=${#result}
68-
grep_output+="$(echo ${result})\n"
69-
done
70-
else
71-
grep_output=$(grep -r "$notes_dir" -li -e "$*" 2>&1)
72-
fi
65+
local grep_output
66+
if [[ -t 1 ]]; then
67+
matches=$(grep --color=$GREP_COLOR -r $NOTES_DIRECTORY -in -e "$*" 2>&1)
68+
IFS=$'\n'
69+
for result in $matches; do
70+
len=${#result}
71+
grep_output+="$(echo ${result})\n"
72+
done
73+
else
74+
grep_output=$(grep -r "$notes_dir" -li -e "$*" 2>&1)
75+
fi
7376

7477
local grep_result=$?
7578
local formatted_output=$(printf "$grep_output" | without_notes_dir)
@@ -85,33 +88,37 @@ grep_notes() {
8588
new_note() {
8689
local note_name="$*"
8790
mkdir -p "$(dirname "$notes_dir/$note_name")"
88-
open_note "$note_name.md"
91+
open_note "$note_name$file_ext"
8992
}
9093

9194
remove_note() {
9295
local rm_args=()
96+
local files="$@"
97+
9398
if [[ "$1" == "-r" || "$1" == "--recursive" ]]; then
9499
rm_args+=("--recursive")
95100
shift
96101
fi
97102

98-
local note_name="$*"
99-
local to_remove="$notes_dir/$note_name"
103+
for files; do
104+
local note_name="$*"
105+
local to_remove="$notes_dir/$note_name"
100106

101-
if [ -f "$notes_dir/$note_name.md" ]; then
102-
to_remove="$notes_dir/$note_name.md"
103-
fi
104-
rm "${rm_args[@]}" "$to_remove"
107+
if [ -f "$notes_dir/$note_name$file_ext" ]; then
108+
to_remove="$notes_dir/$note_name$file_ext"
109+
fi
110+
rm "${rm_args[@]}" "$to_remove"
111+
done
105112
}
106113

107114
open_something() {
108115
if [[ -p /dev/stdin ]]; then
109116
read -d'\n' note_names
110-
local buffer=()
117+
local buffer=()
111118
while read note_name; do
112119
buffer+=$note_name
113120
done <<< "$note_names"
114-
open_note $note_names
121+
open_note $note_names
115122
elif [ $# -gt 0 ]; then
116123
open_note "$*"
117124
else
@@ -121,28 +128,37 @@ open_something() {
121128

122129
open_note() {
123130
local note_path
124-
local buffer=()
125-
126-
for arg in $@; do
127-
note_path=$arg
128-
if [[ "$note_path" != *.md ]]; then
129-
note_path="$note_path.md"
130-
fi
131-
if [ ! -f "$note_path" ]; then
132-
note_path="$notes_dir/$note_path"
133-
fi
134-
if [ -z "$EDITOR" ]; then
135-
printf "Please set \$EDITOR to edit notes\n"
136-
exit 1
137-
fi
138-
buffer+="$note_path "
139-
done
131+
local ext_check
132+
local buffer=()
133+
134+
135+
for arg in $@; do
136+
137+
ext_check=true
138+
note_path=$arg
139+
140+
if echo $note_path | grep -q '\.[a-z]'; then
141+
ext_check=false
142+
fi
143+
144+
if [[ "$note_path" != *$file_ext ]] && $ext_check; then
145+
note_path="$note_path$file_ext"
146+
fi
147+
if [ ! -f "$note_path" ]; then
148+
note_path="$notes_dir/$note_path"
149+
fi
150+
if [ -z "$EDITOR" ]; then
151+
printf "Please set \$EDITOR to edit notes\n"
152+
exit 1
153+
fi
154+
buffer+="$note_path "
155+
done
140156

141157
$EDITOR $buffer < /dev/tty
142158
}
143159

144160
usage() {
145-
cat <<EOF
161+
cat <<EOF
146162
notes is a command line note taking tool.
147163
148164
Usage:

0 commit comments

Comments
 (0)