From 03e617b64a446e6d63f6e92126766874f21b5352 Mon Sep 17 00:00:00 2001 From: lonkaars Date: Tue, 12 Mar 2024 10:53:38 +0100 Subject: use file instead of folder to store state --- core/pause | 4 ++-- core/reset | 4 +++- core/start | 4 ++-- core/state | 2 +- core/toggle | 2 +- core/update | 70 +++++++++++++++++++++++++++++-------------------------------- 6 files changed, 42 insertions(+), 44 deletions(-) diff --git a/core/pause b/core/pause index 3037c1c..abba7ca 100755 --- a/core/pause +++ b/core/pause @@ -4,10 +4,10 @@ . "$core_path/lib" . "$core_path/update" -[ "$state" != 'running' ] && err "timer is not running" +[ $running -eq 0 ] && err "timer is not running" time="$(echo "$time - $now" | bc)" -state='paused' +running=0 save_state diff --git a/core/reset b/core/reset index a4c245e..b3342c2 100755 --- a/core/reset +++ b/core/reset @@ -2,9 +2,11 @@ [ "$1" = "info" ] && echo "reset the timer and lap counter" && exit 2 lap=0 -state='reset' +running=0 update_time=1 +original_state='foo' # prevent loading of state on disk + . "$core_path/update" save_state diff --git a/core/start b/core/start index dbbabc0..65e9c1f 100755 --- a/core/start +++ b/core/start @@ -26,7 +26,7 @@ while [ $# -gt 0 ] ; do esac done -if [ "$state" = 'running' ] ; then +if [ $running -eq 1 ] ; then [ $allow_skip -eq 0 ] && err "timer is already running, use -s to skip lap" lap=$(( $lap + 1 )) update_time=1 @@ -34,7 +34,7 @@ if [ "$state" = 'running' ] ; then fi time="$(echo "$now + $time" | bc)" -state='running' +running=1 save_state diff --git a/core/state b/core/state index 580afc6..6aad04b 100755 --- a/core/state +++ b/core/state @@ -17,5 +17,5 @@ EOF } . "$core_path/update" -echo "lap $lap, $state, $(fmt_time $remaining)" +echo "lap $lap, $([ $running -eq 1 ] && echo "running" || echo "paused"), $(fmt_time $remaining)" diff --git a/core/toggle b/core/toggle index bfb96d2..54dcc8d 100755 --- a/core/toggle +++ b/core/toggle @@ -3,5 +3,5 @@ . "$core_path/update" -[ "$state" = 'running' ] && exec "$prog" pause || exec "$prog" start +[ $running -eq 1 ] && exec "$prog" pause || exec "$prog" start diff --git a/core/update b/core/update index 8787754..686def2 100644 --- a/core/update +++ b/core/update @@ -2,25 +2,25 @@ [ "$skip_libraries" ] && return export now="$(date +%s.%N)" -# load current state -mkdir -p "$POMODORO_STATE_PATH" -load_or_init() { - property="$1" - default_value="$2" - property_path="$POMODORO_STATE_PATH/current/$property" - if [ -f "$property_path" ] ; then - eval $property='"$(cat "$property_path")"' - else - eval $property='$default_value' +# export state +state() { echo "$lap $running $time" ; } + +# load original_state once per invocation of dppt +if [ -z "$original_state" ] ; then + # import state + mkdir -p "$POMODORO_STATE_PATH" + if [ -e "$POMODORO_STATE_PATH/current" ] ; then + read -r lap running time < "$POMODORO_STATE_PATH/current" fi -} -[ -z "$lap" ] && load_or_init lap 0 -[ -z "$state" ] && load_or_init state 'reset' -[ -z "$time" ] && load_or_init time 0.0 -# save state (for diff) -diff_state() { printf '%s:' "$lap" "$state" "$time" ; } -original_state="$(diff_state)" + # ensure state variables are always initialized + lap=${lap:-0} + running=${running:-0} + time=${time:-0} + + # save original state (see save_state function) + original_state="$(state)" +fi # allow overriding this value manually [ -z "$update_time" ] && update_time=0 @@ -28,19 +28,18 @@ original_state="$(diff_state)" # calculate remaining time on timer if [ -z "$remaining" ] ; then remaining="$time" - [ "$state" = 'running' ] && remaining="$(echo "( $time - $now ) / 1" | bc)" + [ $running -eq 1 ] && remaining="$(echo "( $time - $now ) / 1" | bc)" fi # go to next lap if this timer expired -if [ "$state" = 'running' ] && [ "${remaining#-}" != "${remaining}" ] ; then +if [ $running -eq 1 ] && [ "${remaining#-}" != "${remaining}" ] ; then lap=$(( $lap + 1 )) state='paused' update_time=1 fi # update remaining time if in reset -[ "$state" = 'reset' ] && update_time=1 -[ "$time" = "0.0" ] && update_time=1 +[ $lap -eq 0 ] && [ $time = '0' ] && update_time=1 if [ $update_time -eq 1 ] ; then # skip_libraries is used to prevent endless loop (see top of this file) @@ -49,8 +48,9 @@ if [ $update_time -eq 1 ] ; then fi save_state() { + new_state="$(state)" # do not save files if state didn't change - [ "$original_state" = "$(diff_state)" ] && return + [ "$original_state" = "$new_state" ] && return # The state update needs to be atomic since other instances may access the # state during an update. This is accomplished by replacing a single symlink, @@ -58,34 +58,30 @@ save_state() { # complicated than a lockfile. $POMODORO_STATE_PATH contains three items: # # 1. current -- a symlink to `primary` or `secondary` - # 2. primary -- folder containing current state* - # 3. secondary -- temporary folder used write new state + # 2. primary -- file containing current state* + # 3. secondary -- temporary file used write new state # # * `primary` will only contain stale state during an update (i.e. while # `current` is pointing to `secondary`) - mkdir -p "$POMODORO_STATE_PATH/primary" "$POMODORO_STATE_PATH/secondary" - - echo "$lap" > "$POMODORO_STATE_PATH/secondary/lap" - echo "$state" > "$POMODORO_STATE_PATH/secondary/state" - echo "$time" > "$POMODORO_STATE_PATH/secondary/time" - - ln -sf "secondary" "$POMODORO_STATE_PATH/secondary/current" + # save state and create replacement symlink for `current` + echo "$new_state" > "$POMODORO_STATE_PATH/secondary" + ln -sf "secondary" "$POMODORO_STATE_PATH/current~" # this is the atomic update that replaces the `current` symlink with the new # state - mv "$POMODORO_STATE_PATH/secondary/current" "$POMODORO_STATE_PATH" + mv "$POMODORO_STATE_PATH/current~" "$POMODORO_STATE_PATH/current" - # next, replace the old `primary` state with the new state from `secondary`, + # next, replace the old `primary` state with the new state from `secondary` + cp "$POMODORO_STATE_PATH/secondary" "$POMODORO_STATE_PATH/primary" # and update the symlink a second time to now point to `primary` again (this # does not change the state) - cp -r "$POMODORO_STATE_PATH/secondary/." "$POMODORO_STATE_PATH/primary/" - ln -sf "primary" "$POMODORO_STATE_PATH/primary/current" - mv "$POMODORO_STATE_PATH/primary/current" "$POMODORO_STATE_PATH" + ln -sf "primary" "$POMODORO_STATE_PATH/current~" + mv "$POMODORO_STATE_PATH/current~" "$POMODORO_STATE_PATH/current" } # state variables -export lap state time +export lap running time # calculated variables export remaining -- cgit v1.2.3