#!/bin/bash
# @desc simple task list viewer
TASKFILE=~/tasks.txt

sedhl(){
	HL="$(tput setaf $1)$(tput bold)"
	RS="$(tput sgr0)"
	sed -u "s/$2/${HL}\0${RS}/"
}

taskfmt() {
	sedhl 1 "0d" | sedhl 3 "[1-3]d" | sedhl 2 "[4-7]d" 
}

show_tasks(){
	awk -F ' ' '{gsub("-"," ",$1);days=int((mktime($1" 00 00 00")-systime())/(24*60*60)+.5);gsub(" ","-",$1);$1=NR":"$1" "days"d";print $0}' "$TASKFILE" | taskfmt
}
show_tasks_day(){
	awk -F ' ' -v"period=$(date --date="$1" +%s)" '{gsub("-"," ",$1);t=mktime($1" 00 00 00");days=int((t-systime())/(24*60*60)+.5);gsub(" ","-",$1);$1=NR":"$1" "days"d";if(t<period) print $0}' "$TASKFILE" | taskfmt
}

touch $TASKFILE
case $1 in
	x)
		sed -i "$2 d" "$TASKFILE"
		show_tasks
		exit	
	;;
esac
if [[ $# -eq 1 && -n "$1" ]]
then
	show_tasks_day "$1"
	exit
fi
if [ $# -eq 0 ]
then
	show_tasks 
	exit
fi
echo $(date --date="$1" -I) ${@:2} | sort -o "$TASKFILE" -m - "$TASKFILE"
show_tasks 
