Browse Source

Split timesheet in reusable helper

master
Georg Hopp 8 years ago
parent
commit
7ab0abbc9e
Signed by: ghopp GPG Key ID: 4C5D226768784538
  1. 22
      timesheet/datetimehelper.sh
  2. 10
      timesheet/stringhelper.sh
  3. 90
      timesheet/taskhelper.sh
  4. 154
      timesheet/timesheet.sh
  5. 11
      timesheet/ttyhelper.sh

22
timesheet/datetimehelper.sh

@ -0,0 +1,22 @@
function tstodate() {
[[ $# -eq 0 ]] && return 1
date -d @${1} +%Y-%m-%d
}
function extracttime() {
local OIFS=${IFS}
if [[ $# -eq 0 || -z $1 ]]
then
TIMEKEY=""
TIME="00:00:00"
else
IFS=:
set -- $=1
IFS=${OIFS}
TIMEKEY=$1
TIME=$(printf "%02d:%02d:%02d" ${2:-0} ${3:-0} $4)
fi
}
# vim: set et ts=4 sw=4:

10
timesheet/stringhelper.sh

@ -0,0 +1,10 @@
function truncate() {
[[ $# -lt 2 ]] && return 1
local STRING=$1
[[ ${#STRING} -gt ${2} ]] && STRING="${STRING:0:$(($2-3))}..."
echo -n ${STRING}
}
# vim: set et ts=4 sw=4:

90
timesheet/taskhelper.sh

@ -0,0 +1,90 @@
#!/bin/zsh
function tasktimes() {
[[ $# -eq 0 ]] && return 1
local KEY=${2:-\\1}
local T
local D='([0-9]{4}-[0-9]{2}-[0-9]{2})'
local N='([0-9]+)'
local EXPR='s/'$D'.*PT('$N'H)?('$N'M)?('$N').*/'${KEY}':\3:\5:\7/;te;d;:e'
task $1 info | sed -r ${EXPR} | awk -F: '
{
s = (d[$1][2] + $4) % 60
_m = int((d[$1][2] + $4) / 60)
m = (d[$1][1] + $3 + _m) % 60
_h = int((d[$1][1] + $3 + _m) / 60)
d[$1][0] += $2 + _h
d[$1][1] = m
d[$1][2] = s
}
END {
for(i in d)
printf("%s:%d:%d:%d\n", i, d[i][0], d[i][1], d[i][2])
}' | sort -t: -k1
}
function tasktimestotal() {
tasktimes $1 TOTAL
}
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:

154
timesheet/timesheet.sh

@ -1,144 +1,20 @@
#!/bin/zsh
function tasktimes() {
[[ $# -eq 0 ]] && return 1
local KEY=${2:-\\1}
local T
local D='([0-9]{4}-[0-9]{2}-[0-9]{2})'
local N='([0-9]+)'
local EXPR='s/'$D'.*PT('$N'H)?('$N'M)?('$N').*/'${KEY}':\3:\5:\7/;te;d;:e'
task $1 info | sed -r ${EXPR} | awk -F: '
{
s = (d[$1][2] + $4) % 60
_m = int((d[$1][2] + $4) / 60)
m = (d[$1][1] + $3 + _m) % 60
_h = int((d[$1][1] + $3 + _m) / 60)
d[$1][0] += $2 + _h
d[$1][1] = m
d[$1][2] = s
}
END {
for(i in d)
printf("%s:%d:%d:%d\n", i, d[i][0], d[i][1], d[i][2])
}' | sort -t: -k1
}
function tasktimestotal() {
tasktimes $1 TOTAL
}
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 $@
}
function underline() {
[[ $# -lt 2 ]] && return 1
printf "\033[4m%${1}s\033[24m" "$2"
}
function bold() {
[[ $# -lt 2 ]] && return 1
printf "\033[1m%${1}s\033[22m" "$2"
}
function tstodate() {
[[ $# -eq 0 ]] && return 1
date -d @${1} +%Y-%m-%d
}
function extracttime() {
[[ $# -eq 0 ]] && return 1
local OIFS=${IFS}
if [[ -z $1 ]]
then
TIMEKEY=""
TIME="00:00:00"
else
IFS=:
set -- $(echo $1)
IFS=${OIFS}
TIMEKEY="$1"
TIME=$(printf "%02d:%02d:%02d" ${2:-0} ${3:-0} $4)
fi
}
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 in "$@"
for T in $@
do
extracttime "$T"
extracttime $T
printf "%37s%-4s%10s: %s\n" "spent - " "$(date -d "$TIMEKEY" +%a)" \
"$TIMEKEY" "$TIME"
$TIMEKEY $TIME
done
}
function truncate() {
[[ $# -lt 2 ]] && return 1
local STRING=$1
[[ ${#STRING} -gt ${2} ]] && STRING="${STRING:0:$(($2-3))}..."
echo -n "${STRING}"
}
function showall() {
[[ $# -eq 0 ]] && return 1
@ -151,9 +27,9 @@ function showall() {
"$(underline -10 completed)" \
"$(underline -10 entered)"
for UUID in "$@"
for UUID in $@
do
extracttime "$(tasktimes $UUID TOTAL)"
extracttime $(tasktimes $UUID TOTAL)
printf "%-16s %-35s %-10s %-10s %-8s\n" \
"$(truncate "$(taskproject $UUID)" 16)" \
"$(truncate "$(taskdescription $UUID)" 35)" \
@ -225,21 +101,21 @@ NL=$'\n'
#
SHORTOPTS=?hf:
LONGOPTS=help,html,file
ARGS=$(getopt -o "${SHORTOPTS}" --long "${LONGOPTS}" -n "timesheet" -- "$@")
ARGS=$(getopt -o ${SHORTOPTS} --long ${LONGOPTS} -n timesheet -- $@)
if [ $? -ne 0 ]
then
usage "$0"
usage $0
exit 1
fi
eval set -- "${ARGS}"
eval set -- ${=ARGS}
unset ARGS
while true
do
case "$1" in
case $1 in
'-?'|'--help')
shift
usage "$0"
usage $0
exit 0
;;
'-h'|'--html')
@ -263,7 +139,7 @@ do
esac
done
[[ $OUTFILE ]] && {
[[ -n $OUTFILE ]] && {
if [[ ${DOHTML} -eq 1 ]]
then
exec 1> >(ansi2html >${OUTFILE}.html)

11
timesheet/ttyhelper.sh

@ -0,0 +1,11 @@
function underline() {
[[ $# -lt 2 ]] && return 1
printf "\033[4m%${1}s\033[24m" $2
}
function bold() {
[[ $# -lt 2 ]] && return 1
printf "\033[1m%${1}s\033[22m" $2
}
# vim: set et ts=4 sw=4:
Loading…
Cancel
Save