-
Notifications
You must be signed in to change notification settings - Fork 0
/
farend.sh
executable file
·326 lines (259 loc) · 6.21 KB
/
farend.sh
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#!/bin/sh
# --------------------------------------
# name: farend
# desc: a simple todo system in shell
# main: jaidyn ann <jadedctrl@posteo.at>
# --------------------------------------
# Take in everything in stdin, then print
# (Useful for piping something into a variable)
function reade {
local stack=""
while read input; do
stack="$(printf '%s\n%s' "$stack" "$input")"
done
echo "$stack"
}
# --------------------------------------
# BASIC MATH
# Add together two numbers
function add {
local a="$1"; local b="$2"
echo "$a + $b" \
| bc
}
# Subtract two numbers
function subtract {
local a="$1"; local b="$2"
echo "$a - $b" \
| bc
}
# Increment a number by one
function inc {
local a="$1"
add 1 "$a"
}
# Decrement a number by one
function dec {
local a="$1"
subtract "$a" 1
}
# Format a number to be a certain amount of digits long
# (done by prepending zeroes)
function digits {
local number="$(reade)"
local digits="$1"
if test "$(dec "$(echo "$number" | wc -c )")" -lt "$(inc "$digits")"
then
local i=1;
while test $i -lt "$digits"; do
printf "0";
i="$(inc "$i")"
done
printf %s $number
else
printf %s $number;
fi
}
# --------------------------------------
# DATE PRIMITIVES
# Return today's date, YYYY-MM-DD
function today {
date +"%Y-%m-%d"
}
# Return the day of a given date
function date_day {
local date="$1"
echo "$date" \
| awk -F "-" '{print $3}'
}
# Return the month of a given date
function date_month {
local date="$1"
echo "$date" \
| awk -F "-" '{print $2}'
}
# Return the year of a given date
function date_year {
local date="$1"
echo "$date" \
| awk -F "-" '{print $1}'
}
# Return how many days ought to be in the given month
function month_days {
local month="$1"
case "$month" in
09) echo 30;;
04) echo 30;;
06) echo 30;;
11) echo 30;;
*) echo 31;;
esac
}
# --------------------------------------
# DATE ARITHMETIC
# Add an amount of days to a given date
function add_days {
local date="$1"
local days_added="$2"
local day="$(date_day "$date")"; local month="$(date_month "$date")"
local year="$(date_year "$date")"
local new_day="$(add "$day" "$days_added" | digits 2)"
carry_date "${year}-${month}-$new_day"
}
# Return whether or not a given date is valid
# (I.E., not too month months or days)
function is_balanced_date {
local date="$1"
local month="$(date_month "$date")"
local day="$(date_day "$date")"
local month_max="$(month_days "$month")"
if test "$month" -gt 12; then
return 1
elif test "$day" -gt "$month_max" ; then
return 1
else
return 0
fi
}
# Correct an unbalanced date
function carry_date {
local date="$1"
is_balanced_date "$date"
while test "$?" -eq 1; do
date="$(carry_months "$(carry_days "$date")")"
done
echo "$date"
}
# If too many days in a given month, carry them over
function carry_days {
local date="$1"
local year="$(date_year "$date")"
local month="$(date_month "$date")"
local days="$(date_day "$date")"
if test "$days" -gt "$(month_days "$month")"; then
local new_days="$(subtract "$days" "$(month_days "$month")")"
local new_month="$(add "$month" "1")"
new_days="$(echo "$new_days" | digits 2)"
new_month="$(echo "$new_month" | digits 2)"
echo "${year}-${new_month}-${new_days}"
else
echo "$date"
fi
}
# If too many months in a year, carry them over
function carry_months {
local date="$1"
local year="$(date_year "$date")"
local day="$(date_day "$date")"
local month="$(date_month "$date")"
if test "$month" -gt 12; then
month="$(subtract "$month" 12 | digits 2)"
year="$(inc "$year")"
fi
echo "${year}-${month}-${day}"
}
# --------------------------------------
# TODO HANDLING
# Clean up a todo file for use with the program
function preprocess_todo {
ignore_todo_blanks \
| demystify_todo_times
}
# Replace piped todo's vague dates with current dates
function demystify_todo_times {
local year="$(date_year "$(today)")"
local month="$(date_month "$(today)")"
local day="$(date_day "$(today)")"
sed 's%^\*-%'"$year"'-%g' \
| sed 's%-\*-%-'"$month"'-%g' \
| sed 's%-\*%-'"$day"'%g'
}
# Filter out comments and blank lines from piped todo
function ignore_todo_blanks {
grep -v "^#" \
| grep -v "^$"
}
# Return all todo lines of the given date
function date_todo_lines {
local date="$1"
grep "$date"
}
# Print all todo lines during the giving days following the start-date
function upcoming_todo_lines {
local todo_file="$1"
local start_date="$2"
local limit="$3"
if test "$limit" -eq 0; then limit="$(inc "$limit")"; fi
local i="0"
while test "$i" -lt "$limit"; do
cat "$todo_file" \
| preprocess_todo \
| date_todo_lines "$(add_days "$start_date" "$i")"
if test "$?" -eq 0 \
-a "$i" -ne "$(dec "$limit")" \
-a "$QUIET_MODE" -ne 0
then echo "---"; fi
i="$(inc "$i")"
done
}
# Print a user-friendly report of upcoming events <3
function generate_report {
local todo_file="$1"
local limit="$2"
local tomorrow="$(add_days "$(today)" 1)"
local today_lines="$(upcoming_todo_lines "$todo_file" "$(today)" 0)"
local later_lines="$(upcoming_todo_lines "$todo_file" "$tomorrow" "$limit")"
if test -n "$today_lines"; then
echo "$TODAY_MSG"
echo "$DIVIDER"
echo "$today_lines"
if test -n "$later_lines"; then echo ''; fi
fi
if test -n "$later_lines"; then
echo "$LATER_MSG"
echo "$DIVIDER"
echo "$later_lines"
fi
}
# --------------------------------------
# INVOCATION
BIN="$(echo "$0" | sed 's%.*/%%')"
function print_help {
echo "usage: $BIN [-hq] [-l \$LIMIT] [-L | -D \$MSG] [\$TODO_PATH]"
exit 2
}
# ------------------
# OPTIONS
DIVIDER="----------------------------------------"
TODO_FILE="$HOME/.todo"
LIMIT=7
TODAY_MSG="TODAY"
LATER_MSG="NEXT EPISODE..."
PAST_MSG="FROM THE GRAVE"
QUIET_MODE=1
while getopts 'l:D:T:L:qh' c; do
case "$c" in
l) LIMIT="$OPTARG" ;;
D) DIVIDER="$OPTARG" ;;
T) TODAY_MSG="$OPTARG" ;;
L) LATER_MSG="$OPTARG" ;;
q) QUIET_MODE=0 ;;
h) print_help ;;
esac
done
shift "$(dec "$OPTIND" 1)"
FREE_ARG="$1"
if test -n "$FREE_ARG"; then
TODO_FILE="$FREE_ARG"
fi
# ------------------
# PROGRAM TYME
if test ! -e "$TODO_FILE"; then
echo "$TODO_FILE: No such file exists"
exit 3
fi
if test $QUIET_MODE -eq 1; then
generate_report "$TODO_FILE" "$LIMIT"
else
upcoming_todo_lines "$TODO_FILE" "$(today)" "$LIMIT"
fi