forked from MiXXiM/miscellaneous
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnotes
239 lines (201 loc) · 5.13 KB
/
snotes
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
#!/usr/bin/env bash
#cito M:755 O:0 G:0 T:/usr/local/bin/snotes
#------------------------------------------------------------------------------
# Project Name - ShellProjects/source/snotes
# Started On - Fri 9 Feb 00:07:19 GMT 2018
# Last Change - Sat 1 Jul 23:08:55 BST 2023
# Author E-Mail - [email protected]
# Author GitHub - https://github.com/terminalforlife
#------------------------------------------------------------------------------
# View and filter notes. Snotes is intended for programmers, but can absolutely
# be used outside of that realm.
#
# Features:
#
#TODO: Find a way to incorporate a multi-character comment string.
#
# Bugs:
#
# N/A
#
# Dependencies:
#
# bash (>= 4.1)
# coreutils (>= 8.28)
#------------------------------------------------------------------------------
CurVer='2023-07-01'
Progrm='snotes'
Usage() {
read -d '' <<-EOF
Usage: $Progrm [OPTS] [STRING]
-h, --help - Displays this help information.
-v, --version - Output only the version datestamp.
-C, --no-color - Disable ANSI color escape sequences.
-c, --comment CHAR - Use CHAR as the single comment character.
-f, --file PATH - Specify & remember the notes file to use.
-n, --line-number - Print line numbers in grep(1) style.
-r, --random - Display random note from results.
The location of the file read by ${Progrm^} is saved here:
${SnotesFile/$HOME/\~}
Comment lines are ignored. To tell $Progrm when you wish to stop
searching for STRING, add a line in the file containing only:
${Comment}END
The default comment character is '$Comment'.
The use of '--' to ignore proceeding flags is supported. The use of
'-' to read from STDIN, instead of the assigned file, is also
supported.
EOF
printf '%s' "$REPLY"
}
Err() {
printf 'Err: %s\n' "$2" 1>&2
(( $1 > 0 )) && exit $1
}
BRed=$'\e[91m'
Green=$'\e[32m'
Cyan=$'\e[36m'
Reset=$'\e[0m'
Random=
NoColors=
ReadSTDIN=
Comment='#'
LineNumber=
SnotesFile="$HOME/.config/$Progrm.conf"
Skip=
Args=()
while [[ -n $1 ]]; do
case $1 in
--)
Args+=(--)
Skip='True' ;;
-[^-]*)
if [[ $Skip == True ]]; then
Args+=("$1")
else
Str=${1#-}
Len=${#Str}
for (( Index = 0; Index < Len; Index++ )); {
Args+=(-"${Str:Index:1}")
}
fi ;;
*)
Args+=("$1") ;;
esac
shift
done
set -- "${Args[@]}"
while [[ -n $1 ]]; do
case $1 in
--)
shift
break ;;
-)
ReadSTDIN='True' ;;
--help|-h)
Usage; exit 0 ;;
--version|-v)
printf '%s\n' "$CurVer"; exit 0 ;;
-C|--no-color|--no-colour)
NoColors='True' ;;
--comment|-c)
ArgComment=$1
if [[ -z $2 ]]; then
Err 1 "Option '$ArgComment' requires an argument."
elif (( ${#2} > 1 )); then
Err 1 "Option '$ArgComment' only supports a single character."
else
Comment=$2
shift
fi ;;
--file|-f)
ArgFile=$1
if [[ -z $2 ]]; then
Err 1 "Option '$ArgFile' requires an argument."
else
NotesFile=$2
shift
fi ;;
--line-number|-n)
LineNumber='True' ;;
--random|-r)
Random='True' ;;
-*)
Err 1 'Incorrect option(s) specified.' ;;
*)
break ;;
esac
shift
done
if ! type -P mkdir &> /dev/null; then
Err 1 "Dependency 'mkdir' not met."
fi
(( $# > 1 )) && Err 1 'Multiple matches not supported.'
#---------------------------------------------Process or Create the Snotes File
if [[ -n $NotesFile ]]; then
ConfigDir=${SnotesFile%/*}
[[ -d $ConfigDir ]] || mkdir -p "$ConfigDir"
printf '%s\n' "$NotesFile" > "$SnotesFile"
printf 'NOTE: New location saved.\n'
exit 0
fi
if [[ $ReadSTDIN == True ]]; then
NotesFile='/dev/stdin'
else
[[ -r $SnotesFile ]] || Err 1 "File '$SnotesFile' unreadable."
NotesFile=$(< "$SnotesFile")
if ! [[ -f $NotesFile ]]; then
Err 1 'Notes file not found.'
elif ! [[ -r $NotesFile ]]; then
Err 1 'Notes file unreadable.'
fi
fi
#----------------------------------------------------------------Main Functions
# Usage: PrintLine LINE_NUMBER LINE_DATA
PrintLine() {
if [[ $LineNumber == True ]]; then
local NumberOut="$Green$1$Reset$Cyan:$Reset"
fi
printf '%s%s\n' "$NumberOut" "$2"
}
#--------------------------------------------------------Parse and Display Data
# For use when grabbing random note.
Lines=()
# Also allow removing colors when non-interactive (e.g., pipe).
if [[ $NoColors == True || ! -t 1 ]]; then
BRed=
Green=
Cyan=
Reset=
fi
Match=$1
MatchLen=${#Match}
LineNr=0
while read -r; do
(( LineNr++ ))
# Avoid unnecessary processing, and stop at '#END' line.
if [[ $REPLY == "${Comment}END" ]]; then
break
elif [[ ${REPLY:0:1} == "$Comment" ]]; then
continue
elif [[ -z $REPLY || $REPLY == +([[:space:]]) ]]; then
continue
fi
if [[ $Random == True ]]; then
[[ $REPLY == *"$Match"* ]] || continue
Lines+=("$LineNr|$REPLY")
continue
fi
if (( $# == 0 )); then
PrintLine $LineNr "$REPLY"
else
if [[ $REPLY == *"$Match"* ]]; then
PrintLine $LineNr "${REPLY//"$Match"/$BRed$Match$Reset}"
fi
fi
done < "$NotesFile"
if [[ $Random == True ]]; then
Chosen=${Lines[RANDOM % ${#Lines[@]}]}
LineNr=${Chosen%%|*}
Line=${Chosen#*|}
PrintLine $LineNr "$Line"
fi