continuum.sh
1 #!/usr/bin/env bash 2 # setting the locale, some users have issues with different locales, this forces the correct one 3 export LC_ALL=en_US.UTF-8 4 5 # configuration 6 # @dracula-continuum-mode default (countdown|time|alert|interval) 7 # @dracula-continuum-time-threshold 15 8 9 alert_mode="@dracula-continuum-mode" 10 time_threshold="@dracula-continuum-time-threshold" 11 warn_threshold=360 12 first_save="@dracula-continuum-first-save" 13 14 # tmux-resurrect and tmux-continuum options 15 if [ -d "$HOME/.tmux/resurrect" ]; then 16 default_resurrect_dir="$HOME/.tmux/resurrect" 17 else 18 default_resurrect_dir="${XDG_DATA_HOME:-$HOME/.local/share}"/tmux/resurrect 19 fi 20 resurrect_dir_option="@resurrect-dir" 21 last_auto_save_option="@continuum-save-last-timestamp" 22 auto_save_interval_option="@continuum-save-interval" 23 auto_save_interval_default="15" 24 25 current_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" 26 source $current_dir/utils.sh 27 28 current_timestamp() { 29 echo "$(date +%s)" 30 } 31 32 file_mtime() { 33 if [ ! -f "$1" ]; then 34 echo -1 35 return 36 fi 37 case $(uname -s) in 38 Linux|Darwin) 39 date -r "$1" +%s 40 ;; 41 42 FreeBSD) 43 stat -f %m "$1" 44 ;; 45 46 CYGWIN*|MINGW32*|MSYS*|MINGW*) 47 # TODO - windows compatability 48 ;; 49 esac 50 } 51 52 timestamp_date() { 53 case $(uname -s) in 54 Linux) 55 date -d "@$1" "$2" 56 ;; 57 58 Darwin|FreeBSD) 59 date -r "$1" "$2" 60 ;; 61 62 CYGWIN*|MINGW32*|MSYS*|MINGW*) 63 # TODO - windows compatability 64 ;; 65 esac 66 } 67 68 set_tmux_option() { 69 local option="$1" 70 local value="$2" 71 tmux set-option -gq "$option" "$value" 72 } 73 74 # tmux-resurrect dir 75 resurrect_dir() { 76 if [ -z "$_RESURRECT_DIR" ]; then 77 local path="$(get_tmux_option "$resurrect_dir_option" "$default_resurrect_dir")" 78 # expands tilde, $HOME and $HOSTNAME if used in @resurrect-dir 79 echo "$path" | sed "s,\$HOME,$HOME,g; s,\$HOSTNAME,$(hostname),g; s,\~,$HOME,g" 80 else 81 echo "$_RESURRECT_DIR" 82 fi 83 } 84 _RESURRECT_DIR="$(resurrect_dir)" 85 86 last_resurrect_file() { 87 echo "$(resurrect_dir)/last" 88 } 89 90 last_saved_timestamp() { 91 local last_saved_timestamp="$(get_tmux_option "$last_auto_save_option" "")" 92 local first_save_timestamp="$(get_tmux_option "$first_save" "")" 93 # continuum sets the last save timestamp to the current time on first load if auto_save_option is not set 94 # so we can outrace it and detect that last_uato_save_option is empty and the timestamp is a dummy save 95 if [ -z "$first_save_timestamp" ]; then 96 last_saved_timestamp="$(file_mtime "$(last_resurrect_file)")" || last_saved_timestamp=-1 97 set_tmux_option "$first_save" "$last_saved_timestamp" 98 elif [ "$first_save_timestamp" != "done" ]; then 99 last_saved_timestamp="$(file_mtime "$(last_resurrect_file)")" || last_saved_timestamp=-1 100 if [ "$last_saved_timestamp" -gt "$first_save_timestamp" ]; then 101 set_tmux_option "$first_save" "done" 102 else 103 last_saved_timestamp="$first_save_timestamp" 104 fi 105 fi 106 echo "$last_saved_timestamp" 107 } 108 109 print_status() { 110 local mode="$(get_tmux_option "$alert_mode" "countdown")" 111 local info_threshold="$(get_tmux_option "$time_threshold" "15")" 112 local save_int="$(get_tmux_option "$auto_save_interval_option" "$auto_save_interval_default")" 113 local interval_seconds="$((save_int * 60))" 114 local status="" 115 local last_timestamp="$(last_saved_timestamp)" 116 local time_delta="$(($(current_timestamp) - last_timestamp))" 117 local time_delta_minutes="$((time_delta / 60))" 118 119 if [[ $save_int -gt 0 ]]; then 120 if [[ "$time_delta" -gt $((interval_seconds + warn_threshold)) ]]; then 121 if [[ "$last_timestamp" == -1 ]]; then 122 status="no save" 123 else 124 status="last save: $(timestamp_date "$last_timestamp" '+%F %T')" 125 fi 126 if [[ "$mode" == "countdown" ]]; then 127 # continuum timestamp may be different than file timestamp on first load 128 local last_continuum_timestamp="$(get_tmux_option "$last_auto_save_option" "")" 129 time_delta="$(($(current_timestamp) - last_continuum_timestamp))" 130 time_delta_minutes="$((time_delta / 60))" 131 132 status="$status; T$(printf '%+d' "$((time_delta_minutes - save_int))")min" 133 fi 134 elif [[ "$time_delta" -le "$info_threshold" ]]; then 135 status="saved" 136 else 137 case "$mode" in 138 countdown) 139 status="T$(printf '%+d' "$((time_delta_minutes - save_int))")min"; 140 ;; 141 142 time) 143 status="$time_delta_minutes"; 144 ;; 145 146 alert) 147 status="" 148 ;; 149 150 interval) 151 status="$save_int" 152 ;; 153 esac 154 fi 155 else 156 status="off" 157 fi 158 159 echo "$status" 160 } 161 print_status