original.sh 3.77 KB
#!/bin/bash
# File : timesheet.sh
#
# This generates timesheet data for my fedora ${TASK}s only
#exec 2>/dev/null

WHICH='/usr/bin/which'
DATE="$(${WHICH} date)"
PRINTF="$(${WHICH} printf)"
ECHO="$(${WHICH} echo)"
SED="$(${WHICH} sed)"
TASK="$(${WHICH} task)"
HEAD="$(${WHICH} head)"
ANSI2HTML="$(${WHICH} ansi2html)"

phrase="1-weeks-ago"
fmt="%Y-%m-%d"
start=$(${DATE} +$fmt -d $phrase)
end=$(${DATE} +$fmt)
filter=''

function usage()
{
    echo   "${0} [OPTIONS] [PROJECT]"
    echo
    echo   "Get a timesheet for the past last week."
    echo   "The \"spent\" time is the whole time spent with this task."
    echo
    echo   "OPTIONS:"
    printf "  %10s : %s\n" '-?' 'Get this help message.'
    printf "  %10s : %s\n" '-h' 'Write HTML instead of ansi console text.'
    printf "  %10s : %s\n" '-o path' 'Save the date under a file defined by'
    printf "  %10s   %s\n" '' '"path" preceeded by the current date and time'
    printf "  %10s   %s\n" '' 'and an ".html" suffix.'
    echo
    echo   "PROJECT:"
    echo   "  The project to create the timesheet for."
}

OUTFILE=''
TO_HTML=0

while getopts 'o:h?' opt
do
	case $opt in
		o)
			OUTFILE="${OPTARG}";;
		h)
			TO_HTML=1;;
		?)
			usage
			exit 0;;
		*)
			usage
			exit 1;;
	esac
done

shift $((${OPTIND} - 1))

test ${1} && filter="project.is:${1}"

function taskdata()
{
	echo "$(${TASK} $@ | ${SED} '1,3d' | ${HEAD} -n -2)"
}

function display()
{
	${ECHO}
	${ECHO} -en "\033[4mproject        \033[24m "
	${ECHO} -en "\033[4mdescription                        \033[24m "
	${ECHO} -en "\033[4mcompleted \033[24m "
	${ECHO} -en "\033[4mentered   \033[24m "
	${ECHO} -e "\033[4mspent   \033[24m"
	for i in $*
	do
		project="$(taskdata uuid:${i} project)"
		local description="$(taskdata uuid:${i} description)"
		local end="$(taskdata uuid:${i} end)"
		local entry="$(taskdata uuid:${i} entry)"
		local times="$(${TASK} uuid:${i} info | ${SED} '/duration/{;s/.*duration: PT\(\([0-9]\+\)H\)\?\(\([0-9]\+\)M\)\?\(\([0-9]\+\)S\)\?.*/\2:\4:\6/;s/^:/0:/;s/::/:0:/;s/:$/:0/;p;};d')"

		project="$(${PRINTF} "%.15s" "${project}")"
		description="$(${PRINTF} "%.35s" "${description}")"

		local years=0
		local months=0
		local days=0
		local hours=0
		local minutes=0
		local seconds=0
		for t in ${times}
		do
			ta=(${t//:/ })
			hours=$((hours+$((10#${ta[0]}))))
			minutes=$((minutes+$((10#${ta[1]}))))
			seconds=$((seconds+$((10#${ta[2]}))))
		done
		minutes=$((minutes+$((seconds/60))))
		seconds=$((seconds%60))
		hours=$((hours+$((minutes/60))))
		minutes=$((minutes%60))

		${PRINTF} "% -15s % -35s % -10s % -10s %02d:%02d:%02d\n" \
			"${project}" "${description}" "${end}" \
			"${entry}" "${hours}" "${minutes}" "${seconds}"
	done
	${ECHO}
}

function timesheet()
{
	${ECHO} " (generated at $(date))"
	${ECHO}
	${ECHO} -e "\033[1mTasks completed from $start to $end (back $phrase)\033[22m"
	display $(taskdata uuid ${filter} end.after:$start)

	${ECHO}
	${ECHO} -e "\033[1mUpcoming tasks\033[22m"
	display $(taskdata uuidnext ${filter})

	${ECHO}
	${ECHO} -e "\033[1mBlocked tasks\033[22m"
	${TASK} blocked $filter rc._forcecolor=on

	${ECHO}
	${ECHO} -e "\033[1mBlocking tasks\033[22m"
	${TASK} blocking $filter rc._forcecolor=on

	${ECHO}
	${ECHO} -e "\033[1mSummary\033[22m"
	${TASK} summary $filter rc._forcecolor=on

	${ECHO}
	${ECHO} -e "\033[1mHistory\033[22m"
	${TASK} history $filter rc._forcecolor=on
	${TASK} ghistory $filter rc._forcecolor=on
	${ECHO} -e "\n"
	${TASK} burndown.daily rc._forcecolor=on
	${ECHO} -e "\n"
	${TASK} burndown rc._forcecolor=on
}

if [ ${OUTFILE} ]
then
	OUTFILE="${OUTFILE}_$(${DATE} +"%Y-%m-%d_%H:%M:%S")"
	if [ ${TO_HTML} -ne 0 ]
	then
		timesheet $@ | ${ANSI2HTML} >${OUTFILE}.html
	else
		timesheet $@ >${OUTFILE}
	fi
else
	if [ ${TO_HTML} -ne 0 ]
	then
		timesheet $@ | ${ANSI2HTML}
	else
		timesheet $@
	fi
fi

# vim: set ft=sh ts=4 sw=4: