-
Notifications
You must be signed in to change notification settings - Fork 11
/
snotes
127 lines (107 loc) · 3.51 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
#!/bin/bash
#----------------------------------------------------------------------------------
# Project Name - miscellaneous/snotes (Search Notes)
# Started On - Fri 9 Feb 00:07:19 GMT 2018
# Last Change - Sun 11 Mar 08:54:42 GMT 2018
# Author E-Mail - terminalforlife@yahoo.com
# Author GitHub - https://github.com/terminalforlife
#----------------------------------------------------------------------------------
# To use your own comment character, change the COM variable. This feature will
# eventually be included into snotes properly.
#----------------------------------------------------------------------------------
_VERSION_="2018-03-11"
XERR(){ printf "[L%0.4d] ERROR: %s\n" "$1" "$2" 1>&2; exit 1; }
ERR(){ printf "[L%0.4d] ERROR: %s\n" "$1" "$2" 1>&2; }
COM="#"
USAGE(){
while read -r; do
printf "%s\n" "$REPLY"
done <<-EOF
SNOTES - SEARCH NOTES ($_VERSION_)
Written by terminalforlife (terminalforlife@yahoo.com)
Search your programming notes on-the-fly with this simple tool.
SYNTAX: snotes [OPTS] MATCH
OPTS: --help|-h|-? - Displays this help information.
--version|-v - Output only the version datestamp.
--debug|-D - Enables the built-in bash debugging.
--file|-f PATH - Specify and remember the notes file to use.
--edit|-e CMD - Open up the notes file with CMD for editing.
If no CMD is provided, the one saved in the
snotes configuration file will be used.
NOTE: Uses regular expressions for MATCH. Comment lines are ignored. To tell
snotes when you wish to stop searching for the MATCH, have a line in
the notes file which contains only:
When using --edit|-e for editing a file, the editor used will be the
one pointed to within the
${COM}END
EOF
}
while [ "$1" ]; do
case "$1" in
--help|-h|-\?)
USAGE; exit 0 ;;
--version|-v)
printf "%s\n" "$_VERSION_"
exit 0 ;;
--debug|-D)
DEBUGME="true" ;;
--file|-f)
shift; FILE="$1" ;;
--edit|-e)
shift; EDIT="$1" ;;
-*)
XERR "$LINENO" "Incorrect argument(s) specified." ;;
*)
break ;;
esac
shift
done
if [ "$2" ]; then
XERR "$LINENO" "Argument error -- is your match correct?"
fi
[ "$DEBUGME" == "true" ] && set -x
SNOTESCHK(){
if ! [ -f "$FILE" -a -r "$FILE" ]; then
XERR "$LINENO" "Notes file inaccessible or not found."
fi
}
if [ -f $HOME/.config/snotes.conf ]; then
if [ "$FILE" ]; then
SNOTESCHK
read -n 1 -e -p "Notes file already exists -- replace? "
if ! [[ "$REPLY" == [Yy] ]]; then
printf "No action taken -- quitting.\n"
exit 0
else
if ! [ -d $HOME/.config ]; then
if [ -x /bin/mkdir ]; then
/bin/mkdir $HOME/.config
else
XERR "$LINENO" "Dependency '/bin/mkdir' not met."
fi
fi
printf "%q\n" "$FILE" > $HOME/.config/snotes.conf
fi
else
FILE="$(< $HOME/.config/snotes.conf)"
fi
else
printf "$FILE\n" > $HOME/.config/snotes.conf
fi
SNOTESCHK
MATCH="$1"
declare -i LINE_COUNT=0
while read -r LINE; do
LINE_COUNT+=1
[ "$LINE" == "${COM}END" ] && break
if [[ "$LINE" =~ ^(|${COM}.*)$ ]]; then
continue
elif [[ "$LINE" =~ $MATCH ]]; then
if [ "$LINE_NUMBERS" == "true" ]; then
printf "%4d %s\n" "$LINE_COUNT" "$LINE"
else
printf "%s\n" "$LINE"
fi
fi
done < "$FILE"
# vim: noexpandtab colorcolumn=84 tabstop=8 noswapfile nobackup