diff options
Diffstat (limited to 'bin')
| -rwxr-xr-x | bin/task/context | 35 |
1 files changed, 21 insertions, 14 deletions
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 @@ | |||
| 5 | # context (Check total time of each context) | 5 | # context (Check total time of each context) |
| 6 | 6 | ||
| 7 | LOG_FILE=~/log/.context && touch $LOG_FILE | 7 | LOG_FILE=~/log/.context && touch $LOG_FILE |
| 8 | PLAN_FILE=~/log/plan.context.md | ||
| 8 | context="$(cat ~/.task/context)" | 9 | context="$(cat ~/.task/context)" |
| 10 | [[ "$@" =~ '-s' ]] && SET=true && shift | ||
| 9 | time="$1" | 11 | time="$1" |
| 10 | 12 | ||
| 11 | # The unit of time display. For example: | 13 | # The unit of time display. For example: |
| @@ -14,7 +16,7 @@ time="$1" | |||
| 14 | UNIT=900 | 16 | UNIT=900 |
| 15 | BLOCK_CHAR=▊ | 17 | BLOCK_CHAR=▊ |
| 16 | # This dictionary store the valid contexts and the time I plan to spend on | 18 | # This dictionary store the valid contexts and the time I plan to spend on |
| 17 | declare -A PLAN | 19 | declare -A SPEND |
| 18 | 20 | ||
| 19 | # Get seconds from given string | 21 | # Get seconds from given string |
| 20 | _get_seconds() { | 22 | _get_seconds() { |
| @@ -25,15 +27,12 @@ _get_seconds() { | |||
| 25 | echo "${hour:-0}*3600 + ${min:-0}*60 + ${sec:-0}" | bc | xargs printf '%.0f\n' | 27 | echo "${hour:-0}*3600 + ${min:-0}*60 + ${sec:-0}" | bc | xargs printf '%.0f\n' |
| 26 | } | 28 | } |
| 27 | 29 | ||
| 28 | _set_context_plan() { | 30 | _save_spend_time_to_dic() { |
| 29 | while read line; do | 31 | while read ctx sec; do |
| 30 | [ -z "$line" ] && break | 32 | [ -z "$ctx" ] && continue |
| 31 | |||
| 32 | ctx=$(awk '{print $1}' <<<"$line") | ||
| 33 | time=$(awk '{print $2}' <<<"$line") | ||
| 34 | 33 | ||
| 35 | PLAN[$ctx]=$(_get_seconds $time) | 34 | SPEND[$ctx]=$sec |
| 36 | done <~/log/plan.context.md | 35 | done <$LOG_FILE |
| 37 | } | 36 | } |
| 38 | 37 | ||
| 39 | # Update spend time on current context | 38 | # Update spend time on current context |
| @@ -49,19 +48,28 @@ _update_spend_time() { | |||
| 49 | while read -r ctx sec; do | 48 | while read -r ctx sec; do |
| 50 | if [ "$ctx" = "$context" ]; then | 49 | if [ "$ctx" = "$context" ]; then |
| 51 | total=$(( "$sec" + "$given_seconds" )) | 50 | total=$(( "$sec" + "$given_seconds" )) |
| 51 | [ "$SET" = true ] && break | ||
| 52 | |||
| 52 | sed -i -E "s/^${context}.*/${context}\t${total}/" $LOG_FILE | 53 | sed -i -E "s/^${context}.*/${context}\t${total}/" $LOG_FILE |
| 53 | exit 0 | 54 | exit 0 |
| 54 | fi | 55 | fi |
| 56 | # Update Log file | ||
| 55 | done <$LOG_FILE | 57 | done <$LOG_FILE |
| 56 | 58 | ||
| 57 | # Update Log file | ||
| 58 | echo -e "$context\t${given_seconds}" >>$LOG_FILE | 59 | echo -e "$context\t${given_seconds}" >>$LOG_FILE |
| 59 | } | 60 | } |
| 60 | 61 | ||
| 61 | # Print spend for each context | 62 | # Print spend for each context |
| 62 | _print_spend_time() { | 63 | _print_spend_time() { |
| 63 | _set_context_plan | 64 | _save_spend_time_to_dic |
| 64 | while read -r ctx sec; do | 65 | while read -r line; do |
| 66 | [ -z "$line" ] && break | ||
| 67 | |||
| 68 | ctx=$(awk '{print $1}' <<<"$line") | ||
| 69 | time=$(awk '{print $2}' <<<"$line") | ||
| 70 | seconds_of_plan=$(_get_seconds $time) | ||
| 71 | sec=${SPEND[$ctx]}; sec=${sec:-0} | ||
| 72 | |||
| 65 | # Print context and time I spend | 73 | # Print context and time I spend |
| 66 | echo -ne "$ctx\t\t" | 74 | echo -ne "$ctx\t\t" |
| 67 | date -u -d @"$sec" +%H:%M | tr -d '\n' | 75 | date -u -d @"$sec" +%H:%M | tr -d '\n' |
| @@ -72,7 +80,6 @@ _print_spend_time() { | |||
| 72 | [ ! "$number_of_spend" = 0 ] && printf "%0.s$BLOCK_CHAR" $(seq $number_of_spend) | 80 | [ ! "$number_of_spend" = 0 ] && printf "%0.s$BLOCK_CHAR" $(seq $number_of_spend) |
| 73 | echo -en "\e[0m" | 81 | echo -en "\e[0m" |
| 74 | 82 | ||
| 75 | seconds_of_plan=${PLAN[$ctx]} | ||
| 76 | if [ -n "$seconds_of_plan" ]; then | 83 | if [ -n "$seconds_of_plan" ]; then |
| 77 | number_of_plan=$(( ${seconds_of_plan} / $UNIT )) | 84 | number_of_plan=$(( ${seconds_of_plan} / $UNIT )) |
| 78 | [ "$(( $seconds_of_plan % $UNIT ))" = 0 ] || (( number_of_plan++ )) | 85 | [ "$(( $seconds_of_plan % $UNIT ))" = 0 ] || (( number_of_plan++ )) |
| @@ -91,7 +98,7 @@ _print_spend_time() { | |||
| 91 | fi | 98 | fi |
| 92 | fi | 99 | fi |
| 93 | echo | 100 | echo |
| 94 | done <$LOG_FILE | 101 | done <$PLAN_FILE |
| 95 | } | 102 | } |
| 96 | 103 | ||
| 97 | if [ -n "$1" ]; then | 104 | if [ -n "$1" ]; then |