1
1
#! /usr/bin/env bash
2
2
3
+ QUICKNOTE_FORMAT=" quicknote-%Y-%m-%d"
4
+ PREFERED_EXT=.md
3
5
GREP_COLOR=always
4
6
5
7
# Look for configuration file at ~/.config/notes/config and use it
6
8
if [ -f ~ /.config/notes/config ]; then
7
9
. ~ /.config/notes/config
8
10
fi
9
11
12
+ file_ext=$PREFERED_EXT
10
13
configured_dir=${NOTES_DIRECTORY%/ } # Remove trailing slashes
11
14
notes_dir=" ${configured_dir:- $HOME / notes} "
12
15
escaped_notes_dir=" $( printf " $notes_dir " | sed -e ' s/[]\/$*.^|[]/\\&/g' ) "
@@ -59,17 +62,17 @@ grep_notes() {
59
62
return 1
60
63
fi
61
64
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
73
76
74
77
local grep_result=$?
75
78
local formatted_output=$( printf " $grep_output " | without_notes_dir)
@@ -85,33 +88,37 @@ grep_notes() {
85
88
new_note () {
86
89
local note_name=" $* "
87
90
mkdir -p " $( dirname " $notes_dir /$note_name " ) "
88
- open_note " $note_name .md "
91
+ open_note " $note_name$file_ext "
89
92
}
90
93
91
94
remove_note () {
92
95
local rm_args=()
96
+ local files=" $@ "
97
+
93
98
if [[ " $1 " == " -r" || " $1 " == " --recursive" ]]; then
94
99
rm_args+=(" --recursive" )
95
100
shift
96
101
fi
97
102
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 "
100
106
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
105
112
}
106
113
107
114
open_something () {
108
115
if [[ -p /dev/stdin ]]; then
109
116
read -d' \n' note_names
110
- local buffer=()
117
+ local buffer=()
111
118
while read note_name; do
112
119
buffer+=$note_name
113
120
done <<< " $note_names"
114
- open_note $note_names
121
+ open_note $note_names
115
122
elif [ $# -gt 0 ]; then
116
123
open_note " $* "
117
124
else
@@ -121,28 +128,37 @@ open_something() {
121
128
122
129
open_note () {
123
130
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
140
156
141
157
$EDITOR $buffer < /dev/tty
142
158
}
143
159
144
160
usage () {
145
- cat << EOF
161
+ cat << EOF
146
162
notes is a command line note taking tool.
147
163
148
164
Usage:
0 commit comments