diff options
Diffstat (limited to 'bin/task/context')
| -rwxr-xr-x | bin/task/context | 59 |
1 files changed, 53 insertions, 6 deletions
diff --git a/bin/task/context b/bin/task/context index 30e465f..d0bb6c4 100755 --- a/bin/task/context +++ b/bin/task/context | |||
| @@ -8,13 +8,32 @@ LOG_FILE=~/log/.context && touch $LOG_FILE | |||
| 8 | context="$(cat ~/.task/context)" | 8 | context="$(cat ~/.task/context)" |
| 9 | time="$1" | 9 | time="$1" |
| 10 | 10 | ||
| 11 | # The unit of time display. For example: | ||
| 12 | # If it is 900(seconds), then the minimal symbol of | ||
| 13 | # time display is 15min | ||
| 14 | UNIT=900 | ||
| 15 | BLOCK_CHAR=▊ | ||
| 16 | # This dictionary store the valid contexts and the time I plan to spend on | ||
| 17 | declare -A PLAN | ||
| 18 | |||
| 11 | # Get seconds from given string | 19 | # Get seconds from given string |
| 12 | _get_seconds() { | 20 | _get_seconds() { |
| 13 | # Modify input to fit the format that `date` can read | 21 | # Modify input to fit the format that `date` can read |
| 14 | hour=$(grep -o '[0-9.]\+h' <<<"$1" | tr -d h) | 22 | hour=$(grep -o '[0-9.]\+h' <<<"$1" | tr -d '[:alpha:]') |
| 15 | min=$(grep -o '[0-9.]\+m' <<<"$1" | tr -d m) | 23 | min=$(grep -o '[0-9.]\+m' <<<"$1" | tr -d '[:alpha:]') |
| 16 | sec=$(grep -o '[0-9.]\+s' <<<"$1" | tr -d s) | 24 | sec=$(grep -o '[0-9.]\+s' <<<"$1" | tr -d '[:alpha:]') |
| 17 | echo "${hour:-0}*3600 + ${min:-0}*60 + ${sec:-0}" | bc | 25 | echo "${hour:-0}*3600 + ${min:-0}*60 + ${sec:-0}" | bc | xargs printf '%.0f\n' |
| 26 | } | ||
| 27 | |||
| 28 | _set_context_plan() { | ||
| 29 | while read line; do | ||
| 30 | [ -z "$line" ] && break | ||
| 31 | |||
| 32 | ctx=$(awk '{print $1}' <<<"$line") | ||
| 33 | time=$(awk '{print $2}' <<<"$line") | ||
| 34 | |||
| 35 | PLAN[$ctx]=$(_get_seconds $time) | ||
| 36 | done <~/log/plan.context.md | ||
| 18 | } | 37 | } |
| 19 | 38 | ||
| 20 | # Update time of current context | 39 | # Update time of current context |
| @@ -39,8 +58,36 @@ if [ -n "$1" ]; then | |||
| 39 | echo -e "$context\t${given_seconds}" >>$LOG_FILE | 58 | echo -e "$context\t${given_seconds}" >>$LOG_FILE |
| 40 | # Print times for each context | 59 | # Print times for each context |
| 41 | else | 60 | else |
| 61 | _set_context_plan | ||
| 42 | while read -r ctx sec; do | 62 | while read -r ctx sec; do |
| 43 | echo -ne "$ctx\t" | 63 | # Print context and time I spend |
| 44 | date -u -d @"$sec" +%H:%M | 64 | echo -ne "$ctx\t\t" |
| 65 | date -u -d @"$sec" +%H:%M | tr -d '\n' | ||
| 66 | |||
| 67 | number_of_spend=$(( $sec / $UNIT )) | ||
| 68 | # Print block of time spend (green) | ||
| 69 | echo -ne "\t\t\e[32m" | ||
| 70 | [ ! "$number_of_spend" = 0 ] && printf "%0.s$BLOCK_CHAR" $(seq $number_of_spend) | ||
| 71 | echo -en "\e[0m" | ||
| 72 | |||
| 73 | seconds_of_plan=${PLAN[$ctx]} | ||
| 74 | if [ -n "$seconds_of_plan" ]; then | ||
| 75 | number_of_plan=$(( ${seconds_of_plan} / $UNIT )) | ||
| 76 | [ "$(( $seconds_of_plan % $UNIT ))" = 0 ] || (( number_of_plan++ )) | ||
| 77 | number_of_remaining=$(( $number_of_plan - $number_of_spend )) | ||
| 78 | |||
| 79 | # Print block of remaining time (normal) | ||
| 80 | if (( "$number_of_remaining" > 0 )); then | ||
| 81 | printf "%0.s$BLOCK_CHAR" $(seq $number_of_remaining) | ||
| 82 | # Print block of exceed time (red) | ||
| 83 | elif (( "$number_of_remaining" < 0 )); then | ||
| 84 | number_of_exceed=${number_of_remaining#-} | ||
| 85 | printf "%0.s\b" $(seq $number_of_exceed) | ||
| 86 | echo -ne "\e[31m" | ||
| 87 | printf "%0.s$BLOCK_CHAR" $(seq $number_of_exceed) | ||
| 88 | echo -en "\e[0m" | ||
| 89 | fi | ||
| 90 | fi | ||
| 91 | echo | ||
| 45 | done <$LOG_FILE | 92 | done <$LOG_FILE |
| 46 | fi | 93 | fi |