2020-01-25 22:51:21 -06:00
|
|
|
#!/bin/sh
|
|
|
|
# --------------------------------------
|
|
|
|
# name: farend
|
|
|
|
# desc: a simple todo system in shell
|
2024-01-29 00:35:36 -06:00
|
|
|
# main: jaidyn ann <jadedctrl@posteo.at>
|
2020-01-25 22:51:21 -06:00
|
|
|
# --------------------------------------
|
|
|
|
|
|
|
|
# Take in everything in stdin, then print
|
|
|
|
# (Useful for piping something into a variable)
|
|
|
|
function reade {
|
|
|
|
local stack=""
|
|
|
|
while read input; do
|
2020-01-25 23:56:38 -06:00
|
|
|
stack="$(printf '%s\n%s' "$stack" "$input")"
|
2020-01-25 22:51:21 -06:00
|
|
|
done
|
|
|
|
echo "$stack"
|
2020-01-25 23:56:38 -06:00
|
|
|
}
|
2020-01-25 22:51:21 -06:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# --------------------------------------
|
|
|
|
# BASIC MATH
|
|
|
|
|
|
|
|
# Add together two numbers
|
|
|
|
function add {
|
2020-01-25 23:56:38 -06:00
|
|
|
local a="$1"; local b="$2"
|
|
|
|
echo "$a + $b" \
|
|
|
|
| bc
|
2020-01-25 22:51:21 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
# Subtract two numbers
|
|
|
|
function subtract {
|
2020-01-25 23:56:38 -06:00
|
|
|
local a="$1"; local b="$2"
|
|
|
|
echo "$a - $b" \
|
|
|
|
| bc
|
2020-01-25 22:51:21 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
# Increment a number by one
|
|
|
|
function inc {
|
2020-01-25 23:56:38 -06:00
|
|
|
local a="$1"
|
|
|
|
add 1 "$a"
|
2020-01-25 22:51:21 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
# Decrement a number by one
|
|
|
|
function dec {
|
2020-01-25 23:56:38 -06:00
|
|
|
local a="$1"
|
|
|
|
subtract "$a" 1
|
2020-01-25 22:51:21 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
# 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
|
2020-01-25 23:56:38 -06:00
|
|
|
local i=1;
|
|
|
|
while test $i -lt "$digits"; do
|
|
|
|
printf "0";
|
|
|
|
i="$(inc "$i")"
|
|
|
|
done
|
|
|
|
printf %s $number
|
|
|
|
else
|
|
|
|
printf %s $number;
|
2020-01-25 22:51:21 -06:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# --------------------------------------
|
|
|
|
# DATE PRIMITIVES
|
|
|
|
|
|
|
|
# Return today's date, YYYY-MM-DD
|
|
|
|
function today {
|
2020-01-25 23:56:38 -06:00
|
|
|
date +"%Y-%m-%d"
|
2020-01-25 22:51:21 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
# Return the day of a given date
|
|
|
|
function date_day {
|
2020-01-25 23:56:38 -06:00
|
|
|
local date="$1"
|
|
|
|
echo "$date" \
|
|
|
|
| awk -F "-" '{print $3}'
|
2020-01-25 22:51:21 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
# Return the month of a given date
|
|
|
|
function date_month {
|
2020-01-25 23:56:38 -06:00
|
|
|
local date="$1"
|
|
|
|
echo "$date" \
|
|
|
|
| awk -F "-" '{print $2}'
|
2020-01-25 22:51:21 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
# Return the year of a given date
|
|
|
|
function date_year {
|
2020-01-25 23:56:38 -06:00
|
|
|
local date="$1"
|
|
|
|
echo "$date" \
|
|
|
|
| awk -F "-" '{print $1}'
|
2020-01-25 22:51:21 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
# Return how many days ought to be in the given month
|
|
|
|
function month_days {
|
2020-01-25 23:56:38 -06:00
|
|
|
local month="$1"
|
|
|
|
case "$month" in
|
|
|
|
09) echo 30;;
|
|
|
|
04) echo 30;;
|
|
|
|
06) echo 30;;
|
|
|
|
11) echo 30;;
|
|
|
|
*) echo 31;;
|
|
|
|
esac
|
2020-01-25 22:51:21 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# --------------------------------------
|
|
|
|
# DATE ARITHMETIC
|
|
|
|
|
|
|
|
# Add an amount of days to a given date
|
|
|
|
function add_days {
|
2020-01-25 23:56:38 -06:00
|
|
|
local date="$1"
|
|
|
|
local days_added="$2"
|
|
|
|
local day="$(date_day "$date")"; local month="$(date_month "$date")"
|
|
|
|
local year="$(date_year "$date")"
|
2020-01-25 22:51:21 -06:00
|
|
|
|
2020-01-25 23:56:38 -06:00
|
|
|
local new_day="$(add "$day" "$days_added" | digits 2)"
|
2020-01-25 22:51:21 -06:00
|
|
|
|
2020-01-25 23:56:38 -06:00
|
|
|
carry_date "${year}-${month}-$new_day"
|
2020-01-25 22:51:21 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
# 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
|
2020-01-25 23:56:38 -06:00
|
|
|
return 1
|
2020-01-25 22:51:21 -06:00
|
|
|
elif test "$day" -gt "$month_max" ; then
|
2020-01-26 00:35:33 -06:00
|
|
|
return 1
|
2020-01-25 23:56:38 -06:00
|
|
|
else
|
|
|
|
return 0
|
|
|
|
fi
|
2020-01-25 22:51:21 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
# Correct an unbalanced date
|
|
|
|
function carry_date {
|
2020-01-25 23:56:38 -06:00
|
|
|
local date="$1"
|
2020-01-25 22:51:21 -06:00
|
|
|
|
2020-01-26 00:35:33 -06:00
|
|
|
is_balanced_date "$date"
|
|
|
|
while test "$?" -eq 1; do
|
2020-01-25 23:56:38 -06:00
|
|
|
date="$(carry_months "$(carry_days "$date")")"
|
2020-01-25 22:51:21 -06:00
|
|
|
done
|
|
|
|
echo "$date"
|
|
|
|
}
|
|
|
|
|
|
|
|
# If too many days in a given month, carry them over
|
|
|
|
function carry_days {
|
2020-01-25 23:56:38 -06:00
|
|
|
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
|
2020-01-26 00:35:33 -06:00
|
|
|
local new_days="$(subtract "$days" "$(month_days "$month")")"
|
2020-01-25 23:56:38 -06:00
|
|
|
local new_month="$(add "$month" "1")"
|
2020-01-26 00:35:33 -06:00
|
|
|
new_days="$(echo "$new_days" | digits 2)"
|
|
|
|
new_month="$(echo "$new_month" | digits 2)"
|
2020-01-25 23:56:38 -06:00
|
|
|
echo "${year}-${new_month}-${new_days}"
|
|
|
|
else
|
|
|
|
echo "$date"
|
|
|
|
fi
|
2020-01-25 22:51:21 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
# If too many months in a year, carry them over
|
|
|
|
function carry_months {
|
2020-01-25 23:56:38 -06:00
|
|
|
local date="$1"
|
|
|
|
local year="$(date_year "$date")"
|
|
|
|
local day="$(date_day "$date")"
|
|
|
|
local month="$(date_month "$date")"
|
2020-01-25 22:51:21 -06:00
|
|
|
|
2020-01-25 23:56:38 -06:00
|
|
|
if test "$month" -gt 12; then
|
|
|
|
month="$(subtract "$month" 12 | digits 2)"
|
|
|
|
year="$(inc "$year")"
|
2020-01-25 22:51:21 -06:00
|
|
|
fi
|
2020-01-25 23:56:38 -06:00
|
|
|
echo "${year}-${month}-${day}"
|
2020-01-25 22:51:21 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# --------------------------------------
|
|
|
|
# TODO HANDLING
|
|
|
|
|
2020-01-25 23:56:38 -06:00
|
|
|
# Clean up a todo file for use with the program
|
2020-01-25 22:51:21 -06:00
|
|
|
function preprocess_todo {
|
2020-01-25 23:56:38 -06:00
|
|
|
ignore_todo_blanks \
|
|
|
|
| demystify_todo_times
|
2020-01-25 22:51:21 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-01-25 23:56:38 -06:00
|
|
|
# Replace piped todo's vague dates with current dates
|
|
|
|
function demystify_todo_times {
|
2020-01-26 01:36:03 -06:00
|
|
|
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'
|
2020-01-25 22:51:21 -06:00
|
|
|
}
|
|
|
|
|
2020-01-25 23:56:38 -06:00
|
|
|
# Filter out comments and blank lines from piped todo
|
|
|
|
function ignore_todo_blanks {
|
|
|
|
grep -v "^#" \
|
|
|
|
| grep -v "^$"
|
2020-01-25 22:51:21 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-01-25 23:56:38 -06:00
|
|
|
# Return all todo lines of the given date
|
|
|
|
function date_todo_lines {
|
|
|
|
local date="$1"
|
|
|
|
grep "$date"
|
2020-01-25 22:51:21 -06:00
|
|
|
}
|
|
|
|
|
2020-01-25 23:56:38 -06:00
|
|
|
# Print all todo lines during the giving days following the start-date
|
2020-01-25 22:51:21 -06:00
|
|
|
function upcoming_todo_lines {
|
2020-01-25 23:56:38 -06:00
|
|
|
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")"
|
2020-01-26 01:36:03 -06:00
|
|
|
|
|
|
|
if test "$?" -eq 0 \
|
|
|
|
-a "$i" -ne "$(dec "$limit")" \
|
|
|
|
-a "$QUIET_MODE" -ne 0
|
|
|
|
then echo "---"; fi
|
|
|
|
|
2020-01-25 23:56:38 -06:00
|
|
|
i="$(inc "$i")"
|
|
|
|
done
|
2020-01-25 22:51:21 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-01-25 23:56:38 -06:00
|
|
|
# Print a user-friendly report of upcoming events <3
|
2020-01-25 22:51:21 -06:00
|
|
|
function generate_report {
|
2020-01-25 23:56:38 -06:00
|
|
|
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
|
2020-01-25 22:51:21 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# --------------------------------------
|
|
|
|
# INVOCATION
|
|
|
|
|
2020-01-26 00:35:33 -06:00
|
|
|
BIN="$(echo "$0" | sed 's%.*/%%')"
|
|
|
|
function print_help {
|
|
|
|
echo "usage: $BIN [-hq] [-l \$LIMIT] [-L | -D \$MSG] [\$TODO_PATH]"
|
|
|
|
exit 2
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-01-25 23:56:38 -06:00
|
|
|
# ------------------
|
|
|
|
# OPTIONS
|
|
|
|
|
|
|
|
DIVIDER="----------------------------------------"
|
2020-01-25 22:51:21 -06:00
|
|
|
TODO_FILE="$HOME/.todo"
|
|
|
|
LIMIT=7
|
|
|
|
TODAY_MSG="TODAY"
|
|
|
|
LATER_MSG="NEXT EPISODE..."
|
|
|
|
PAST_MSG="FROM THE GRAVE"
|
|
|
|
QUIET_MODE=1
|
|
|
|
|
2020-01-26 00:35:33 -06:00
|
|
|
while getopts 'l:D:T:L:qh' c; do
|
2020-01-25 22:51:21 -06:00
|
|
|
case "$c" in
|
2020-01-25 23:56:38 -06:00
|
|
|
l) LIMIT="$OPTARG" ;;
|
|
|
|
D) DIVIDER="$OPTARG" ;;
|
|
|
|
T) TODAY_MSG="$OPTARG" ;;
|
|
|
|
L) LATER_MSG="$OPTARG" ;;
|
|
|
|
q) QUIET_MODE=0 ;;
|
2020-01-26 00:35:33 -06:00
|
|
|
h) print_help ;;
|
2020-01-25 22:51:21 -06:00
|
|
|
esac
|
|
|
|
done
|
2020-01-25 23:56:38 -06:00
|
|
|
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
|
2020-01-25 22:51:21 -06:00
|
|
|
|
|
|
|
if test $QUIET_MODE -eq 1; then
|
|
|
|
generate_report "$TODO_FILE" "$LIMIT"
|
|
|
|
else
|
2020-01-25 23:56:38 -06:00
|
|
|
upcoming_todo_lines "$TODO_FILE" "$(today)" "$LIMIT"
|
2020-01-25 22:51:21 -06:00
|
|
|
fi
|