@@ -31,10 +31,161 @@ set_script_path_options() {
3131 tmux set-option -gq " $restore_path_option " " $CURRENT_DIR /scripts/restore.sh"
3232}
3333
34+ install_attach_script () {
35+ local bin_dir=" $HOME /bin"
36+ local attach_script=" $bin_dir /attach"
37+
38+ # Only create if attach script doesn't already exist
39+ if [[ -f " $attach_script " ]]; then
40+ return 0
41+ fi
42+
43+ # Create $HOME/bin if it doesn't exist
44+ mkdir -p " $bin_dir "
45+
46+ # Create the attach script
47+ cat > " $attach_script " << ATTACH_SCRIPT
48+ #!/bin/bash
49+
50+ # Directory where tmux-resurrect saves sessions
51+ RESURRECT_DIR="\$ HOME/.tmux/resurrect"
52+
53+ # Path to restore script (set at install time)
54+ RESTORE_SCRIPT="$CURRENT_DIR /scripts/restore.sh"
55+
56+ # Check if the directory exists
57+ if [[ ! -d "\$ RESURRECT_DIR" ]]; then
58+ echo "Error: Tmux-resurrect directory not found at \$ RESURRECT_DIR"
59+ exit 1
60+ fi
61+
62+ # List saved sessions
63+ echo "Available saved tmux sessions:"
64+ SAVED_SESSIONS=(\$ (find "\$ RESURRECT_DIR" -maxdepth 1 -type f ! -name "last" ! -name "readme*" -printf "%f\n" | sort))
65+ if [[ \$ {#SAVED_SESSIONS[@]} -eq 0 ]]; then
66+ echo "No saved sessions found."
67+ exit 0
68+ fi
69+
70+ # Display session names
71+ for i in "\$ {!SAVED_SESSIONS[@]}"; do
72+ echo "\$ i) \$ {SAVED_SESSIONS[\$ i]}"
73+ done
74+
75+ echo ""
76+ echo "Options: [number] to restore, [d/D] to delete, [r/R] to rename"
77+ read -p "Enter your choice: " CHOICE
78+
79+ # Handle delete option
80+ if [[ "\$ CHOICE" =~ ^[dD]\$ ]]; then
81+ read -p "Enter the number of the session to delete: " DEL_CHOICE
82+ if [[ ! "\$ DEL_CHOICE" =~ ^[0-9]+\$ ]] || [[ "\$ DEL_CHOICE" -ge "\$ {#SAVED_SESSIONS[@]}" ]]; then
83+ echo "Invalid selection. Exiting."
84+ exit 1
85+ fi
86+ DEL_SESSION="\$ {SAVED_SESSIONS[\$ DEL_CHOICE]}"
87+ read -p "Are you sure you want to delete '\$ DEL_SESSION'? [y/N]: " CONFIRM
88+ if [[ "\$ CONFIRM" =~ ^[yY]\$ ]]; then
89+ rm "\$ RESURRECT_DIR/\$ DEL_SESSION"
90+ echo "Deleted: \$ DEL_SESSION"
91+ else
92+ echo "Deletion cancelled."
93+ fi
94+ exit 0
95+ fi
96+
97+ # Handle rename option
98+ if [[ "\$ CHOICE" =~ ^[rR]\$ ]]; then
99+ read -p "Enter the number of the session to rename: " REN_CHOICE
100+ if [[ ! "\$ REN_CHOICE" =~ ^[0-9]+\$ ]] || [[ "\$ REN_CHOICE" -ge "\$ {#SAVED_SESSIONS[@]}" ]]; then
101+ echo "Invalid selection. Exiting."
102+ exit 1
103+ fi
104+ OLD_SESSION="\$ {SAVED_SESSIONS[\$ REN_CHOICE]}"
105+ read -p "Enter new name (without .txt extension): " NEW_NAME
106+ if [[ -z "\$ NEW_NAME" ]]; then
107+ echo "Invalid name. Exiting."
108+ exit 1
109+ fi
110+ NEW_SESSION="\$ {NEW_NAME}.txt"
111+ if [[ -f "\$ RESURRECT_DIR/\$ NEW_SESSION" ]]; then
112+ echo "Error: A session with that name already exists."
113+ exit 1
114+ fi
115+ mv "\$ RESURRECT_DIR/\$ OLD_SESSION" "\$ RESURRECT_DIR/\$ NEW_SESSION"
116+ echo "Renamed: \$ OLD_SESSION -> \$ NEW_SESSION"
117+ exit 0
118+ fi
119+
120+ # Handle restore (number input)
121+ if [[ ! "\$ CHOICE" =~ ^[0-9]+\$ ]] || [[ "\$ CHOICE" -ge "\$ {#SAVED_SESSIONS[@]}" ]]; then
122+ echo "Invalid selection. Exiting."
123+ exit 1
124+ fi
125+
126+ # Restore the selected session
127+ SELECTED_SESSION="\$ {SAVED_SESSIONS[\$ CHOICE]}"
128+
129+ # Extract the desired session name (remove .txt extension)
130+ DESIRED_NAME="\$ {SELECTED_SESSION%.txt}"
131+
132+ # Check if 'last' is a symlink and remove it if necessary
133+ if [[ -L "\$ RESURRECT_DIR/last" ]]; then
134+ rm "\$ RESURRECT_DIR/last"
135+ fi
136+
137+ # Copy the selected session to 'last'
138+ cp "\$ RESURRECT_DIR/\$ SELECTED_SESSION" "\$ RESURRECT_DIR/last"
139+
140+ # Ensure the tmux server is running by creating a detached session
141+ temp_session="resurrect-temp-\$\$ -\$ RANDOM"
142+ created_temp_session=false
143+ if tmux new-session -d -s "\$ temp_session" 2>/dev/null; then
144+ created_temp_session=true
145+ fi
146+
147+ # Restore the session using tmux-resurrect
148+ tmux run-shell "\$ RESTORE_SCRIPT"
149+
150+ # Wait for restoration to complete
151+ sleep 2
152+
153+ # Kill the temp session only if we created it
154+ if [[ "\$ created_temp_session" == true ]]; then
155+ tmux kill-session -t "\$ temp_session" 2>/dev/null
156+ fi
157+
158+ # Get all sessions
159+ SESSIONS=\$ (tmux list-sessions -F '#{session_name}' 2>/dev/null)
160+
161+ if [[ -z "\$ SESSIONS" ]]; then
162+ echo "Error: No sessions found after restoration."
163+ exit 1
164+ fi
165+
166+ # Get the first session (the restored one)
167+ FIRST_SESSION=\$ (echo "\$ SESSIONS" | head -n 1)
168+
169+ # Rename the session to match the filename
170+ tmux rename-session -t "\$ FIRST_SESSION" "\$ DESIRED_NAME"
171+
172+ echo "Available sessions after restoration:"
173+ tmux list-sessions
174+
175+ echo ""
176+ echo "Attaching to session: \$ DESIRED_NAME"
177+ tmux attach-session -t "\$ DESIRED_NAME"
178+ ATTACH_SCRIPT
179+
180+ # Make it executable
181+ chmod +x " $attach_script "
182+ }
183+
34184main () {
35185 set_save_bindings
36186 set_restore_bindings
37187 set_default_strategies
38188 set_script_path_options
189+ install_attach_script
39190}
40191main
0 commit comments