From daa7578cf0b5dadcd83c28f2094bcaacd632df99 Mon Sep 17 00:00:00 2001 From: Hsieh Chin Fan Date: Mon, 10 Apr 2023 14:28:07 +0800 Subject: Print all planned context --- bin/task/context | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) (limited to 'bin/task/context') diff --git a/bin/task/context b/bin/task/context index 431b6ad..600eae0 100755 --- a/bin/task/context +++ b/bin/task/context @@ -5,7 +5,9 @@ # context (Check total time of each context) LOG_FILE=~/log/.context && touch $LOG_FILE +PLAN_FILE=~/log/plan.context.md context="$(cat ~/.task/context)" +[[ "$@" =~ '-s' ]] && SET=true && shift time="$1" # The unit of time display. For example: @@ -14,7 +16,7 @@ time="$1" UNIT=900 BLOCK_CHAR=▊ # This dictionary store the valid contexts and the time I plan to spend on -declare -A PLAN +declare -A SPEND # Get seconds from given string _get_seconds() { @@ -25,15 +27,12 @@ _get_seconds() { echo "${hour:-0}*3600 + ${min:-0}*60 + ${sec:-0}" | bc | xargs printf '%.0f\n' } -_set_context_plan() { - while read line; do - [ -z "$line" ] && break - - ctx=$(awk '{print $1}' <<<"$line") - time=$(awk '{print $2}' <<<"$line") +_save_spend_time_to_dic() { + while read ctx sec; do + [ -z "$ctx" ] && continue - PLAN[$ctx]=$(_get_seconds $time) - done <~/log/plan.context.md + SPEND[$ctx]=$sec + done <$LOG_FILE } # Update spend time on current context @@ -49,19 +48,28 @@ _update_spend_time() { while read -r ctx sec; do if [ "$ctx" = "$context" ]; then total=$(( "$sec" + "$given_seconds" )) + [ "$SET" = true ] && break + sed -i -E "s/^${context}.*/${context}\t${total}/" $LOG_FILE exit 0 fi + # Update Log file done <$LOG_FILE - # Update Log file echo -e "$context\t${given_seconds}" >>$LOG_FILE } # Print spend for each context _print_spend_time() { - _set_context_plan - while read -r ctx sec; do + _save_spend_time_to_dic + while read -r line; do + [ -z "$line" ] && break + + ctx=$(awk '{print $1}' <<<"$line") + time=$(awk '{print $2}' <<<"$line") + seconds_of_plan=$(_get_seconds $time) + sec=${SPEND[$ctx]}; sec=${sec:-0} + # Print context and time I spend echo -ne "$ctx\t\t" date -u -d @"$sec" +%H:%M | tr -d '\n' @@ -72,7 +80,6 @@ _print_spend_time() { [ ! "$number_of_spend" = 0 ] && printf "%0.s$BLOCK_CHAR" $(seq $number_of_spend) echo -en "\e[0m" - seconds_of_plan=${PLAN[$ctx]} if [ -n "$seconds_of_plan" ]; then number_of_plan=$(( ${seconds_of_plan} / $UNIT )) [ "$(( $seconds_of_plan % $UNIT ))" = 0 ] || (( number_of_plan++ )) @@ -91,7 +98,7 @@ _print_spend_time() { fi fi echo - done <$LOG_FILE + done <$PLAN_FILE } if [ -n "$1" ]; then -- cgit v1.2.3-70-g09d2