From 71dce3a13572fdae0ce5f7b1169c69ea9610d800 Mon Sep 17 00:00:00 2001 From: Hsieh Chin Fan Date: Thu, 6 Apr 2023 21:44:47 +0800 Subject: Add feature for time of current context --- X11/show_timer.sh | 8 ++++++-- bin/task/context_spend_time.sh | 31 +++++++++++++++++++++++++++++++ bin/unix/timer.sh | 9 +++++---- 3 files changed, 42 insertions(+), 6 deletions(-) create mode 100755 bin/task/context_spend_time.sh diff --git a/X11/show_timer.sh b/X11/show_timer.sh index 6ff405a..4fbb5e5 100755 --- a/X11/show_timer.sh +++ b/X11/show_timer.sh @@ -1,6 +1,7 @@ #! /bin/sh -export origin=`xdotool getactivewindow`; \ +origin="$(xdotool getactivewindow)" +export origin # If timer is set, focus to it xdotool search --name "TIMER" windowactivate && exit 0 @@ -14,4 +15,7 @@ alacritty --title TIMER --hold \ -o "window.position.y=0" \ -o "window.opacity=0.6" \ -o "font.size=40" \ - -e sh -c '~/helper/bin/unix/timer.sh SIGINT "xdotool windowactivate $origin"' + -e "$HOME"/helper/bin/unix/timer.sh \ + SIGINT \ + "xdotool windowactivate $origin" \ + 'echo $count | ~/helper/bin/task/context_spend_time.sh' diff --git a/bin/task/context_spend_time.sh b/bin/task/context_spend_time.sh new file mode 100755 index 0000000..cbadc47 --- /dev/null +++ b/bin/task/context_spend_time.sh @@ -0,0 +1,31 @@ +#! /bin/bash + +LOG_FILE=~/log/context +context="$(cat ~/.task/context)" +count="$1" + +if [ -z "$context" ] || [ "$context" = none ]; then + exit 1 +fi + +if [ -n "$1" ]; then + # Update Log file + while read -r ctx sec; do + if [ "$ctx" = "$context" ]; then + summary=$(( "$sec" + "$count" )) + update=true + break + fi + done <$LOG_FILE + if [ "$update" = true ]; then + sed -i -E "s/^$context.*/$context\t$summary" $LOG_FILE + else + echo -e "$context\t$summary" >>$LOG_FILE + fi +else + # Print times for each context + while read -r ctx sec; do + echo -ne "$ctx\t" + date -u -d @"$sec" +%H:%M:%S + done <$LOG_FILE +fi diff --git a/bin/unix/timer.sh b/bin/unix/timer.sh index 152d828..b4f48c5 100755 --- a/bin/unix/timer.sh +++ b/bin/unix/timer.sh @@ -5,7 +5,8 @@ exec 3>&1 exec 1>/dev/tty SIGNAL=${1:-SIGTERM} -COMMAND="$2" +COMMAND_START="$2" +COMMAND_EXIT="$3" # If SIGNAL is received, switch to next display trap 'next_display' "$SIGNAL" @@ -34,8 +35,8 @@ toggle_timer() { read -p '? ' -r input # Disable input on terminal stty -echo -# If COMMAND is given, run it after timer is set -result="$([ -n "$COMMAND" ] && eval "$COMMAND" 2>&1)" +# If COMMAND_START is given, run it after timer is set +result="$([ -n "$COMMAND_START" ] && eval "$COMMAND_START" 2>&1)" notify-send "$result" &>/tmp/openbox @@ -78,7 +79,7 @@ timer() { done } -trap 'exec 1>&3; echo $count; send-notify foo' EXIT KILL TERM STOP +trap 'exec 1>&3; [ -n "COMMAND_EXIT" ] && eval "$COMMAND_EXIT"' EXIT QUIT HUP while [ $count -lt $SET ]; do [ $stop = true ] && sleep 0.3 && continue -- cgit v1.2.3-70-g09d2