From e6c1cacb60010ec75e02cf72acf34278417670fd Mon Sep 17 00:00:00 2001 From: lonkaars Date: Mon, 11 Mar 2024 18:33:02 +0100 Subject: initial commit (basic functionality) --- core/update | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 core/update (limited to 'core/update') diff --git a/core/update b/core/update new file mode 100644 index 0000000..04fd29c --- /dev/null +++ b/core/update @@ -0,0 +1,55 @@ +#!/bin/sh +export now="$(date +%s.%N)" + +update_time=0 + +mkdir -p "$POMODORO_STATE_PATH" +load_or_init() { + property="$1" + default_value="$2" + property_path="$POMODORO_STATE_PATH/$property" + if [ -f "$property_path" ] ; then + eval $property='"$(cat "$property_path")"' + else + eval $property='$default_value' + fi +} +[ -z "$lap" ] && load_or_init lap 0 +[ -z "$state" ] && load_or_init state 'reset' +[ -z "$time" ] && load_or_init time 0.0 +[ "$time" = "0.0" ] && update_time=1 + +# calculate remaining time on timer +if [ -z "$remaining" ] ; then + remaining="$time" + [ "$state" = 'running' ] && remaining="$(echo "( $time - $now ) / 1" | bc)" +fi + +# go to next lap if this timer expired +if [ "$state" = 'running' ] && [ "${remaining#-}" != "${remaining}" ] ; then + lap=$(( $lap + 1 )) + state='paused' + update_time=1 +fi + +# update remaining time if in reset +[ "$state" = 'reset' ] && update_time=1 + +if [ $update_time -eq 1 ] ; then + time="$POMODORO_NORMAL_DURATION" + [ $(( $lap % $POMODORO_BREAK_SHORT_INTERVAL )) -eq $(( $POMODORO_BREAK_SHORT_INTERVAL - 1 )) ] && time=$POMODORO_BREAK_SHORT_DURATION + [ $(( $lap % $POMODORO_BREAK_LONG_INTERVAL )) -eq $(( $POMODORO_BREAK_LONG_INTERVAL - 1 )) ] && time=$POMODORO_BREAK_LONG_DURATION + remaining="$time" +fi + +# save state +save_state() { + mkdir -p "$POMODORO_STATE_PATH" + echo "$lap" > "$POMODORO_STATE_PATH/lap" + echo "$state" > "$POMODORO_STATE_PATH/state" + echo "$time" > "$POMODORO_STATE_PATH/time" +} + +# export state variables +export lap state time + -- cgit v1.2.3