bf-start:
#!/bin/bash
[[ $(bf-status) == "start" ]] || echo "$(date -u +%Y-%m-%dT%H:%M:%S%Z);start" >> ~/.bf-worklog
bf-stop:
#!/bin/bash
[[ $(bf-status) == "stop" ]] || echo "$(date -u +%Y-%m-%dT%H:%M:%S%Z);stop" >> ~/.bf-worklog
bf-toggle:
#!/bin/bash
case "$(bf-status)" in
"stop")
bf-start
;;
"start")
bf-stop
;;
*)
;;
esac
bf-polybar:
#!/bin/bash
case "$(bf-status)" in
"stop")
echo "▱ $(bf-active-today)"
;;
"start")
echo "▰ $(bf-active-today)"
;;
*)
;;
esac
bf-active-today:
#!/bin/bash
TODAY=${1:-$(date -u +%Y-%m-%d)}
LAST_STATE="stop"
LAST_EPOCH=0
TOTAL_EPOCH=0
while read line
do
if [[ "$line" =~ ^"$TODAY" ]]
then
# Make sure we're alternating between different states and first line is a start
STATE=$(echo "$line" | cut -d';' -f2)
if [[ "$LAST_STATE" == "$STATE" ]]
then
echo "Data inconsistent! Same state repeated."
echo "$line"
fi
LAST_STATE=$STATE
# extract the unix epoch timestamp for this data point
DATE=$(echo "$line" | cut -d';' -f1)
EPOCH=$(date --date="$DATE" +%s)
if [[ "$STATE" == "stop" ]]
then
# if we're ending an interval, calculate the interval length and sum it up
INTERVAL_EPOCH=$(( $EPOCH - $LAST_EPOCH ))
TOTAL_EPOCH=$(($TOTAL_EPOCH + $INTERVAL_EPOCH ))
else
# if we're not ending an interval, remember the start epoch timestamp
LAST_EPOCH=$EPOCH
fi
fi
done < ~/.bf-worklog
if [[ "$LAST_STATE" == "start" ]]
then
# we're in an active interval still, so let's just assume that we finish right now to include the interval up until now.
EPOCH=$(date +%s)
INTERVAL_EPOCH=$(( $EPOCH - $LAST_EPOCH ))
TOTAL_EPOCH=$(($TOTAL_EPOCH + $INTERVAL_EPOCH ))
fi
echo "$(echo "scale=1; $TOTAL_EPOCH / ( 60 * 60)" | bc)"