report.sh 2.3 KB
#!/bin/zsh

source $0:A:h/datetimehelper.sh
source $0:A:h/taskhelper.sh
source $0:A:h/ttyhelper.sh

function tasksallsince() {
    [[ $# -lt 1 ]] && return 1
    taskuuids \(\(\( +COMPLETED or +DELETED \) end.after:${1} \) \
        or +PENDING \) $@
}

function datetimedescription() {
    [[ $# -lt 1 ]] && return 1
    for UUID in $(tasksallsince $@)
    do
        DESCRIPTION="$(taskdescription $UUID)"
        PROJECT="$(taskproject $UUID)"
		COMPLETED="$(taskuuids $UUID \( +COMPLETED or +DELETED \))"
        for T in $(tasktimes $UUID)
        do
            extracttime $T
            printf "%s;%s;%s;%s;%s\n" \
				"$TIMEKEY" "$TIME" "$PROJECT" "$DESCRIPTION" "$COMPLETED"
        done
    done
}

function report() {
    [[ $# -lt 1 ]] && return 1
    local D DATE T
    DATE=$1
	for LINE in "${(f)$(datetimedescription $@|sort -t\; -f -k1,1 -k3,3)}"
    do
        set -- "${(@s(;))LINE}"
        if [[ -n $1 && $(date -d $1 +%s) -ge $(date -d $DATE +%s) ]]
		then
			if [[ $D != $1 ]]
			then
				D=$1
				printf "%s\n" $(underline "" $D)
			fi
			T=$2
			if [[ -n $5 ]] && T=$(bold "" $T)
			if [[ -n $D ]] && printf "%s [%s] - %s\n" "$T" "$3" "$4"
		fi
    done
}

function usage() {
    local USAGE=$(cat <<-USAGE 
    Usage: %s [-?] [DATETIME] [FILTER] 
	
	OPTIONS: 
	  -?, --help    Show this help
	  -d, --date    DATETIME is a date and time identifier as accepted by the
	                date command. It specifies how long back done tasks
	                should be considered.

	FILTER can be additional taskwarriors filters to limit the result 
	any further. 
	USAGE
    )
    /usr/bin/printf "${USAGE}\n" $0
}

#
# parse command line arguments
#
SHORTOPTS=?d:
LONGOPTS=help,date
ARGS=$(getopt -o ${SHORTOPTS} --long ${LONGOPTS} -n report -- $@)
if [ $? -ne 0 ]
then
    usage $0
    exit 1
fi
eval set -- ${=ARGS}
unset ARGS

while true
do
    case $1 in
        '-?'|'--help')
            shift
            usage $0
            exit 0
            ;;
        '-d'|'--date')
            DATEFROM=$(date -d ${2} +%Y-%m-%d)
            shift 2
            continue
            ;;
        '--')
            shift
            break
            ;;
        *)
            echo 'Internal error!' >&2
            exit 1
            ;;
    esac
done

DATEFROM=${DATEFROM:-$(date +%Y-%m-01)}
report $DATEFROM $@

# vim: set et ts=4 sw=4: