Skip to content

Commit 5fd2928

Browse files
committed
Handle multiple buffers better
1 parent d35d2c6 commit 5fd2928

File tree

1 file changed

+39
-19
lines changed

1 file changed

+39
-19
lines changed

notes

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#!/usr/bin/env bash
22

3+
GREP_COLOR=always
4+
35
# Look for configuration file at ~/.config/notes/config and use it
4-
if [ -f ~/.config/notes/config ]; then
6+
if [ -f ~/.config/notes/config ]; then
57
. ~/.config/notes/config
68
fi
79

@@ -15,7 +17,7 @@ if [ -z "$EDITOR" ] && type editor &>/dev/null; then
1517
fi
1618

1719
without_notes_dir() {
18-
cat | sed -e "s/^$escaped_notes_dir//g" | sed -E "s/^\/+//g"
20+
cat | sed -e "s/$escaped_notes_dir//g" | sed -E "s/^\/+//g"
1921
}
2022

2123
ls_notes() {
@@ -53,11 +55,22 @@ find_notes() {
5355

5456
grep_notes() {
5557
if [ ! "$#" -gt 0 ]; then
56-
printf "Grep requires a pattern, but none was provided."
58+
printf "Grep requires a pattern, but none was provided.\n"
5759
return 1
5860
fi
5961

60-
local grep_output=$(grep -r "$notes_dir" -li -e "$*" 2>&1)
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
73+
6174
local grep_result=$?
6275
local formatted_output=$(printf "$grep_output" | without_notes_dir)
6376

@@ -94,9 +107,11 @@ remove_note() {
94107
open_something() {
95108
if [[ -p /dev/stdin ]]; then
96109
read -d'\n' note_names
110+
local buffer=()
97111
while read note_name; do
98-
open_note "$note_name"
112+
buffer+=$note_name
99113
done <<< "$note_names"
114+
open_note $note_names
100115
elif [ $# -gt 0 ]; then
101116
open_note "$*"
102117
else
@@ -105,20 +120,25 @@ open_something() {
105120
}
106121

107122
open_note() {
108-
local note_path=$1
109-
110-
if [[ "$note_path" != *.md ]]; then
111-
note_path="$note_path.md"
112-
fi
113-
if [ ! -f "$note_path" ]; then
114-
note_path="$notes_dir/$note_path"
115-
fi
116-
if [ -z "$EDITOR" ]; then
117-
printf "Please set \$EDITOR to edit notes\n"
118-
exit 1
119-
fi
120-
121-
$EDITOR "$note_path" < /dev/tty
123+
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
140+
141+
$EDITOR $buffer < /dev/tty
122142
}
123143

124144
usage() {

0 commit comments

Comments
 (0)