taskhelper.sh 1.87 KB
#!/bin/zsh

function duration() {
    [[ $# -eq 0 ]] && return 1
    task $1 info|\
        sed 's/\([^ ]*\) \([^ ]*\).*duration: \(\([0-9]*\)d \)\?\([^)]*\).*/\1_\2|\4:\5/;te;d;:e'
}

function tasktimes() {
    [[ $# -eq 0 ]] && return 1
    duration $1
}

function tasktimestotal() {
    set -- $(duration $1)

    # == Total calculation ==
    DAYS=0
    TI=0
    for dur; do
        # first skip key
        key=${dur%%|*}
        dur=${dur#*|}

        days=${dur%%:*}
        DAYS=$((DAYS+${days:-0}))

        ti=$(printf "1970-01-01 %s" ${dur#*:}|xargs -d '\n' date +%s -u -d)
        TI=$((TI+$ti))

        if [[ $TI -ge 86400 ]]; then
            # bigger than a day?
            DAYS=$((DAYS+1))
            TI=$((TI-86400))
        fi
    done
    TI="$(date -u -d@${TI} +%X)"
    echo "${DAYS}:${TI}"
    # == End Total Calculation ==
}

function taskdescription() {
    [[ $# -eq 0 ]] && return 1
    task $1 _unique description
}

function taskproject() {
    [[ $# -eq 0 ]] && return 1
    task $1 _unique project
}

function taskurgency() {
    [[ $# -eq 0 ]] && return 1
    task $1 _urgency | cut -d\  -f 4
}

function taskentry() {
    [[ $# -eq 0 ]] && return 1
    task $1 _unique entry
}

function taskend() {
    [[ $# -eq 0 ]] && return 1
    task $1 _unique end
}

function taskuuids() {
    local UUID

    [[ $# -eq 0 ]] && set -- \( +PENDING or +DONE \)
    for UUID in $(task $@ _uuid)
    do
        printf "%05.2f\037%s\037%s\n" \
            "$(taskurgency $UUID)" \
            "$(taskdescription $UUID)" \
            $UUID
    done | sort -t$'\037' -k1nr,2 | cut -d$'\037' -f3
}

function pendinguuids() {
    taskuuids +PENDING $@
}

function doneuuids() {
    taskuuids +COMPLETED $@
}

function blockeduuids() {
    taskuuids +BLOCKED $@
}

function blockinguuids() {
    taskuuids +BLOCKING $@
}

function deleteduuids() {
    taskuuids +DELETED $@
}

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