-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathalmanac
executable file
·236 lines (219 loc) · 5.7 KB
/
almanac
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
#!/bin/bash
#
# This is almanac, a small personal loggind script for linux systems.
# I made this for my own personal use so i could have a lightweight CLI
# tool to quicky jot down progress on projects. If you find it usefull
# that is good too.
#
#
# Print the help to the screen
#
print_help() {
echo -e "Logging utility for simple life logging."
echo -e "The config file is in ~/.almanac.d/config"
echo -e "Currently you can only set the default subject in the config file, it defaults to personal\n"
echo -e "Adding to the log:"
echo -e "Create a log entry with the default subject"
echo -e " almanac -m \"this is the message to log\""
echo -e "Create an entry with an associated subject."
echo -e " almanac -s subject -m \"this is the message\"\n"
echo -e "Displaying the log:"
echo -e "Display the whole log with"
echo -e " almanac -A"
echo -e "Display all entries with a given subject with"
echo -e " almanac -S subject"
echo -e "You can also search with year and month. To get all entries with the subject of work in august 2019 do"
echo -e " almanac -S work -M 08 -Y 19"
}
#
# Input a line into the log file
#
input_log() {
if [ -z ${SUBJECT} ]; then
echo -e "$ENTRY_DATE ($DEFAULT_SUBJECT) $MESSAGE" >> $LOG_FILE
else
echo -e "$ENTRY_DATE ($SUBJECT) $MESSAGE" >> $LOG_FILE
if ! grep -q $SUBJECT $TAG_FILE; then
echo $SUBJECT >> $TAG_FILE
fi
fi
}
#
# If the search file does not exist then create it with the contents of the log
#
init_searchfile() {
if [ ! -f $SEARCH_FILE ]; then
cp $LOG_FILE $SEARCH_FILE
fi
}
#
# Counts the number posts for each tag and diplays this information
# Also displays the total number of entries
#
count_tags() {
printf "\n"
while IFS= read -r line; do
num= grep -o '('$line')' $LOG_FILE | wc -l | tr '\n' '\0'
echo "${num} $line"
done < $TAG_FILE
printf "\nTotal entries: "
< $LOG_FILE wc -l
}
#
# Variable declarations
#
DATA_DIR=$HOME"/.almanac.d"
LOG_FILE=$DATA_DIR"/log"
CONFIG_FILE=$DATA_DIR"/config"
SEARCH_FILE=$DATA_DIR"/search"
TAG_FILE=$DATA_DIR"/tags"
TEMP_FILE=$DATA_DIR"/temp"
ENTRY_DATE=`date +"%d%m%y%H%M%S"`
REMOVE_FILE=$DATA_DIR"/rfile"
#
# Create the logger directory if it does not already exist
#
mkdir -p $DATA_DIR
#
# Make a default settings file if not existing
#
if [ ! -f $CONFIG_FILE ]; then
echo "DEFAULT_SUBJECT=\"personal\"" >> $CONFIG_FILE
fi
#
# Load the config file
#
. $CONFIG_FILE
#
# Create the log file if one does not exist
#
if [ ! -f $LOG_FILE ]; then
touch $LOG_FILE
fi
#
# Create the tag file if one does not exist
#
if [ ! -f $TAG_FILE ]; then
echo $DEFAULT_SUBJECT >> $TAG_FILE
fi
#
# Parse the command line arguments
#
for i in "$@"
do
key=$1
case $key in
-m|--message)
MESSAGE="$2"
shift # past argument
shift # past value
;;
-s|--subject)
SUBJECT="$2"
shift # past argument
shift # past value
;;
-h|--help)
print_help
shift # past argument
;;
-S|--search-subject)
STERM="$2"
init_searchfile
grep --color -E '\('$STERM'\)' $SEARCH_FILE > $TEMP_FILE
rm $SEARCH_FILE
mv $TEMP_FILE $SEARCH_FILE
shift # past arg
shift # past value
;;
-Y|--search-year)
YTERM="$2"
init_searchfile
grep --color -E '[0-9]{4}'$YTERM'[0-9]{6}' $SEARCH_FILE > $TEMP_FILE
rm $SEARCH_FILE
mv $TEMP_FILE $SEARCH_FILE
shift # past arg
shift # past value
;;
-M|--search-month)
MTERM="$2"
init_searchfile
grep --color -E '[0-9]{2}'$MTERM'[0-9]{8}' $SEARCH_FILE > $TEMP_FILE
rm $SEARCH_FILE
mv $TEMP_FILE $SEARCH_FILE
shift # past arg
shift # past value
;;
-D|--search-day)
DTERM="$2"
init_searchfile
grep --color -E $DTERM'[0-9]{10}' $SEARCH_FILE > $TEMP_FILE
rm $SEARCH_FILE
mv $TEMP_FILE $SEARCH_FILE
shift # past arg
shift # past value
;;
-A|--list-all)
grep --color -E '[0-9]{12}' $LOG_FILE
shift
;;
-T|--tags)
cat $TAG_FILE
shift
;;
-C|--count-tags)
count_tags
shift
;;
-l|--last-entry)
awk '/./{line=$0} END{print line}' $LOG_FILE | grep --color -E '[0-9]{12}'
shift
;;
--remove-entry)
RPATTERN="$2"
grep --color "^$RPATTERN" $LOG_FILE
read -p "Really remove these from the log? [y/N] " choice
case "$choice" in
y|Y)
grep -v "^$RPATTERN" $LOG_FILE > $REMOVE_FILE
rm $LOG_FILE
mv $REMOVE_FILE $LOG_FILE
echo "Even though it is removed from the almanac, it remains part of your life."
;;
*)
echo "A wise choice. There are no mistakes in the almanac, just lessons."
;;
esac
shift # past argument
shift # past value
;;
--default)
DEFAULT=YES
shift # past argument with no value
;;
*)
# unknown option
;;
esac
done
#
# If a search file exists display it and then remove it
#
if [ -f $SEARCH_FILE ]; then
grep --color -E '[0-9]{12}' $SEARCH_FILE
rm $SEARCH_FILE
fi
#
# Remove a loftover temp file if it exists
#
if [ -f $TEMP_FILE ]; then
rm $TEMP_FILE
fi
#
# Set a message if one has been input
#
if [ -z "$MESSAGE" ]; then
exit 0
else
input_log
fi