Improve tmux-continuum status plugin

This commit is contained in:
Aaron Kollasch 2022-10-28 06:34:42 -04:00
parent 5af1faa367
commit 14300a6e75
No known key found for this signature in database
GPG key ID: F813CAE853E39883
2 changed files with 98 additions and 49 deletions

View file

@ -322,20 +322,17 @@ set -g @dracula-clients-plural clients
#### continuum options #### continuum options
Hide output if no save has been performed recently Set the output mode. Options are:
(otherwise, show the continuum save interval) - **countdown**: Show a T- countdown to the next save (default)
- **time**: Show the time since the last save
- **alert**: Hide output if no save has been performed recently
- **interval**: Show the continuum save interval
```bash ```bash
set -g @dracula-continuum-mode alert set -g @dracula-continuum-mode countdown
``` ```
Show the time since the last continuum save Show if the last save was performed less than 60 seconds ago (default threshold is 15 seconds)
```bash
set -g @dracula-continuum-mode time
```
In alert mode, show if a the last save was performed less then one minute ago (default is 15 seconds)
```bash ```bash
set -g @dracula-continuum-time-threshold 60 set -g @dracula-continuum-time-threshold 60

View file

@ -1,15 +1,28 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# setting the locale, some users have issues with different locales, this forces the correct one # setting the locale, some users have issues with different locales, this forces the correct one
export LC_ALL=en_US.UTF-8 export LC_ALL=en_US.UTF-8
if command -v gdate &>/dev/null; then
DATE=gdate
else
DATE=date
fi
# configuration # configuration
# @dracula-continuum-mode default (default|alert|time) # @dracula-continuum-mode default (countdown|time|alert|interval)
# @dracula-continuum-time-threshold 15 # @dracula-continuum-time-threshold 15
alert_mode="@dracula-continuum-mode" alert_mode="@dracula-continuum-mode"
time_threshold="@dracula-continuum-time-threshold" time_threshold="@dracula-continuum-time-threshold"
warn_threshold=15 warn_threshold=360
first_save="@dracula-continuum-first-save"
# tmux-resurrect and tmux-continuum options
if [ -d "$HOME/.tmux/resurrect" ]; then
default_resurrect_dir="$HOME/.tmux/resurrect"
else
default_resurrect_dir="${XDG_DATA_HOME:-$HOME/.local/share}"/tmux/resurrect
fi
resurrect_dir_option="@resurrect-dir"
last_auto_save_option="@continuum-save-last-timestamp" last_auto_save_option="@continuum-save-last-timestamp"
auto_save_interval_option="@continuum-save-interval" auto_save_interval_option="@continuum-save-interval"
auto_save_interval_default="15" auto_save_interval_default="15"
@ -21,55 +34,94 @@ current_timestamp() {
echo "$(date +%s)" echo "$(date +%s)"
} }
time_since_last_run_passed() { set_tmux_option() {
local last_saved_timestamp="$(get_tmux_option "$last_auto_save_option" "0")" local option="$1"
printf "%s" "$(($(current_timestamp) - last_saved_timestamp))" local value="$2"
tmux set-option -gq "$option" "$value"
}
# tmux-resurrect dir
resurrect_dir() {
if [ -z "$_RESURRECT_DIR" ]; then
local path="$(get_tmux_option "$resurrect_dir_option" "$default_resurrect_dir")"
# expands tilde, $HOME and $HOSTNAME if used in @resurrect-dir
echo "$path" | sed "s,\$HOME,$HOME,g; s,\$HOSTNAME,$(hostname),g; s,\~,$HOME,g"
else
echo "$_RESURRECT_DIR"
fi
}
_RESURRECT_DIR="$(resurrect_dir)"
last_resurrect_file() {
echo "$(resurrect_dir)/last"
}
last_saved_timestamp() {
local last_saved_timestamp="$(get_tmux_option "$last_auto_save_option" "")"
local first_save_timestamp="$(get_tmux_option "$first_save" "")"
# continuum sets the last save timestamp to the current time on first load if auto_save_option is not set
# so we can outrace it and detect that last_uato_save_option is empty and the timestamp is a dummy save
if [ -z "$first_save_timestamp" ]; then
last_saved_timestamp="$($DATE -r "$(last_resurrect_file)" +%s)" || last_saved_timestamp=0
set_tmux_option "$first_save" "$last_saved_timestamp"
elif [ "$first_save_timestamp" != "done" ]; then
last_saved_timestamp="$($DATE -r "$(last_resurrect_file)" +%s)" || last_saved_timestamp=0
if [ "$last_saved_timestamp" -gt "$first_save_timestamp" ]; then
set_tmux_option "$first_save" "done"
else
last_saved_timestamp="$first_save_timestamp"
fi
fi
echo "$last_saved_timestamp"
} }
print_status() { print_status() {
local mode="$(get_tmux_option "$alert_mode" "default")" local mode="$(get_tmux_option "$alert_mode" "countdown")"
local threshold="$(get_tmux_option "$time_threshold" "15")" local info_threshold="$(get_tmux_option "$time_threshold" "15")"
local save_int="$(get_tmux_option "$auto_save_interval_option" "$auto_save_interval_default")" local save_int="$(get_tmux_option "$auto_save_interval_option" "$auto_save_interval_default")"
local interval_seconds="$((save_int * 60))" local interval_seconds="$((save_int * 60))"
local status="" local status=""
local time_delta="$(time_since_last_run_passed)" local last_timestamp="$(last_saved_timestamp)"
local time_delta="$(($(current_timestamp) - last_timestamp))"
local time_delta_minutes="$((time_delta / 60))" local time_delta_minutes="$((time_delta / 60))"
case "$mode" in if [[ $save_int -gt 0 ]]; then
time) if [[ "$time_delta" -gt $((interval_seconds + warn_threshold)) ]]; then
if [[ $save_int -gt 0 ]]; then if [[ "$mode" == "countdown" ]]; then
if [[ "$time_delta" -gt $((interval_seconds+warn_threshold)) ]]; then # continuum timestamp may be different than file timestamp on first load
status="no save after $time_delta_minutes minutes!" local last_continuum_timestamp="$(get_tmux_option "$last_auto_save_option" "")"
else time_delta="$(($(current_timestamp) - last_continuum_timestamp))"
status="$time_delta_minutes" time_delta_minutes="$((time_delta / 60))"
fi
else
status="off"
fi
;;
alert) status="last save: $($DATE -d "@$last_timestamp" '+%F %T'); next: T$(printf '%+d' "$((time_delta_minutes - save_int)) min")"
if [[ "$time_delta" -gt $((interval_seconds+warn_threshold)) ]]; then
status="no save after $time_delta_minutes minutes!"
elif [[ "$time_delta" -le "$threshold" ]]; then
status="saved"
elif [[ $save_int -gt 0 ]]; then
status=""
else else
status="off" status="last save: $($DATE -d "@$last_timestamp" '+%F %T')"
fi fi
;; elif [[ "$time_delta" -le "$info_threshold" ]]; then
status="saved"
else
case "$mode" in
countdown)
status="T$(printf '%+d' "$((time_delta_minutes - save_int))")min";
;;
time)
status="$time_delta_minutes";
;;
alert)
status=""
;;
interval)
status="$save_int"
;;
esac
fi
else
status="off"
fi
*)
if [[ "$time_delta" -le "$threshold" ]]; then
status="saved"
elif [[ $save_int -gt 0 ]]; then
status="$save_int"
else
status="off"
fi
;;
esac
echo "$status" echo "$status"
} }
print_status print_status