all repos

tmux-stare @ fdac49f

session manager, but my session manager
1 files changed, 92 insertions(+), 0 deletions(-)
restore
Author: Oleksandr Smirnov olexsmir@gmail.com
Committed at: 2026-02-09 02:08:41 +0200
Authored at: 2026-02-08 19:15:37 +0200
Change ID: tuxqoqvurxmlsmsnolnypylopklksyxz
Parent: f520da9
A scripts/restore.sh
ยทยทยท
        
        1
        +#!/usr/bin/env bash

      
        
        2
        +CURRENT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

      
        
        3
        +source "$CURRENT_DIR/helpers.sh"

      
        
        4
        +

      
        
        5
        +# TODO: set the separator globally, or do whole sessions in one module

      
        
        6
        +declare S=$'\t'

      
        
        7
        +

      
        
        8
        +restore_session_from_file() {

      
        
        9
        +  local session_file="$1"

      
        
        10
        +  local session_name=$(basename "$session_file" | sed 's/_last$//')

      
        
        11
        +  exec <"$session_file"

      
        
        12
        +

      
        
        13
        +  start_spinner "Restoring session $session_name"

      
        
        14
        +

      
        
        15
        +  local session_path="$(head -n1 | cut -d"$SEPARATOR" -f2)"

      
        
        16
        +  tmux new-session -ds "$session_name" -c "$session_path"

      
        
        17
        +

      
        
        18
        +  declare -A window_layouts

      
        
        19
        +  declare active_window

      
        
        20
        +  while read -r line; do

      
        
        21
        +    case $line in

      
        
        22
        +    window*)

      
        
        23
        +      IFS=$S read -r _ window_index window_name window_layout window_active <<<"$line"

      
        
        24
        +      window_id="$session_name:$window_index"

      
        
        25
        +      tmux new-window -k -t "$window_id" -n "$window_name"

      
        
        26
        +      window_layouts["$window_id"]="$window_layout"

      
        
        27
        +      if [[ "$window_active" == "1" ]]; then

      
        
        28
        +        active_window="$window_id"

      
        
        29
        +      fi

      
        
        30
        +      ;;

      
        
        31
        +

      
        
        32
        +    pane*)

      
        
        33
        +      IFS=$S read -r _ pane_index pane_current_path pane_active window_index command <<<"$line"

      
        
        34
        +      if [[ "$pane_index" == "$(get_tmux_option base-index 0)" ]]; then

      
        
        35
        +        tmux send-keys -t "$session_name:$window_index" "cd \"$pane_current_path\"" Enter "clear" Enter

      
        
        36
        +      else

      
        
        37
        +        tmux split-window -d -t "$session_name:$window_index" -c "$pane_current_path"

      
        
        38
        +      fi

      
        
        39
        +      if [[ "$pane_active" == "1" ]]; then

      
        
        40
        +        tmux select-pane -t "$session_name:$window_index.$pane_index"

      
        
        41
        +      fi

      
        
        42
        +      if [[ -n "$command" ]]; then

      
        
        43
        +        tmux send-keys -t "$session_name:$window_index.$pane_index" "$command" Enter

      
        
        44
        +      fi

      
        
        45
        +      ;;

      
        
        46
        +    esac

      
        
        47
        +  done

      
        
        48
        +

      
        
        49
        +  for window in "${!window_layouts[@]}"; do

      
        
        50
        +    tmux select-layout -t "$window" "${window_layouts[$window]}"

      
        
        51
        +  done

      
        
        52
        +

      
        
        53
        +  tmux select-window -t "$active_window"

      
        
        54
        +  tmux switch-client -t "$session_name"

      
        
        55
        +  stop_spinner "Session restored"

      
        
        56
        +}

      
        
        57
        +

      
        
        58
        +restore_session() {

      
        
        59
        +  local name="$1"

      
        
        60
        +  if tmux has-session -t "$name" 2>/dev/null; then

      
        
        61
        +    tmux switch-client -t "$name"

      
        
        62
        +    return 0

      
        
        63
        +  fi

      
        
        64
        +

      
        
        65
        +  local session_file="$(get_opt_dir)/${name}_last"

      
        
        66
        +  if [[ ! -f "$session_file" ]]; then

      
        
        67
        +    tmux display-message "No saved session found for: $name"

      
        
        68
        +    return 1

      
        
        69
        +  fi

      
        
        70
        +

      
        
        71
        +  restore_session_from_file "$session_file"

      
        
        72
        +}

      
        
        73
        +

      
        
        74
        +restore_last() {

      
        
        75
        +  local last_file="$(get_opt_dir)/last"

      
        
        76
        +  if [[ ! -e "$last_file" ]]; then

      
        
        77
        +    tmux display-message "No last session saved"

      
        
        78
        +    return 1

      
        
        79
        +  fi

      
        
        80
        +

      
        
        81
        +  local session_name=$(basename "$(readlink "$last_file")" | sed 's/_last$//')

      
        
        82
        +  restore_session "$session_name"

      
        
        83
        +}

      
        
        84
        +

      
        
        85
        +main() {

      
        
        86
        +  if [[ -n "$1" ]]; then

      
        
        87
        +    restore_session "$1"

      
        
        88
        +  else

      
        
        89
        +    restore_last

      
        
        90
        +  fi

      
        
        91
        +}

      
        
        92
        +main "$@"