-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnotes
More file actions
executable file
·257 lines (239 loc) · 11.4 KB
/
notes
File metadata and controls
executable file
·257 lines (239 loc) · 11.4 KB
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
#!/bin/bash
#reference table for quicknotes similar to tldr but for specific function calls
#Author: Will Russell (Scotchman0)
#Version: 5.2
#Patch date: 6/16/21
#basic usage: notes [option] [argument]
#options listed below in -h case segment
#arguments are always strings (add quotes for more specific searches or when adding a space between filenames/greps)
#will cat the contents of matching file entry in neighboring ~/bin/reference dir
########################################################################################
#first time run block:
example_note_gen () {
#setting up ~/my-notes/example document with some general syntax help
echo "" >> ~/my-notes/example
echo "welcome to notes!" >> ~/my-notes/example
echo "note's options are modifiable in the ~/notes.conf file" >> ~/my-notes/example
echo "by default, notes opens with 'CAT', but you can use 'LESS' or other text programs" >> ~/my-notes/example
echo "" >> ~/my-notes/example
echo "ways to get started with notes:" >> ~/my-notes/example
echo "you can create a new note with 'notes -n <note_name>" >> ~/my-notes/example
echo "get information about notes with notes -h" >> ~/my-notes/example
echo "and grep for strings inside your notes with 'notes -g <string>" >> ~/my-notes/example
echo "feel free to check the wiki for more data, discussions." >> ~/my-notes/example
echo "have fun with notes!" >> ~/my-notes/example
echo "" >> ~/my-notes/example
echo "to see how this works, try 'notes -g query'" >> ~/my-notes/example
echo "notes also supports tab completion and therefore you can type 'notes e<tab>'" >> ~/my-notes/example
echo "to suggest 'example'" >> ~/my-notes/example
echo "be advised however you need to have this enabled (see readme) by appending your" >> ~/my-notes/example
echo "bashrc profile with: '. ~/.notes.conf" >> ~/my-notes/example
echo "in addition, you will need to restart your shell session to have it populate" >> ~/my-notes/example
echo "" >> ~/my-notes/example
echo "and finally, try notes -n 'my_note' (default: VIM), you can edit this" >> ~/my-notes/example
echo "by opening ~/.notes.conf and change the 'editor=' to NANO, xdg-open, subl" >> ~/my-notes/example
echo "which should open in your default text program, takes commands as args" >> ~/my-notes/example
}
first_run() {
#generate initial conf file + default values for editing later (but will still work if
#user cancels script at this point)
touch ~/.notes.conf
mkdir ~/my-notes
echo "#!/bin/bash" >> ~/.notes.conf
echo "#notes app default settings:" >> ~/.notes.conf
echo "#changing the below values will modify the 'notes' command behavior" >> ~/.notes.conf
echo "mypath=~/my-notes #where you will save notes for query (leave out the trailing slash)" >> ~/.notes.conf
echo "myeditor=vi #default editor for creating new notes with -n option" >> ~/.notes.conf
echo "myreader=cat #cat is default, can change to any other view option like less|subl|glow|<your_reader_app> - accepts quoted commands" >> ~/.notes.conf
echo 'pathlength="$(echo $mypath/ | wc -m)" #trims the start of the path so that -k only shares filenames' >> ~/.notes.conf
echo 'notes_extension= #left blank (default) can update a default for easy export - accepts .md|.log|.txt|etc' >> ~/.notes.conf
echo '#the note_extension can also be left blank, it is not required to set a filetype for notes to read data by default' >> ~/.notes.conf
echo "#the below line is used for auto-completion with tabbing. to enable this feature add the line:" >> ~/.notes.conf
echo "# '. ~/.notes.conf' to the bottom of your ~/.bashrc or ~/.zshrc file" >> ~/.notes.conf
#this reference allows auto-tabbing when referenced by bash profile. is surprisingly difficult to echo this into a file
#therefore split it up into separate parsable bits.
complete1='complete -W "$(q=($mypath/*); '
complete2="sed 's@\.md @ @g' "
complete3='<<<${q[@]##*/})" notes'
echo "${complete1}${complete2}${complete3}" >> ~/.notes.conf
#placing this inside the first_run function means it hopefully shouldn't overwrite 'example' every time
EXAMPLE_NOTE=~/my-notes/example
if [ ! -f "$EXAMPLE_NOTE" ]; then
example_note_gen
echo "created an initial setup note in ~/my-notes/example"
fi
}
auto_complete() {
# #would you like to enable tab completion?
echo "would you like to enable tab completion? (recommended) (y/n)"
read option
case $option in
y|Y) clear
echo "in a separate terminal window run the following line to append your ~/.bashrc or ~/.zshrc profile"
echo ""
echo 'echo ". ~/.notes.conf" >> ~/.bashrc'
echo ""
echo "this will reference the conf file where a completion line is ready formatted for tab completion on shell launch"
echo "press return when ready to continue (then close/open a new shell)"
read placeholder
clear
;;
n|N) echo "no worries, you can always complete later - see README on how to append your bashrc with correct syntax"
;;
*) echo "non y/n answer recieved, defaulting to no change"
;;
esac
}
#checks to see if conf file exists, if it doesn't - assumes program has never been launched before, spins up defaults and
#sets up ~/my-notes folder for library + places an 'example' doc in the folder
FILENAME=~/.notes.conf #define conf
if [ ! -f "$FILENAME" ]; then
first_run
auto_complete
echo "establishing default settings in ~/.notes.conf and creating default save directory in ~/my-notes"
echo ""
echo "try notes -h or 'notes example' for getting started"
fi
#definitions:
. ~/.notes.conf #the conf file that is auto-generated with definitions
package=notes #in case you decide to rename the script command to something else this will make it easy for the -h to make sense
#basic run script
while test $# -gt 0; do #general while loop to lock behavior surrounding case options.
case "$1" in
-h|--help)
echo "$package - your local library of quick notes"
echo " "
echo "notes are currently being saved in $mypath"
echo "$package [options] [arguments]"
echo "configuration settings managed in ~/.notes.conf"
echo "configuration settings managed in ~/.notes.conf"
echo "change options can be set by running 'notes --settings' or manually editing this file"
echo " "
echo "USAGE:"
echo " "
echo "notes {options} {target-note-name}"
echo " "
echo "OPTIONS:"
echo ""
echo " settings opens the ~/.notes.conf file for modification"
echo " -h, --help show brief help"
echo " -l, --list= LIST NOTES list all note titles (tab doesn't populate until next shell launch)"
echo " -g, --grep=GREP STRING greps match + 2 lines following in library (-i is enabled so not exact match)"
echo " -k, --search searches for string match (use quotes) and displays note titles with positive hit"
echo " -n, --new=NEW NOTE/edit opens $myeditor in ${mypath}/<note_name> (sets a filetype extension if set in settings)"
echo " -e, --edit opens $myeditor in ${mypath}/<note_name> but does not append a filetype extension"
echo " -d, --delete deletes note: notes -d <note_name> --> (ask to confirm=true)"
echo " -d! --delete! delete without asking: notes -d! <notename> --> (deleted)"
echo " -p --print | print prints content of note directly to stdout instead of opening it with a reader for copy/paste"
echo " --settings opens the settings panel and allows modification of the default configuration/experience"
echo " "
echo "{target-note-name}: (always a string)"
echo " "
echo "example usage:"
echo " "
echo "$package example #print the content of the note named 'example to stdout (or open with less, if configured in settings)"
echo "$package a + <tab> # lists note names starting with the letter 'a' (if you've enabled autocompletion)"
echo "$package -g welcome #greps for existing notes with the word 'welcome' + two subsequent lines for context and the note name"
echo "$package -n new_note # runs $myeditor at ${mypath}/new_note"
echo ""
echo "try the command: 'notes example'"
echo ""
exit 0
;;
-g|--grep) #cat's inexact match + 2 following lines on context. uses -w flag to ensure word match to escape partial-string match.
#usage: notes -g "<string>" --> pulls matching phrase + 2 appended lines from existing notes + lists note name
shift
if test $# -gt 0; then
#cat $mypath/* | grep -i -A 2 "$@"
grep -i -A 2 -w "$@" $mypath/* | cut -c $pathlength-
else
echo "no match found, try again with quotes or adjust query (or try -k with search parameters)"
exit 1
fi
shift
;;
-n|--new) #create new note or edit existing note (*if specify exact note name)
#usage: notes -n new_note_<date> --> creates a new file and opens it with selected editor (vim/nano)
shift
if test $# -gt 0; then
$myeditor $mypath/"$@""$notes_extension"
else
echo "need to specify a new/existing file name"
fi
shift
;;
-e|--edit) #edit existing note
#usage: notes -e tcpdump_examples --> opens editor selection (vim/nano) on existing file.
shift
if test $# -gt 0; then
$myeditor $mypath/"$@"
else
echo "need to specify an existing filename"
fi
break
;;
-l|--list) #list all note names for reference (useful when you've created new notes and haven't refreshed session for tab-complete)
#usage: notes -l --> list all notes
ls $mypath
break
;;
-k|--search) #search for string and provide note name of notes that match string for review (man search functionality)
#usage: notes -k tcpdump --> list notes that include keyword IN the note contents, not just note-name
shift
if test $# -gt 0; then
grep -i -l "$@" $mypath/* | cut -c $pathlength-
else
echo "no match found, try again with quotes or adjust query"
fi
break
;;
-d|--delete) #delete and ask
#usage: notes -d <notename> --> delete file but ask for confirmation before execution of rm.
shift
echo "delete '$@'? (y/n)"
read answer
case $answer in
y|Y)
rm $mypath/"$@"
shift
echo "deleted!"
;;
n|N)
shift
echo "cancelled delete"
exit 1
;;
esac
;;
-d!|--delete!) #delete without asking
#usage: notes -d! <notename> --> immediate deletion of file selected at path if found.
shift
rm $mypath/"$@"
shift
;;
--settings) #launch editor on local settings panel
#usage: notes --settings --> launch selected editor program (vim/nano) on ~/.notes.conf
shift
$myeditor ~/.notes.conf
break
;;
-p | --print | print) #dump contents of note straight to stdout (useful if you've set default notes command to 'less/more/tail' or another buffer)
#usage: notes -p notes_on_tcpdump --> stdout contents of note.
shift
if test $# -gt 0; then
cat $mypath/"$@" #edit this later to include a variable option for actually PRINTING the file
break
fi
;;
*)
if test $# -gt 0; then
$myreader $mypath/"$@"
break
else
echo "try notes example or 'notes -h'"
break
fi
;;
esac
done
exit 0