aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorHsieh Chin Fan <pham@topo.tw>2023-04-10 10:46:03 +0800
committerHsieh Chin Fan <pham@topo.tw>2023-04-10 10:46:03 +0800
commit280208cd6a0d070151c8bd183b04e51fe7a4515a (patch)
treeb8096d651ef44a25d4a5bcc15a529fa54507dc50
parent823d4baaf4c1900f93afd581ca2f9180570f3a1a (diff)
Improve context
Now argument could be '1h20m' or some other things
-rwxr-xr-xbin/task/context43
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
3LOG_FILE=~/log/.context && touch $LOG_FILE 7LOG_FILE=~/log/.context && touch $LOG_FILE
4context="$(cat ~/.task/context)" 8context="$(cat ~/.task/context)"
5count="$1" 9time="$1"
6 10
7if [ -z "$context" ] || [ "$context" = none ]; then 11# Get seconds from given string
8 exit 1 12_get_seconds() {
9fi 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
11if [ -n "$1" ]; then 21if [ -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
25else 41else
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