aboutsummaryrefslogtreecommitdiffhomepage
path: root/bin/task/context
diff options
context:
space:
mode:
authorHsieh Chin Fan <pham@topo.tw>2023-04-10 14:28:07 +0800
committerHsieh Chin Fan <pham@topo.tw>2023-04-10 14:28:07 +0800
commitdaa7578cf0b5dadcd83c28f2094bcaacd632df99 (patch)
tree2c7d16c0a1134409bcfb5f91920b9a39e3fc8540 /bin/task/context
parent91dd04beb19dc3d141c4743ce85579e7d6c29855 (diff)
Print all planned context
Diffstat (limited to 'bin/task/context')
-rwxr-xr-xbin/task/context35
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
7LOG_FILE=~/log/.context && touch $LOG_FILE 7LOG_FILE=~/log/.context && touch $LOG_FILE
8PLAN_FILE=~/log/plan.context.md
8context="$(cat ~/.task/context)" 9context="$(cat ~/.task/context)"
10[[ "$@" =~ '-s' ]] && SET=true && shift
9time="$1" 11time="$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"
14UNIT=900 16UNIT=900
15BLOCK_CHAR=▊ 17BLOCK_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
17declare -A PLAN 19declare -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
97if [ -n "$1" ]; then 104if [ -n "$1" ]; then