Browse code

Replace date_distance (ugh) system with simple greps/increment

Jaidyn Levesque authored on 2020-01-26 05:56:38
Showing 1 changed files
... ...
@@ -7,16 +7,13 @@
7 7
 
8 8
 # Take in everything in stdin, then print
9 9
 # (Useful for piping something into a variable)
10
-#
11
-# local distance=0
12 10
 function reade {
13 11
 	local stack=""
14 12
 	while read input; do
15
-    		stack="$(printf '%s\n%s' "$stack" "$input")"
13
+		stack="$(printf '%s\n%s' "$stack" "$input")"
16 14
 	done
17
-
18 15
 	echo "$stack"
19
-	}
16
+}
20 17
 
21 18
 
22 19
 
... ...
@@ -25,45 +22,36 @@ function reade {
25 22
 
26 23
 # Add together two numbers
27 24
 function add {
28
-    local a="$1"; local b="$2"
29
-    echo "$a + $b" \
30
-    | bc
25
+	local a="$1"; local b="$2"
26
+	echo "$a + $b" \
27
+	| bc
31 28
 }
32 29
 
33 30
 # Subtract two numbers
34 31
 function subtract {
35
-    local a="$1"; local b="$2"
36
-    echo "$a - $b" \
37
-    | bc 
32
+	local a="$1"; local b="$2"
33
+	echo "$a - $b" \
34
+	| bc 
38 35
 }
39 36
 
40 37
 # Multiply two numbers
41 38
 function multiply {
42
-    local a="$1"; local b="$2"
43
-    echo "$a * $b" \
44
-    | bc 
39
+	local a="$1"; local b="$2"
40
+	echo "$a * $b" \
41
+	| bc 
45 42
 }
46 43
 
47 44
 
48 45
 # Increment a number by one
49 46
 function inc {
50
-    local a="$1"
51
-    add 1 "$a"
47
+	local a="$1"
48
+	add 1 "$a"
52 49
 }
53 50
 
54 51
 # Decrement a number by one
55 52
 function dec {
56
-    local a="$1"
57
-    subtract "$a" 1
58
-}
59
-
60
-
61
-# Return whether or not a number is negative
62
-function is_negative {
63
-    local a="$!"
64
-    echo "$a" \
65
-    | grep "^-" \
66
-    > /dev/null
53
+	local a="$1"
54
+	subtract "$a" 1
67 55
 }
68 56
 
69 57
 
... ...
@@ -75,13 +63,14 @@ function digits {
75 63
 
76 64
 	if test "$(dec "$(echo "$number" | wc -c )")" -lt "$(inc "$digits")"
77 65
 	then
78
-        	local i=1;
79
-        	while test $i -lt "$digits"; do
80
-            		printf "0";
81
-            		i="$(inc "$i")"
82
-	        done
83
-	printf %s $number
84
-		else echo "$number";
66
+		local i=1;
67
+		while test $i -lt "$digits"; do
68
+			printf "0";
69
+			i="$(inc "$i")"
70
+		done
71
+		printf %s $number
72
+	else
73
+		printf %s $number;
85 74
 	fi
86 75
 }
87 76
 
... ...
@@ -92,89 +81,89 @@ function digits {
92 81
 
93 82
 # Return today's date, YYYY-MM-DD
94 83
 function today {
95
-    date +"%Y-%m-%d"
84
+	date +"%Y-%m-%d"
96 85
 }
97 86
 
98 87
 # Return current time, HH:MM
99 88
 function now {
100
-    date +"%H:%M"
89
+	date +"%H:%M"
101 90
 }
102 91
 
103 92
 
104 93
 # Return the day of a given date
105 94
 function date_day {
106
-    local date="$1"
107
-    echo "$date" \
108
-    | awk -F "-" '{print $3}'
95
+	local date="$1"
96
+	echo "$date" \
97
+	| awk -F "-" '{print $3}'
109 98
 }
110 99
 
111 100
 # Return the month of a given date
112 101
 function date_month {
113
-    local date="$1"
114
-    echo "$date" \
115
-    | awk -F "-" '{print $2}'
102
+	local date="$1"
103
+	echo "$date" \
104
+	| awk -F "-" '{print $2}'
116 105
 }
117 106
 
118 107
  # Return the year of a given date
119 108
 function date_year {
120
-    local date="$1"
121
-    echo "$date" \
122
-    | awk -F "-" '{print $1}'
109
+	local date="$1"
110
+	echo "$date" \
111
+	| awk -F "-" '{print $1}'
123 112
 }
124 113
 
125 114
 
126 115
 # Return the hour of a given time
127 116
 function time_hour {
128
-    local time="$1"
129
-    echo "$time" \
130
-    | awk -F ":" '{print $1}'
117
+	local time="$1"
118
+	echo "$time" \
119
+	| awk -F ":" '{print $1}'
131 120
 }
132 121
 
133 122
 # Return the minute of a given time
134 123
 function time_minute {
135
-    local time="$1"
136
-    echo "$time" \
137
-    | awk -F ":" '{print $2}'
124
+	local time="$1"
125
+	echo "$time" \
126
+	| awk -F ":" '{print $2}'
138 127
 }
139 128
 
140 129
 
141 130
 # Return current year
142 131
 function this_year {
143
-    date_year "$(today)"
132
+	date_year "$(today)"
144 133
 }
145 134
 
146 135
 # Return current month
147 136
 function this_month {
148
-    date_month "$(today)"
137
+	date_month "$(today)"
149 138
 }
150 139
 
151 140
 # Return current day
152 141
 function this_day {
153
-    date_day "$(today)"
142
+	date_day "$(today)"
154 143
 }
155 144
 
156 145
 
157 146
 # Return current hour
158 147
 function this_hour {
159
-    time_hour "$(now)"
148
+	time_hour "$(now)"
160 149
 }
161 150
 
162 151
 # Return current minute
163 152
 function this_minute  {
164
-    time_minute "$(now)"
153
+	time_minute "$(now)"
165 154
 }
166 155
 
167 156
 
168 157
 # Return how many days ought to be in the given month
169 158
 function month_days {
170
-    local month="$1"
171
-    case "$month" in
172
-    	09) echo 30;;
173
-    	04) echo 30;;
174
-    	06) echo 30;;
175
-    	11) echo 30;;
176
-    	*)  echo 31;;
177
-    esac
159
+	local month="$1"
160
+	case "$month" in
161
+		09) echo 30;;
162
+		04) echo 30;;
163
+		06) echo 30;;
164
+		11) echo 30;;
165
+		*)  echo 31;;
166
+	esac
178 167
 }
179 168
 
180 169
 
... ...
@@ -184,32 +173,16 @@ function month_days {
184 173
 
185 174
 # Add an amount of days to a given date
186 175
 function add_days {
187
-    local date="$1"
188
-    local days_added="$2"
189
-    local date_day="$(date_day "$date")"
176
+	local date="$1"
177
+	local days_added="$2"
178
+	local day="$(date_day "$date")"; local month="$(date_month "$date")"
179
+	local year="$(date_year "$date")"
190 180
 
191
-    carry_date "$date" "$(add "$date_day" "$days_added")"
192
-}
181
+	local new_day="$(add "$day" "$days_added" | digits 2)"
193 182
 
194
-# Return the amount of days between two dates
195
-function date_distance {
196
-    local date_a="$1"; local date_b="$2"
197
-
198
-    local year_a="$(date_year "$date_a")"; local year_b="$(date_year "$date_b")"
199
-    local month_a="$(date_month "$date_a")"
200
-    local month_b="$(date_month "$date_b")"
201
-    local day_a="$(date_day "$date_a")"; local day_b="$(date_day "$date_b")"
202
-
203
-    if test "$month_a" -eq "$month_b" -a "$year_a" -eq "$year_a"; then
204
-     	same_year_month_distance "$month_a" "$day_a" "$day_b"
205
-    elif test "$year_a" -eq "$year_b"; then
206
-        same_year_distance "$month_a" "$day_a" "$month_b" "$day_b"
207
-    else different_year_distance "$year_a" "$month_a" "$day_a" \
208
-                                 "$year_b" "$month_b" "$day_b"
209
-    fi
183
+	carry_date "${year}-${month}-$new_day"
210 184
 }
211 185
 
212
-
213 186
 # Return whether or not a given date is valid
214 187
 # (I.E., not too month months or days)
215 188
 function is_balanced_date {
... ...
@@ -219,92 +192,53 @@ function is_balanced_date {
219 192
 	local month_max="$(month_days "$month")"
220 193
 
221 194
 	if test "$month" -gt 12; then
222
-		echo "1"
195
+		return 1
223 196
 	elif test "$day" -gt "$month_max" ; then
224
-    		echo "1";
225
-    	else echo "0"
226
-        fi
197
+    		return 1
198
+	else
199
+		return 0
200
+	fi
227 201
 }
228 202
 
229 203
 # Correct an unbalanced date
230 204
 function carry_date {
231
-    local date="$1"
205
+	local date="$1"
232 206
 
233 207
 	while test "$(is_balanced_date "$date")" -eq 1; do
234
-    		date="$(carry_months "$(carry_days "$date")")"
208
+		date="$(carry_months "$(carry_days "$date")")"
235 209
 	done
236 210
 	echo "$date"
237 211
 }
238 212
 
239 213
 # If too many days in a given month, carry them over
240 214
 function carry_days {
241
-    local date="$1"
242
-    local year="$(date_year "$date")"
243
-    local month="$(date_month "$date")"
244
-    local days="$(date_day "$date")"
245
-
246
-    if test "$days" -gt "$(month_days "$month")"; then
247
-        local new_days="$(subtract "$days" "$(month_days "$month")" | digits 2)"
248
-        local new_month="$(add "$month" "1")"
249
-        echo "${year}-${new_month}-${new_days}"
250
-    else
251
-        echo "$date"
252
-    fi
215
+	local date="$1"
216
+	local year="$(date_year "$date")"
217
+	local month="$(date_month "$date")"
218
+	local days="$(date_day "$date")"
219
+
220
+	if test "$days" -gt "$(month_days "$month")"; then
221
+		local new_days="$(subtract "$days"
222
+		                           "$(month_days "$month")" | digits 2)"
223
+		local new_month="$(add "$month" "1")"
224
+		echo "${year}-${new_month}-${new_days}"
225
+	else
226
+		echo "$date"
227
+	fi
253 228
 }
254 229
 
255 230
 # If too many months in a year, carry them over
256 231
 function carry_months {
257
-    local date="$1"
258
-    local year="$(date_year "$date")"
259
-    local day="$(date_day "$date")"
260
-    local month="$(date_month "$date")"
261
-    if test "$month" -gt 12; then
262
-        month="$(subtract "$month" 12 | digits 2)"
263
-        year="$(inc "$year")"
264
-fi
265
-    echo "${year}-${month}-${day}"
266
-}
267
-
268
-
269
-# Return the distance between two dates of different years
270
-# (Helper function to date_distance)
271
-function different_year_distance {
272
-    local year_a="$1"; local month_a="$2"; local day_a="$3"
273
-    local year_b="$4"; local month_b="$5"; local day_b="$6"
274
-
275
-    add "$(multiply 365 $(subtract $year_b $year_a))" \
276
-   	 "$(same_year_distance "$month_a" "$day_a" "$month_b" "$day_b")"
277
-}
278
-
279
-# Return the distance between two dates of the same year and month
280
-# (Helper function to date_distance)
281
-function same_year_month_distance {
282
-    local month="$1"
283
-    local day_a="$2"; local day_b="$3"
284
-    subtract "$day_b" "$day_a"
285
-}
286
-
287
-# Return the distance between two dates of the same year
288
-# (Helper function to date_distance)
289
-function same_year_distance {
290
-    local month_a="$1"; local day_a="$2"
291
-    local month_b="$3"; local day_b="$4"
292
-
293
-    local month_days_a="$(month_days "$month_a")"
294
-    local distance=0
295
-
296
-    if test "$month_a" -gt "$month_b"; then
297
-        echo "-$(same_year_distance "$month_b" "$day_b" "$month_a" "$day_a")"
298
-    else
299
-	distance="$(subtract "$month_days_a" "$day_a")"
300
-	month_a="$(inc "$month_a")"
232
+	local date="$1"
233
+	local year="$(date_year "$date")"
234
+	local day="$(date_day "$date")"
235
+	local month="$(date_month "$date")"
301 236
 
302
-	while test "$month_a" -lt "$month_b"; do
303
-    		month_a="$(inc "$month_a")"
304
-    		distance="$(add "$distance" "$(month_days "$month_a")")"
305
-	done
306
-	add "$distance" "$day_b"
237
+	if test "$month" -gt 12; then
238
+		month="$(subtract "$month" 12 | digits 2)"
239
+		year="$(inc "$year")"
307 240
 	fi
241
+	echo "${year}-${month}-${day}"
308 242
 }
309 243
 
310 244
 
... ...
@@ -312,80 +246,74 @@ function same_year_distance {
312 246
 # --------------------------------------
313 247
 # TODO HANDLING
314 248
 
315
-# Replace piped todo's vague dates with current dates
316
-function demystify_todo_times {
317
-    sed 's%^\*-%'"$(this_year)"'-%g' \
318
-    | sed 's%-\*-%-'"$(this_month)"'-%g' \
319
-    | sed 's%-\*%-'"$(this_day)"'%g' \
320
-    | sed 's%\*:%'"$(this_hour)"':%g' \
321
-    | sed 's%:\*%:'"$(this_minute)"'%g'
322
-}
323
-
324
-# Filter out comments and blank lines from piped todo
325
-function ignore_todo_blanks {
326
-    grep -v "^#" \
327
-    | grep -v "^$"
328
-}
329
-
249
+# Clean up a todo file for use with the program
330 250
 function preprocess_todo {
331
-    ignore_todo_blanks \
332
-    | demystify_todo_times
251
+	ignore_todo_blanks \
252
+	| demystify_todo_times
333 253
 }
334 254
 
335 255
 
336
-function todo_line_date {
337
-    awk '{print $1}'
338
-}
339
-
340
-function todo_line_time {
341
-    awk '{print $2}'
256
+# Replace piped todo's vague dates with current dates
257
+function demystify_todo_times {
258
+	sed 's%^\*-%'"$(this_year)"'-%g' \
259
+	| sed 's%-\*-%-'"$(this_month)"'-%g' \
260
+	| sed 's%-\*%-'"$(this_day)"'%g' \
261
+	| sed 's%\*:%'"$(this_hour)"':%g' \
262
+	| sed 's%:\*%:'"$(this_minute)"'%g'
342 263
 }
343 264
 
344
-function todo_line_desc {
345
-    awk '{print $2}'
265
+# Filter out comments and blank lines from piped todo
266
+function ignore_todo_blanks {
267
+	grep -v "^#" \
268
+	| grep -v "^$"
346 269
 }
347 270
 
348
-function todo_line_distance {
349
-    local line_date="$(todo_line_date)"
350
-    date_distance "$(today)" "$line_date"
351
-}
352 271
 
353
-function todo_line_is_trash {
354
-    if is_negative "$(todo_line_distance)"; then
355
-        return 0
356
-	else
357
-    	return 1
358
-    	fi
272
+# Return all todo lines of the given date
273
+function date_todo_lines {
274
+	local date="$1"
275
+	grep "$date"
359 276
 }
360 277
 
278
+# Print all todo lines during the giving days following the start-date
361 279
 function upcoming_todo_lines {
362
-    local limit="$1"
363
-    preprocess_todo \
364
-    | while IFS= read -r line; do
365
-        if test "$limit" -gt "$(echo "$line" | todo_line_distance)"; then
366
-        echo "$line"
367
-        fi
368
-      done
280
+	local todo_file="$1"
281
+	local start_date="$2"
282
+	local limit="$3"
283
+
284
+	if test "$limit" -eq 0; then limit="$(inc "$limit")"; fi
285
+
286
+	local i="0"
287
+	while test "$i" -lt "$limit"; do
288
+		cat "$todo_file" \
289
+		| preprocess_todo \
290
+		| date_todo_lines "$(add_days "$start_date" "$i")"
291
+		i="$(inc "$i")"
292
+	done
369 293
 }
370 294
 
371
-function today_todo_lines {
372
-    grep "$(today)"
373
-}
374 295
 
296
+# Print a user-friendly report of upcoming events <3
375 297
 function generate_report {
376
-    local todo_file="$1"
377
-    local limit="$2"
378
-    local today_lines="$(cat "$todo_file" | preprocess_todo | today_todo_lines)"
379
-
380
-    echo "$TODAY_MSG"
381
-    echo "$DIVIDER"
382
-    echo "$today_lines"
383
-    echo
384
-    echo "$LATER_MSG"
385
-    echo "$DIVIDER"
386
-    grep -v "$(today)" $todo_file \
387
-    | upcoming_todo_lines $limit \
388
-    | sort -n
298
+	local todo_file="$1"
299
+	local limit="$2"
300
+
301
+	local tomorrow="$(add_days "$(today)" 1)"
302
+	local today_lines="$(upcoming_todo_lines "$todo_file" "$(today)" 0)"
303
+	local later_lines="$(upcoming_todo_lines "$todo_file" "$tomorrow" "$limit")"
304
+
305
+	if test -n "$today_lines"; then
306
+		echo "$TODAY_MSG"
307
+		echo "$DIVIDER"
308
+		echo "$today_lines"
309
+		if test -n "$later_lines"; then echo ''; fi
310
+	fi
311
+
312
+	if test -n "$later_lines"; then
313
+		echo "$LATER_MSG"
314
+		echo "$DIVIDER"
315
+		echo "$later_lines"
316
+	fi
389 317
 }
390 318
 
391 319
 
... ...
@@ -393,9 +321,12 @@ function generate_report {
393 321
 # --------------------------------------
394 322
 # INVOCATION
395 323
 
324
+# ------------------
325
+# OPTIONS
326
+
327
+DIVIDER="----------------------------------------"
396 328
 TODO_FILE="$HOME/.todo"
397 329
 LIMIT=7
398
-DIVIDER="----------------------------------------"
399 330
 TODAY_MSG="TODAY"
400 331
 LATER_MSG="NEXT EPISODE..."
401 332
 PAST_MSG="FROM THE GRAVE"
... ...
@@ -403,21 +334,33 @@ QUIET_MODE=1
403 334
 
404 335
 while getopts 'l:D:T:L:P:qh' c; do
405 336
 	case "$c" in
406
-    		l) LIMIT="$OPTARG" ;;
407
-    		D) DIVIDER="$OPTARG" ;;
408
-    		T) TODAY_MSG="$OPTARG" ;;
409
-    		L) LATER_MSG="$OPTARG" ;;
410
-    		P) PAST_MSG="$OPTARG" ;;
411
-    		q) QUIET_MODE=0 ;;
412
-    		h) echo "$HELP"; exit 2 ;;
337
+			l) LIMIT="$OPTARG" ;;
338
+			D) DIVIDER="$OPTARG" ;;
339
+			T) TODAY_MSG="$OPTARG" ;;
340
+			L) LATER_MSG="$OPTARG" ;;
341
+			P) PAST_MSG="$OPTARG" ;;
342
+			q) QUIET_MODE=0 ;;
343
+			h) echo "$HELP"; exit 2 ;;
413 344
 	esac
414 345
 done
346
+shift "$(dec "$OPTIND" 1)"
347
+
348
+FREE_ARG="$1"
349
+if test -n "$FREE_ARG"; then
350
+	TODO_FILE="$FREE_ARG"
351
+fi
352
+
353
+
354
+# ------------------
355
+# PROGRAM TYME
356
+
357
+if test ! -e "$TODO_FILE"; then
358
+	echo "$TODO_FILE: No such file exists"
359
+	exit 3
360
+fi
415 361
 
416 362
 if test $QUIET_MODE -eq 1; then
417 363
 	generate_report "$TODO_FILE" "$LIMIT"
418 364
 else
419
-	cat "$TODO_FILE" \
420
-	| preprocess_todo \
421
-	| upcoming_todo_lines "$LIMIT" \
422
-	| sort -n
365
+	upcoming_todo_lines "$TODO_FILE" "$(today)" "$LIMIT"
423 366
 fi