Replace date_distance (ugh) system with simple greps/increment
This commit is contained in:
parent
0b34e4004d
commit
7b58330d53
199
farend.sh
199
farend.sh
|
@ -7,14 +7,11 @@
|
|||
|
||||
# Take in everything in stdin, then print
|
||||
# (Useful for piping something into a variable)
|
||||
#
|
||||
# local distance=0
|
||||
function reade {
|
||||
local stack=""
|
||||
while read input; do
|
||||
stack="$(printf '%s\n%s' "$stack" "$input")"
|
||||
done
|
||||
|
||||
echo "$stack"
|
||||
}
|
||||
|
||||
|
@ -58,15 +55,6 @@ function dec {
|
|||
}
|
||||
|
||||
|
||||
# Return whether or not a number is negative
|
||||
function is_negative {
|
||||
local a="$!"
|
||||
echo "$a" \
|
||||
| grep "^-" \
|
||||
> /dev/null
|
||||
}
|
||||
|
||||
|
||||
# Format a number to be a certain amount of digits long
|
||||
# (done by prepending zeroes)
|
||||
function digits {
|
||||
|
@ -81,7 +69,8 @@ function digits {
|
|||
i="$(inc "$i")"
|
||||
done
|
||||
printf %s $number
|
||||
else echo "$number";
|
||||
else
|
||||
printf %s $number;
|
||||
fi
|
||||
}
|
||||
|
||||
|
@ -186,30 +175,14 @@ function month_days {
|
|||
function add_days {
|
||||
local date="$1"
|
||||
local days_added="$2"
|
||||
local date_day="$(date_day "$date")"
|
||||
local day="$(date_day "$date")"; local month="$(date_month "$date")"
|
||||
local year="$(date_year "$date")"
|
||||
|
||||
carry_date "$date" "$(add "$date_day" "$days_added")"
|
||||
local new_day="$(add "$day" "$days_added" | digits 2)"
|
||||
|
||||
carry_date "${year}-${month}-$new_day"
|
||||
}
|
||||
|
||||
# Return the amount of days between two dates
|
||||
function date_distance {
|
||||
local date_a="$1"; local date_b="$2"
|
||||
|
||||
local year_a="$(date_year "$date_a")"; local year_b="$(date_year "$date_b")"
|
||||
local month_a="$(date_month "$date_a")"
|
||||
local month_b="$(date_month "$date_b")"
|
||||
local day_a="$(date_day "$date_a")"; local day_b="$(date_day "$date_b")"
|
||||
|
||||
if test "$month_a" -eq "$month_b" -a "$year_a" -eq "$year_a"; then
|
||||
same_year_month_distance "$month_a" "$day_a" "$day_b"
|
||||
elif test "$year_a" -eq "$year_b"; then
|
||||
same_year_distance "$month_a" "$day_a" "$month_b" "$day_b"
|
||||
else different_year_distance "$year_a" "$month_a" "$day_a" \
|
||||
"$year_b" "$month_b" "$day_b"
|
||||
fi
|
||||
}
|
||||
|
||||
|
||||
# Return whether or not a given date is valid
|
||||
# (I.E., not too month months or days)
|
||||
function is_balanced_date {
|
||||
|
@ -219,10 +192,11 @@ function is_balanced_date {
|
|||
local month_max="$(month_days "$month")"
|
||||
|
||||
if test "$month" -gt 12; then
|
||||
echo "1"
|
||||
return 1
|
||||
elif test "$day" -gt "$month_max" ; then
|
||||
echo "1";
|
||||
else echo "0"
|
||||
return 1
|
||||
else
|
||||
return 0
|
||||
fi
|
||||
}
|
||||
|
||||
|
@ -244,7 +218,8 @@ function carry_days {
|
|||
local days="$(date_day "$date")"
|
||||
|
||||
if test "$days" -gt "$(month_days "$month")"; then
|
||||
local new_days="$(subtract "$days" "$(month_days "$month")" | digits 2)"
|
||||
local new_days="$(subtract "$days"
|
||||
"$(month_days "$month")" | digits 2)"
|
||||
local new_month="$(add "$month" "1")"
|
||||
echo "${year}-${new_month}-${new_days}"
|
||||
else
|
||||
|
@ -258,6 +233,7 @@ function carry_months {
|
|||
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")"
|
||||
|
@ -266,52 +242,17 @@ fi
|
|||
}
|
||||
|
||||
|
||||
# Return the distance between two dates of different years
|
||||
# (Helper function to date_distance)
|
||||
function different_year_distance {
|
||||
local year_a="$1"; local month_a="$2"; local day_a="$3"
|
||||
local year_b="$4"; local month_b="$5"; local day_b="$6"
|
||||
|
||||
add "$(multiply 365 $(subtract $year_b $year_a))" \
|
||||
"$(same_year_distance "$month_a" "$day_a" "$month_b" "$day_b")"
|
||||
}
|
||||
|
||||
# Return the distance between two dates of the same year and month
|
||||
# (Helper function to date_distance)
|
||||
function same_year_month_distance {
|
||||
local month="$1"
|
||||
local day_a="$2"; local day_b="$3"
|
||||
subtract "$day_b" "$day_a"
|
||||
}
|
||||
|
||||
# Return the distance between two dates of the same year
|
||||
# (Helper function to date_distance)
|
||||
function same_year_distance {
|
||||
local month_a="$1"; local day_a="$2"
|
||||
local month_b="$3"; local day_b="$4"
|
||||
|
||||
local month_days_a="$(month_days "$month_a")"
|
||||
local distance=0
|
||||
|
||||
if test "$month_a" -gt "$month_b"; then
|
||||
echo "-$(same_year_distance "$month_b" "$day_b" "$month_a" "$day_a")"
|
||||
else
|
||||
distance="$(subtract "$month_days_a" "$day_a")"
|
||||
month_a="$(inc "$month_a")"
|
||||
|
||||
while test "$month_a" -lt "$month_b"; do
|
||||
month_a="$(inc "$month_a")"
|
||||
distance="$(add "$distance" "$(month_days "$month_a")")"
|
||||
done
|
||||
add "$distance" "$day_b"
|
||||
fi
|
||||
}
|
||||
|
||||
|
||||
|
||||
# --------------------------------------
|
||||
# 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 {
|
||||
sed 's%^\*-%'"$(this_year)"'-%g' \
|
||||
|
@ -327,65 +268,52 @@ function ignore_todo_blanks {
|
|||
| grep -v "^$"
|
||||
}
|
||||
|
||||
function preprocess_todo {
|
||||
ignore_todo_blanks \
|
||||
| demystify_todo_times
|
||||
}
|
||||
|
||||
|
||||
function todo_line_date {
|
||||
awk '{print $1}'
|
||||
}
|
||||
|
||||
function todo_line_time {
|
||||
awk '{print $2}'
|
||||
}
|
||||
|
||||
function todo_line_desc {
|
||||
awk '{print $2}'
|
||||
}
|
||||
|
||||
function todo_line_distance {
|
||||
local line_date="$(todo_line_date)"
|
||||
date_distance "$(today)" "$line_date"
|
||||
}
|
||||
|
||||
function todo_line_is_trash {
|
||||
if is_negative "$(todo_line_distance)"; then
|
||||
return 0
|
||||
else
|
||||
return 1
|
||||
fi
|
||||
|
||||
# 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 limit="$1"
|
||||
preprocess_todo \
|
||||
| while IFS= read -r line; do
|
||||
if test "$limit" -gt "$(echo "$line" | todo_line_distance)"; then
|
||||
echo "$line"
|
||||
fi
|
||||
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")"
|
||||
i="$(inc "$i")"
|
||||
done
|
||||
}
|
||||
|
||||
function today_todo_lines {
|
||||
grep "$(today)"
|
||||
}
|
||||
|
||||
# Print a user-friendly report of upcoming events <3
|
||||
function generate_report {
|
||||
local todo_file="$1"
|
||||
local limit="$2"
|
||||
local today_lines="$(cat "$todo_file" | preprocess_todo | today_todo_lines)"
|
||||
|
||||
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"
|
||||
echo
|
||||
if test -n "$later_lines"; then echo ''; fi
|
||||
fi
|
||||
|
||||
if test -n "$later_lines"; then
|
||||
echo "$LATER_MSG"
|
||||
echo "$DIVIDER"
|
||||
grep -v "$(today)" $todo_file \
|
||||
| upcoming_todo_lines $limit \
|
||||
| sort -n
|
||||
echo "$later_lines"
|
||||
fi
|
||||
}
|
||||
|
||||
|
||||
|
@ -393,9 +321,12 @@ function generate_report {
|
|||
# --------------------------------------
|
||||
# INVOCATION
|
||||
|
||||
# ------------------
|
||||
# OPTIONS
|
||||
|
||||
DIVIDER="----------------------------------------"
|
||||
TODO_FILE="$HOME/.todo"
|
||||
LIMIT=7
|
||||
DIVIDER="----------------------------------------"
|
||||
TODAY_MSG="TODAY"
|
||||
LATER_MSG="NEXT EPISODE..."
|
||||
PAST_MSG="FROM THE GRAVE"
|
||||
|
@ -412,12 +343,24 @@ while getopts 'l:D:T:L:P:qh' c; do
|
|||
h) echo "$HELP"; exit 2 ;;
|
||||
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
|
||||
cat "$TODO_FILE" \
|
||||
| preprocess_todo \
|
||||
| upcoming_todo_lines "$LIMIT" \
|
||||
| sort -n
|
||||
upcoming_todo_lines "$TODO_FILE" "$(today)" "$LIMIT"
|
||||
fi
|
||||
|
|
Ŝarĝante…
Reference in New Issue