timesheet.sh 3.91 KB
#!/bin/zsh

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

function printtimes() {
    local T
    for T
    do
        key=${T%%|*}
        dur=${T#*|}
        date="$(date -u -d "${key//_/ }" +%c)"
        ti=$(printf "1970-01-01 %s" ${dur#*:}|xargs -d '\n' date +%X -u -d)
        TIME="$(formattime ${dur})"
        printf "%50s: %14s\n" "${date}" $ti
    done
}

function showall() {
    [[ $# -eq 0 ]] && return 1

    local T UUID OIFS

    printf "%s %s %s %s %s\n" \
        "$(underline -16 project)" \
        "$(underline -35 description)" \
        "$(underline -13 spent)" \
        "$(underline -10 completed)" \
        "$(underline -10 entered)"

    for UUID in $@
    do
        TIME="$(formattime $(tasktimestotal $UUID))"
        printf "%-16s %-35s %13s %-10s %-10s\n" \
            "$(truncate "$(taskproject $UUID)" 16)" \
            "$(truncate "$(taskdescription $UUID)" 35)" \
            "$(bold "" "$TIME")" \
            "$(tstodate $(taskend $UUID))" \
            "$(tstodate $(taskentry $UUID))" \

        printtimes $(tasktimes $UUID)
    done
}

function timesheet() {
    local PHRASE="1-week-ago"
    local START="$(date +%Y-%m-%d -d ${PHRASE})"

    local DONE="$(showall $(doneuuids end.after:${START} $@))"
    local UPCOMING="$(showall $(pendinguuids $@))"
    local BLOCKED="$(showall $(blockeduuids $@))"
    local BLOCKING="$(showall $(blockinguuids $@))"

    DONE="${DONE:+${DONE}${NL}}"
    UPCOMING="${UPCOMING:+${UPCOMING}${NL}}"
    BLOCKED="${BLOCKED:+${BLOCKED}${NL}}"
    BLOCKING="${BLOCKING:+${BLOCKING}${NL}}"

    printf " (generated at %s)\n\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n" \
        "${TODAY}" \
        "$(bold "" "Tasks completed from $START to $TODAY (back $PHRASE)")" \
        "${DONE}" \
        "$(bold "" "Upcoming tasks")" \
        "${UPCOMING}" \
        "$(bold "" "Blocked tasks")" \
        "${BLOCKED}" \
        "$(bold "" "Blocking tasks")" \
        "${BLOCKING}"

    halfyear="entry.after:$(date -d '6 month ago' +%F)"
    twoyears="entry.after:$(date -d '2 years ago' +%F)"

    printf "%s%s\n\n%s%s\n%s\n\n%s\n\n%s\n" \
        "$(bold "" "Summary")" \
        "$(task rc._forcecolor=on $@ summary 2>/dev/null)" \
        "$(bold "" "History")" \
        "$(task rc._forcecolor=on $@ ${halfyear} history 2>/dev/null)" \
        "$(task rc._forcecolor=on $@ ${halfyear} ghistory 2>/dev/null)" \
        "$(task rc._forcecolor=on $@ ${halfyear} burndown.daily 2>/dev/null)" \
        "$(task rc._forcecolor=on $@ ${twoyears} burndown 2>/dev/null)"
}

function usage() {
    local USAGE=$(cat <<-USAGE
	Usage: %s [OPTION]... [FILTER]

	OPTIONS:
	  -?, --help    Show this help
	  -h, --html    Create HTML form ANSI data.
	  -f, --file    The file to write data to. Actually this specifies the
	                basename which will be postfixed by the creation date.

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

TODAY="$(date +%Y-%m-%d)"
NL=$'\n'

#
# parse command line arguments
#
SHORTOPTS=?hf:
LONGOPTS=help,html,file
ARGS=$(getopt -o ${SHORTOPTS} --long ${LONGOPTS} -n timesheet -- $@)
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
            ;;
        '-h'|'--html')
            DOHTML=1
            shift
            continue
            ;;
        '-f'|'--file')
            OUTFILE="${2}_$TODAY"
            shift 2
            continue
            ;;
        '--')
            shift
            break
            ;;
        *)
            echo 'Internal error!' >&2
            exit 1
            ;;
    esac
done

[[ -n $OUTFILE ]] && {
    if [[ ${DOHTML} -eq 1 ]]
    then
        exec 1> >(ansi2html >${OUTFILE}.html)
    else
        exec 1> ${OUTFILE}.ansi
    fi
}

timesheet $@

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