get_time() { date +"%Y%m%dT%H%M%S"; }
get_tmux_option() {
  local option="$1"
  local default_value="$2"
  local option_value=$(tmux show-option -gqv "$option")
  if [ -z "$option_value" ]; then
    echo "$default_value"
  else
    echo "$option_value"
  fi
}

set_tmux_option() {
  local option="$1"
  local value="$2"
  tmux set-option -gq "$option" "$value"
}

wait_for_client() {
  local i=0
  while [ "$i" -lt 100 ]; do
    if tmux list-clients >/dev/null 2>&1; then break; fi
    sleep 0.1
    i=$((i + 1))
  done
}

# === options
get_opt_interval() { get_tmux_option "@stare-interval" "15"; }
get_opt_start() { get_tmux_option "@stare-start" ""; }
get_opt_last() { get_tmux_option "@stare-last" "0"; }
set_opt_last() { set_tmux_option "@stare-last" "$1"; }
get_opt_save() { get_tmux_option "@stare-save" "C-s"; }
get_opt_pick() { get_tmux_option "@stare-pick" ""; }
get_opt_processes() { get_tmux_option "@stare-processes" "vi vim nvim man less more tial htop btop claude opencode copilot pi"; }
get_opt_dir() {
  local dir="$(get_tmux_option "@stare-dir" "${HOME}/.local/share/tmux/stare" | sed "s,\$HOME,$HOME,g; s,\~,$HOME,g")"
  mkdir -p "$dir"
  echo "$dir"
}

# === spiner
# TODO: use one of those briael fonts
new_spinner() {
  local current=0
  local -r chars="/-\|"
  while true; do
    tmux display-message -- "${chars:$current:1} $1"
    current=$(((current + 1) % 4))
    sleep 0.1
  done
}

start_spinner() {
  new_spinner "$1" &
  export SPINNER_PID=$!
}

stop_spinner() {
  kill "$SPINNER_PID"
  tmux display-message " $1"
}
