-
Notifications
You must be signed in to change notification settings - Fork 0
/
nb
executable file
·267 lines (248 loc) · 6.22 KB
/
nb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#!/bin/bash
VERSION=0.0.1
nb_usage () {
echo "nb [-hV] [-e] [-sl] [name|query]"
echo
echo "Options:"
echo " -h|--help Print this help dialogue and exit"
echo " -V|--version Print the current version and exit"
echo " --env Print completion / function shortcuts for eval"
echo " -e|--encrypt Pass this flag to encrypt/decrypt notes with gpg"
echo " -l|--ls|--list [query] List notes. Optional query arg will filter list"
echo " -s|--search <query> Search notes using query argument"
echo " -m|--move|-r|--rename <name> <newname> Rename note"
echo " -p|--publish <name> Publish note."
}
nb_require () {
local dep=""
for dep in "$@"; do
if ! type "$dep" &>/dev/null; then
echo "required dependency not found: $dep" >&2
if type brew &>/dev/null; then
echo "hint: try 'brew install $dep'" >&2
fi
exit 1
fi
done
}
nb_move () {
local ext="$1"
local from="${2%.*}"
local to="${3%.*}"
local notefile="$dir/$from.$ext"
local destfile="$dir/$to.$ext"
if [[ -e "$destfile" ]]; then
echo "Error: destination note already exists, will not overwrite: $destname"
return 1
fi
mv "$notefile" "$destfile"
echo "Moved: $rootname -> $destname"
}
nb_pub () {
local filename="$1"
local dir="${NB_DIR:-$HOME/notes}"
if [ ! -f "$dir/$filename" ]; then
echo "Error: File not found: $filename" >&2
return 1
fi
local pubdir="${NB_PUB_DIR:-$HOME/pub}"
if [ ! -d "$pubdir" ]; then
mkdir -p "$pubdir"
fi
cd "$pubdir"
if [ ! -d ".git" ]; then
if ! git init; then
echo "Error: could not initialize git repo" >&2
return 1
fi
fi
local branch="${NB_PUB_BRANCH:-public}"
if ! git checkout "$branch"; then
if ! git checkout -b "$branch"; then
echo "Error: could not create branch: $branch" >&2
return 1
fi
fi
cp "$dir/$filename" "$pubdir/$filename"
git add "$filename"
git commit -m "Publish Update: $filename"
if ! git push origin public; then
echo "Error: could not push to origin. Please check your remote settings" >&2
return 1
fi
return 0
}
nb_encrypt () {
local srcfile="$1"
local encfile="$2"
local pass="$3"
echo "$pass" | gpg --batch --passphrase-fd 0 --no-tty --yes --symmetric --output "$encfile" "$srcfile"
}
nb_decrypt () {
local encfile="$1"
local destfile="$2"
local pass="$3"
echo "$pass" | gpg --batch --passphrase-fd 0 --no-tty --yes --output "$destfile" --decrypt "$encfile"
}
nb_env () {
echo "
nbgrep () { nb --search \"\$@\"; }
nbls () { nb --list \"\$@\"; }
nbmove () { nb --move \"\$@\"; }
nbcat () { EDITOR=cat nb \"\$@\"; }
__nb_complete () {
local cur=\${COMP_WORDS[COMP_CWORD]}
COMPREPLY=( \$( compgen -W \"\$(nb --list)\" -- \$cur ) )
return 0
}
complete -o default -o nospace -F __nb_complete nb
complete -o default -o nospace -F __nb_complete nbls
complete -o default -o nospace -F __nb_complete nbgrep
complete -o default -o nospace -F __nb_complete nbcat
complete -o default -o nospace -F __nb_complete nbmove
"
}
nb () {
local dir="${NB_DIR:-$HOME/notes}"
if ! mkdir -p "$dir"; then
echo "Setup error: cannot mkdir -p $dir" >&2
return 1
fi
declare -i local search=0
declare -i local list=0
declare -i local encrypt=0
declare -i local move=0
declare -i local pub=0
local text=""
local query=""
local name=""
local grep_opt=""
local num_args=$#
for arg in "${@}"; do
case "$arg" in
-V|--version)
echo "$VERSION"
return 0
;;
-h|--help)
nb_usage
return 0
;;
--env)
nb_env
return 0
;;
--list|--ls|-l)
list=1
;;
--search|-s)
search=1
;;
-w)
grep_opt="$grep_opt -w"
;;
--encrypt|-e)
encrypt=1
;;
--move|-m|--rename|-r)
move=1
;;
--publish|-p|--pub)
pub=1
;;
-*)
echo "Error: flag not recognized: $arg" >&2
return 1
;;
*)
if [[ -z "$name" ]]; then
name="$arg"
elif [[ -z "$text" ]]; then
text="$arg"
else
text="$text $arg"
fi
;;
esac
done
if (( num_args == 0 )); then
nb_usage
return 1
fi
local notefile=""
local rootname=${name%.*}
local ext=${NOTE_EXT:-md}
if (( encrypt == 1 )); then
ext="gpg"
fi
if [[ "$rootname" != "$name" ]]; then
ext=${name##*.}
fi
if [[ -n "$rootname" ]]; then
for test_ext in 'txt' 'md' 'gpg'; do
if [[ "$ext" != "$test_ext" ]] && [[ -f "$dir/$rootname.$test_ext" ]]; then
ext="$test_ext"
break
fi
done
notefile="$dir/$rootname.$ext"
fi
if [[ "$ext" == "gpg" ]]; then
encrypt=1
fi
(( encrypt == 1 )) && nb_require gpg
if (( move == 1 )); then
nb_move "$ext" "$query" "$text"
return $?
elif (( pub == 1 )); then
nb_pub "$rootname.$ext"
return $?
elif (( search == 1 )); then
query="$name"
if [[ -z "$query" ]]; then
echo "--search requires a query" >&2
nb_usage
return 1
fi
grep $grep_opt -i -R -C 1 "$query" "$dir"
elif (( list == 1 )); then
query="${name:-.}"
for fname in $(ls -c "$dir" | grep "$query"); do
echo "${fname%.*}"
done
else
if [[ -z "$notefile" ]]; then
echo "no notefile specified for edit/append" >&2
nb_usage
return 1
fi
local tmpfile=""
local editfile="$notefile"
local pass=""
if (( encrypt == 1 )); then
echo -n "Enter your password for encryption: "
read -s pass
tmpfile=$(mktemp)
if [[ -f "$notefile" ]]; then
if ! nb_decrypt "$notefile" "$tmpfile" "$pass"; then
echo "Error: notefile could not be decrypted" >&2
return $?
fi
fi
editfile="$tmpfile"
fi
if [[ -n "$text" ]]; then
echo "$text" >> "$editfile"
echo "Appended text to end of $notefile:"
tail -n 5 "$notefile"
else
${EDITOR:-vim} "$editfile"
fi
if (( encrypt == 1 )); then
if ! ( nb_encrypt "$editfile" "$notefile" "$pass" && rm "$editfile" ); then
echo "warning: encryption failed, editfile not removed: $editfile" >&2
fi
fi
fi
}
nb "${@}"