diff options
| author | Hsieh Chin Fan <pham@topo.tw> | 2023-04-15 08:38:44 +0800 |
|---|---|---|
| committer | Hsieh Chin Fan <pham@topo.tw> | 2023-04-15 08:41:53 +0800 |
| commit | eba32df4188f3c2127cf03ee26cda6eb4f3fab41 (patch) | |
| tree | 95f647276575b7a18b3b42e2c3e39a4cce619aa4 /bin/task | |
| parent | eb61d8dcff14bd2975abeca85227dd65cce1c205 (diff) | |
Update context script
- Use "record" to log spend time
- Remove "-s" option
- Add "edit" option to directly modify record
Diffstat (limited to 'bin/task')
| -rwxr-xr-x | bin/task/context | 36 |
1 files changed, 11 insertions, 25 deletions
diff --git a/bin/task/context b/bin/task/context index c67b4de..95d17c1 100755 --- a/bin/task/context +++ b/bin/task/context | |||
| @@ -14,12 +14,15 @@ LOG_FILE=~/.context && touch $LOG_FILE | |||
| 14 | PLAN_FILE=~/log/plan.context.md | 14 | PLAN_FILE=~/log/plan.context.md |
| 15 | PROC=$$ | 15 | PROC=$$ |
| 16 | 16 | ||
| 17 | context="$(cat ~/.task/context)" | 17 | if [[ "$1" =~ ^e ]]; then |
| 18 | [[ "$*" =~ '-s' ]] && SET=true && shift | 18 | $EDITOR $LOG_FILE |
| 19 | [[ "$1" =~ ^[a-zA-Z] ]] && { | 19 | exit 0 |
| 20 | elif [[ "$1" =~ ^[a-zA-Z] ]]; then | ||
| 20 | echo "$1" > ~/.task/context | 21 | echo "$1" > ~/.task/context |
| 21 | exit 0 | 22 | exit 0 |
| 22 | } | 23 | fi |
| 24 | |||
| 25 | context="$(cat ~/.task/context)" | ||
| 23 | time="$1" | 26 | time="$1" |
| 24 | 27 | ||
| 25 | # The unit of time display. For example: | 28 | # The unit of time display. For example: |
| @@ -54,11 +57,9 @@ _format_seconds() { | |||
| 54 | } | 57 | } |
| 55 | 58 | ||
| 56 | _save_spend_time_to_dic() { | 59 | _save_spend_time_to_dic() { |
| 57 | while read -r ctx sec; do | 60 | while read -r date time ctx sec; do |
| 58 | 61 | SPEND[$ctx]=$(( SPEND[$ctx] + $sec )) | |
| 59 | [ -z "$ctx" ] && continue | 62 | done < <(sed -n "/^$(date +%Y%m%d)/p" $LOG_FILE) |
| 60 | SPEND[$ctx]=$sec | ||
| 61 | done <$LOG_FILE | ||
| 62 | } | 63 | } |
| 63 | 64 | ||
| 64 | _desktop_notify() { | 65 | _desktop_notify() { |
| @@ -75,22 +76,7 @@ _update_spend_time() { | |||
| 75 | given_seconds="$(_get_seconds "$time")" | 76 | given_seconds="$(_get_seconds "$time")" |
| 76 | _desktop_notify "$(_format_seconds "${given_seconds}")" | 77 | _desktop_notify "$(_format_seconds "${given_seconds}")" |
| 77 | 78 | ||
| 78 | # Get total time of given time and current context time | 79 | echo -e "$(date +'%Y%m%d %H:%m:%S')\t$context\t${given_seconds}" >>$LOG_FILE |
| 79 | while read -r ctx sec; do | ||
| 80 | if [ "$ctx" = "$context" ]; then | ||
| 81 | total=$(( "$sec" + "$given_seconds" )) | ||
| 82 | if [ "$SET" = true ]; then | ||
| 83 | sed -i -E "/^${context}.*/d" $LOG_FILE | ||
| 84 | break | ||
| 85 | else | ||
| 86 | sed -i -E "s/^${context}.*/${context}\t${total}/" $LOG_FILE | ||
| 87 | exit 0 | ||
| 88 | fi | ||
| 89 | fi | ||
| 90 | # Update Log file | ||
| 91 | done <"$LOG_FILE" | ||
| 92 | |||
| 93 | echo -e "$context\t${given_seconds}" >>$LOG_FILE | ||
| 94 | } | 80 | } |
| 95 | 81 | ||
| 96 | # Print spend for each context | 82 | # Print spend for each context |