diff options
Diffstat (limited to 'bin/task')
-rwxr-xr-x | bin/task/context | 43 |
1 files changed, 29 insertions, 14 deletions
diff --git a/bin/task/context b/bin/task/context index 75597b3..30e465f 100755 --- a/bin/task/context +++ b/bin/task/context | |||
@@ -1,29 +1,44 @@ | |||
1 | #! /bin/bash | 1 | #! /bin/bash |
2 | 2 | ||
3 | # Usage: | ||
4 | # context 1h2m30s (Update current context with given time) | ||
5 | # context (Check total time of each context) | ||
6 | |||
3 | LOG_FILE=~/log/.context && touch $LOG_FILE | 7 | LOG_FILE=~/log/.context && touch $LOG_FILE |
4 | context="$(cat ~/.task/context)" | 8 | context="$(cat ~/.task/context)" |
5 | count="$1" | 9 | time="$1" |
6 | 10 | ||
7 | if [ -z "$context" ] || [ "$context" = none ]; then | 11 | # Get seconds from given string |
8 | exit 1 | 12 | _get_seconds() { |
9 | fi | 13 | # Modify input to fit the format that `date` can read |
14 | hour=$(grep -o '[0-9.]\+h' <<<"$1" | tr -d h) | ||
15 | min=$(grep -o '[0-9.]\+m' <<<"$1" | tr -d m) | ||
16 | sec=$(grep -o '[0-9.]\+s' <<<"$1" | tr -d s) | ||
17 | echo "${hour:-0}*3600 + ${min:-0}*60 + ${sec:-0}" | bc | ||
18 | } | ||
10 | 19 | ||
20 | # Update time of current context | ||
11 | if [ -n "$1" ]; then | 21 | if [ -n "$1" ]; then |
12 | # Update Log file | 22 | # If current conetxt is not given, exit with 1 |
23 | if [ -z "$context" ] || [ "$context" = none ]; then | ||
24 | exit 1 | ||
25 | fi | ||
26 | |||
27 | given_seconds="$(_get_seconds "$time")" | ||
28 | |||
29 | # Get total time of given time and current context time | ||
13 | while read -r ctx sec; do | 30 | while read -r ctx sec; do |
14 | if [ "$ctx" = "$context" ]; then | 31 | if [ "$ctx" = "$context" ]; then |
15 | summary=$(( "$sec" + "$count" )) | 32 | total=$(( "$sec" + "$given_seconds" )) |
16 | update=true | 33 | sed -i -E "s/^${context}.*/${context}\t${total}/" $LOG_FILE |
17 | break | 34 | exit 0 |
18 | fi | 35 | fi |
19 | done <$LOG_FILE | 36 | done <$LOG_FILE |
20 | if [ "$update" = true ]; then | 37 | |
21 | sed -i -E "s/^$context.*/$context\t$summary/" $LOG_FILE | 38 | # Update Log file |
22 | else | 39 | echo -e "$context\t${given_seconds}" >>$LOG_FILE |
23 | echo -e "$context\t$count" >>$LOG_FILE | 40 | # Print times for each context |
24 | fi | ||
25 | else | 41 | else |
26 | # Print times for each context | ||
27 | while read -r ctx sec; do | 42 | while read -r ctx sec; do |
28 | echo -ne "$ctx\t" | 43 | echo -ne "$ctx\t" |
29 | date -u -d @"$sec" +%H:%M | 44 | date -u -d @"$sec" +%H:%M |