diff options
| author | Hsieh Chin Fan <pham@topo.tw> | 2023-04-11 22:18:14 +0800 |
|---|---|---|
| committer | Hsieh Chin Fan <pham@topo.tw> | 2023-04-11 22:18:14 +0800 |
| commit | aefed8f34bbc8675cf3eda4f6ee160277dcdcccd (patch) | |
| tree | 11c3344b818600614c7685831b70227641328b2b /bin | |
| parent | 164c161a97d985270ac2dbed51cea4624a272d25 (diff) | |
Update
Diffstat (limited to 'bin')
| -rwxr-xr-x | bin/task/context | 27 |
1 files changed, 16 insertions, 11 deletions
diff --git a/bin/task/context b/bin/task/context index 89773b5..d1942a5 100755 --- a/bin/task/context +++ b/bin/task/context | |||
| @@ -4,11 +4,13 @@ | |||
| 4 | # context 1h2m30s (Update current context with given time) | 4 | # context 1h2m30s (Update current context with given time) |
| 5 | # context -s 1h20m (Set current context with given time) | 5 | # context -s 1h20m (Set current context with given time) |
| 6 | # context (Check total time of each context) | 6 | # context (Check total time of each context) |
| 7 | # | ||
| 8 | # TODO support minus time | ||
| 7 | 9 | ||
| 8 | LOG_FILE=~/log/.context && touch $LOG_FILE | 10 | LOG_FILE=~/log/.context && touch $LOG_FILE |
| 9 | PLAN_FILE=~/log/plan.context.md | 11 | PLAN_FILE=~/log/plan.context.md |
| 10 | PROC=$$ | 12 | PROC=$$ |
| 11 | trap 'exit 1' 10 | 13 | trap 'exit 1' SIGUSR1 |
| 12 | 14 | ||
| 13 | context="$(cat ~/.task/context)" | 15 | context="$(cat ~/.task/context)" |
| 14 | [[ "$*" =~ '-s' ]] && SET=true && shift | 16 | [[ "$*" =~ '-s' ]] && SET=true && shift |
| @@ -36,7 +38,7 @@ _get_seconds() { | |||
| 36 | } | 38 | } |
| 37 | 39 | ||
| 38 | _save_spend_time_to_dic() { | 40 | _save_spend_time_to_dic() { |
| 39 | while read ctx sec; do | 41 | while read -r ctx sec; do |
| 40 | [ -z "$ctx" ] && continue | 42 | [ -z "$ctx" ] && continue |
| 41 | 43 | ||
| 42 | SPEND[$ctx]=$sec | 44 | SPEND[$ctx]=$sec |
| @@ -52,6 +54,9 @@ _update_spend_time() { | |||
| 52 | 54 | ||
| 53 | given_seconds="$(_get_seconds "$time")" | 55 | given_seconds="$(_get_seconds "$time")" |
| 54 | 56 | ||
| 57 | date >>/tmp/context | ||
| 58 | echo "$given_seconds" >>/tmp/context | ||
| 59 | |||
| 55 | # Get total time of given time and current context time | 60 | # Get total time of given time and current context time |
| 56 | while read -r ctx sec; do | 61 | while read -r ctx sec; do |
| 57 | if [ "$ctx" = "$context" ]; then | 62 | if [ "$ctx" = "$context" ]; then |
| @@ -62,7 +67,7 @@ _update_spend_time() { | |||
| 62 | exit 0 | 67 | exit 0 |
| 63 | fi | 68 | fi |
| 64 | # Update Log file | 69 | # Update Log file |
| 65 | done <$LOG_FILE | 70 | done <"$LOG_FILE" |
| 66 | 71 | ||
| 67 | echo -e "$context\t${given_seconds}" >>$LOG_FILE | 72 | echo -e "$context\t${given_seconds}" >>$LOG_FILE |
| 68 | } | 73 | } |
| @@ -75,7 +80,7 @@ _print_spend_time() { | |||
| 75 | 80 | ||
| 76 | ctx=$(awk '{print $1}' <<<"$line") | 81 | ctx=$(awk '{print $1}' <<<"$line") |
| 77 | time=$(awk '{print $2}' <<<"$line") | 82 | time=$(awk '{print $2}' <<<"$line") |
| 78 | seconds_of_plan=$(_get_seconds $time) | 83 | seconds_of_plan=$(_get_seconds "$time") |
| 79 | sec=${SPEND[$ctx]}; sec=${sec:-0} | 84 | sec=${SPEND[$ctx]}; sec=${sec:-0} |
| 80 | 85 | ||
| 81 | # Print context and time I spend | 86 | # Print context and time I spend |
| @@ -85,26 +90,26 @@ _print_spend_time() { | |||
| 85 | echo -n "/" | 90 | echo -n "/" |
| 86 | date -u -d @"$seconds_of_plan" +%H:%M | tr -d '\n' | 91 | date -u -d @"$seconds_of_plan" +%H:%M | tr -d '\n' |
| 87 | 92 | ||
| 88 | number_of_spend=$(( $sec / $UNIT )) | 93 | number_of_spend=$(( sec / UNIT )) |
| 89 | # Print block of time spend (green) | 94 | # Print block of time spend (green) |
| 90 | echo -ne "\t\t\e[32m" | 95 | echo -ne "\t\t\e[32m" |
| 91 | [ ! "$number_of_spend" = 0 ] && printf "%0.s$BLOCK_CHAR" $(seq $number_of_spend) | 96 | [ ! "$number_of_spend" = 0 ] && printf "%0.s$BLOCK_CHAR" $(seq $number_of_spend) |
| 92 | echo -en "\e[0m" | 97 | echo -en "\e[0m" |
| 93 | 98 | ||
| 94 | if [ -n "$seconds_of_plan" ]; then | 99 | if [ -n "$seconds_of_plan" ]; then |
| 95 | number_of_plan=$(( ${seconds_of_plan} / $UNIT )) | 100 | number_of_plan=$(( seconds_of_plan / UNIT )) |
| 96 | [ "$(( $seconds_of_plan % $UNIT ))" = 0 ] || (( number_of_plan++ )) | 101 | [ "$(( seconds_of_plan % UNIT ))" = 0 ] || (( number_of_plan++ )) |
| 97 | number_of_remaining=$(( $number_of_plan - $number_of_spend )) | 102 | number_of_remaining=$(( number_of_plan - number_of_spend )) |
| 98 | 103 | ||
| 99 | # Print block of remaining time (normal) | 104 | # Print block of remaining time (normal) |
| 100 | if (( "$number_of_remaining" > 0 )); then | 105 | if (( "$number_of_remaining" > 0 )); then |
| 101 | printf "%0.s$BLOCK_CHAR" $(seq $number_of_remaining) | 106 | printf "%0.s$BLOCK_CHAR" $(seq $number_of_remaining) |
| 102 | # Print block of exceed time (red) | 107 | # Print block of exceed time (red) |
| 103 | elif (( "$number_of_remaining" < 0 )); then | 108 | elif (( "$number_of_remaining" < 0 )); then |
| 104 | number_of_exceed=${number_of_remaining#-} | ||
| 105 | printf "%0.s\b" $(seq $number_of_exceed) | ||
| 106 | echo -ne "\e[31m" | 109 | echo -ne "\e[31m" |
| 107 | printf "%0.s$BLOCK_CHAR" $(seq $number_of_exceed) | 110 | number_of_exceed=${number_of_remaining#-} |
| 111 | printf "%0.s\b" $(seq "$number_of_exceed") | ||
| 112 | printf "%0.s$BLOCK_CHAR" $(seq "$number_of_exceed") | ||
| 108 | echo -en "\e[0m" | 113 | echo -en "\e[0m" |
| 109 | fi | 114 | fi |
| 110 | fi | 115 | fi |