-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathnote.bash
executable file
·252 lines (227 loc) · 7.46 KB
/
note.bash
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
#!/usr/bin/env bash
# shellcheck source="${XDG_CONFIG_HOME:-$HOME/.config}/notekeeper/noterc"
# shellcheck disable=SC1091
YEAR=$(date +'%Y')
MONTH=$(date +'%m')
DAY=$(date +'%d')
# Set default configuration
NOTE_DIR="$HOME/notes"
NOTE_NAME="$YEAR-$MONTH-$DAY.md"
PRINT_TOOL="cat"
organize_by_date=true
# Overwrite default configs from noterc configuration file
NOTERC="${XDG_CONFIG_HOME:-$HOME/.config}/notekeeper/noterc"
if [ -f "$NOTERC" ]; then source "$NOTERC"; fi
BASE_NOTE_DIR=$NOTE_DIR
if [ "$organize_by_date" = true ]; then
NOTE_PATH="$NOTE_DIR/$YEAR/$MONTH/$DAY"
else
NOTE_PATH=$NOTE_DIR
fi
create_note() {
if [ ! -f "$NOTE_PATH/$NOTE_NAME" ]; then
mkdir -p "$NOTE_PATH"
touch "$NOTE_PATH/$NOTE_NAME"
printf "%s-%s-%s\n---\n\n" "$DAY" "$MONTH" "$YEAR" > "$NOTE_PATH/$NOTE_NAME"
printf "Created new note: %s/%s\n" "$NOTE_PATH" "$NOTE_NAME"
fi
}
print_help() {
printf "Notekeeper 1.2 (10 November 2021)
Usage: note [<args>]
Arguments:
-h | --help Display usage guide.
-e | --edit <FILENAME> Open a specific note for editing.
-p | --print Print the contents of a note.
-c | --create Create a note but don't open it.
-n | --name <FILENAME> Use a specific filename rather than the
default name (\$YEAR-\$MONTH-\$DAY.md).
Can be combined with args (-c, -p, -t).
-t | --time Add a timestamp when opening a note.
-d | --delete <FILENAME> Move a note to the trash directory.
--destroy <FILENAME> Permanently delete (rm) a note.
-l | --list [<File Extension>] List note files. Defaults to .md files
if no file extension is specified.
Notekeeper loads configuration variables from:
\$HOME/.config/notekeeper/noterc.
For more help, see: https://github.com/dcchambers/note-keeper\n"
}
open_note() {
printf "Opening note: %s\n" "$1"
if [[ $EDITOR = *"vim"* ]] || [[ $EDITOR = *"nvim"* ]]; then
# Open Vim or Neovim in insert mode.
$EDITOR "+normal G$" +startinsert! "$1"
elif [[ $EDITOR = *"emacs"* ]]; then
# Open Emacs with cursor at EOF.
emacs -nw "$1" --eval "(goto-char (point-max))"
elif [[ $EDITOR = "" ]]; then
# If no default editor, use Vim and open in insert mode.
vim "+normal G$" +startinsert! "$1"
else
$EDITOR "$1"
fi
}
edit_note() {
# If find returns more than 1 result or no results (1 blank char) then
# we return an error.
path_to_note=$(find "$BASE_NOTE_DIR" -name "$1")
if [ "$(echo "$path_to_note" | wc -l)" -gt 1 ]; then
printf "Error: More than one note with that name was found.\n"
printf "Please edit note(s) manually with your editor of choice.\n\n"
printf "Files found:\n\n%s\n" "$path_to_note"
exit 1
elif [ "$path_to_note" = "" ]; then
printf "Unable to find a note with that name in %s.\n" "$BASE_NOTE_DIR"
exit 1
else
open_note "$path_to_note"
fi
}
soft_delete_note() {
if [ ! -d "$BASE_NOTE_DIR/trash" ]; then
mkdir -p "$BASE_NOTE_DIR/trash"
fi
# If find returns more than 1 result or no results (1 blank char) then
# we return an error and exit.
path_to_note=$(find "$BASE_NOTE_DIR" -name "$1")
if [ "$(echo "$path_to_note" | wc -l)" -gt 1 ]; then
printf "Error: More than one note with that name was found.\n"
printf "Please edit note(s) manually with your editor of choice.\n\n"
printf "Files found:\n\n%s\n" "$path_to_note"
exit 1
elif [ "$path_to_note" = "" ]; then
printf "Unable to find a note with that name in %s.\n" "$BASE_NOTE_DIR"
exit 1
else
printf "Moving %s to trash.\n" "$path_to_note"
mv -n "$path_to_note" "$BASE_NOTE_DIR/trash"
exit 0
fi
}
destroy_note() {
# If find returns more than 1 result or no results (1 blank char) then
# we return an error and exit.
path_to_note=$(find "$BASE_NOTE_DIR" -name "$1")
if [ "$(echo "$path_to_note" | wc -l)" -gt 1 ]; then
printf "Error: More than one note with that name was found.\n"
printf "Please edit note(s) manually with your editor of choice.\n\n"
printf "Files found:\n\n%s\n" "$path_to_note"
exit 1
elif [ "$path_to_note" = "" ]; then
printf "Unable to find a note with that name in %s.\n" "$BASE_NOTE_DIR"
exit 1
else
printf "Permanently deleting %s.\n" "$path_to_note"
rm -i "$path_to_note"
exit 0
fi
}
list_notes() {
find "$BASE_NOTE_DIR" -type f -name "*$1" | sed "s@.*/@@" | sort
}
openNote=false
printNoteOnly=false
createNoteOnly=false
if (($# > 0)); then
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
-e | --edit)
if [ "$#" -ne 2 ]; then
printf "Incorrect number of arguments.\n"
printf "Usage: note --edit <FILENAME>\n"
exit 1
fi
NOTE_NAME="$2"
if [ -z "$NOTE_NAME" ]; then
printf "Expected additional argument <Note Filename>.\n"
exit 1
else
edit_note "$NOTE_NAME"
fi
exit 0
;;
-p | --print)
printNoteOnly=true
shift
;;
-c | --create)
createNoteOnly=true
shift
;;
-n | --name)
NOTE_NAME="$2"
openNote=true
if [ -z "$NOTE_NAME" ]; then
printf "Expected additional argument <Note Filename>.\n"
exit 1
fi
shift
shift
;;
-h | --help)
print_help
exit 0
;;
-t | --time)
addTimeStamp=true
openNote=true
shift
;;
-d | --delete)
if [ "$#" -ne 2 ]; then
printf "Incorrect number of arguments.\n"
printf "Usage: note --delete <FILENAME>\n"
exit 1
fi
soft_delete_note "$2"
exit 0
;;
--destroy)
if [ "$#" -ne 2 ]; then
printf "Incorrect number of arguments.\n"
printf "Usage: note --destroy <FILENAME>\n"
exit 1
fi
destroy_note "$2"
exit 0
;;
-l | --list)
if [ "$#" -eq 2 ]; then
file_extension=$2
else
file_extension=".md"
fi
list_notes $file_extension
exit 0
;;
*)
printf "Unknown Argument \"%s\"\n" "$1"
printf "Use \"note --help\" to see usage information.\n"
exit 1
;;
esac
done
else
#no arguments/options, just open the default note.
openNote=true
fi
if [ "$printNoteOnly" = true ]; then
if [ ! -f "$NOTE_PATH/$NOTE_NAME" ]; then
printf "Unable to find a note to print in directory: %s\n" "$NOTE_PATH"
exit 1
fi
$PRINT_TOOL "$NOTE_PATH/$NOTE_NAME"
exit 0
fi
if [ "$createNoteOnly" = true ]; then
create_note
exit 0
fi
if [ "$addTimeStamp" = true ]; then
printf "[%s]\n" "$(date +%T)" >> "$NOTE_PATH/$NOTE_NAME"
fi
if [ "$openNote" = true ]; then
create_note
open_note "$NOTE_PATH/$NOTE_NAME"
fi