save.sh 9.76 KB
Newer Older
Bruno Sutic's avatar
Bruno Sutic committed
1
2
3
4
#!/usr/bin/env bash

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

5
source "$CURRENT_DIR/variables.sh"
Bruno Sutic's avatar
Bruno Sutic committed
6
source "$CURRENT_DIR/helpers.sh"
7
source "$CURRENT_DIR/spinner_helpers.sh"
Bruno Sutic's avatar
Bruno Sutic committed
8

Bruno Sutic's avatar
Bruno Sutic committed
9
10
11
12
# delimiters
d=$'\t'
delimiter=$'\t'

Bruno Sutic's avatar
Bruno Sutic committed
13
14
15
# if "quiet" script produces no output
SCRIPT_OUTPUT="$1"

Bruno Sutic's avatar
Bruno Sutic committed
16
17
18
19
20
21
22
23
24
25
26
27
grouped_sessions_format() {
	local format
	format+="#{session_grouped}"
	format+="${delimiter}"
	format+="#{session_group}"
	format+="${delimiter}"
	format+="#{session_id}"
	format+="${delimiter}"
	format+="#{session_name}"
	echo "$format"
}

28
pane_format() {
Bruno Sutic's avatar
Bruno Sutic committed
29
	local format
30
31
	format+="pane"
	format+="${delimiter}"
Bruno Sutic's avatar
Bruno Sutic committed
32
33
34
35
	format+="#{session_name}"
	format+="${delimiter}"
	format+="#{window_index}"
	format+="${delimiter}"
36
	format+=":#{window_name}"
37
	format+="${delimiter}"
Bruno Sutic's avatar
Bruno Sutic committed
38
39
40
41
42
43
	format+="#{window_active}"
	format+="${delimiter}"
	format+=":#{window_flags}"
	format+="${delimiter}"
	format+="#{pane_index}"
	format+="${delimiter}"
44
	format+=":#{pane_current_path}"
Bruno Sutic's avatar
Bruno Sutic committed
45
46
	format+="${delimiter}"
	format+="#{pane_active}"
Bruno Sutic's avatar
Bruno Sutic committed
47
48
49
50
	format+="${delimiter}"
	format+="#{pane_current_command}"
	format+="${delimiter}"
	format+="#{pane_pid}"
51
52
	format+="${delimiter}"
	format+="#{history_size}"
Bruno Sutic's avatar
Bruno Sutic committed
53
54
55
	echo "$format"
}

56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
window_format() {
	local format
	format+="window"
	format+="${delimiter}"
	format+="#{session_name}"
	format+="${delimiter}"
	format+="#{window_index}"
	format+="${delimiter}"
	format+="#{window_active}"
	format+="${delimiter}"
	format+=":#{window_flags}"
	format+="${delimiter}"
	format+="#{window_layout}"
	echo "$format"
}

72
73
74
75
76
77
78
79
80
81
state_format() {
	local format
	format+="state"
	format+="${delimiter}"
	format+="#{client_session}"
	format+="${delimiter}"
	format+="#{client_last_session}"
	echo "$format"
}

Bruno Sutic's avatar
Bruno Sutic committed
82
dump_panes_raw() {
83
	tmux list-panes -a -F "$(pane_format)"
84
85
}

Bruno Sutic's avatar
Bruno Sutic committed
86
87
88
89
dump_windows_raw(){
	tmux list-windows -a -F "$(window_format)"
}

90
91
92
93
94
toggle_window_zoom() {
	local target="$1"
	tmux resize-pane -Z -t "$target"
}

95
_save_command_strategy_file() {
Bruno Sutic's avatar
Bruno Sutic committed
96
	local save_command_strategy="$(get_tmux_option "$save_command_strategy_option" "$default_save_command_strategy")"
97
	local strategy_file="$CURRENT_DIR/../save_command_strategies/${save_command_strategy}.sh"
Bruno Sutic's avatar
Bruno Sutic committed
98
	local default_strategy_file="$CURRENT_DIR/../save_command_strategies/${default_save_command_strategy}.sh"
99
100
101
102
103
104
105
	if [ -e "$strategy_file" ]; then # strategy file exists?
		echo "$strategy_file"
	else
		echo "$default_strategy_file"
	fi
}

Bruno Sutic's avatar
Bruno Sutic committed
106
pane_full_command() {
107
	local pane_pid="$1"
108
109
110
	local strategy_file="$(_save_command_strategy_file)"
	# execute strategy script to get pane full command
	$strategy_file "$pane_pid"
Bruno Sutic's avatar
Bruno Sutic committed
111
112
}

113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
number_nonempty_lines_on_screen() {
	local pane_id="$1"
	tmux capture-pane -pJ -t "$pane_id" |
		sed '/^$/d' |
		wc -l |
		sed 's/ //g'
}

# tests if there was any command output in the current pane
pane_has_any_content() {
	local pane_id="$1"
	local history_size="$(tmux display -p -t "$pane_id" -F "#{history_size}")"
	local cursor_y="$(tmux display -p -t "$pane_id" -F "#{cursor_y}")"
	# doing "cheap" tests first
	[ "$history_size" -gt 0 ] || # history has any content?
		[ "$cursor_y" -gt 0 ] || # cursor not in first line?
		[ "$(number_nonempty_lines_on_screen "$pane_id")" -gt 1 ]
}

quentin's avatar
quentin committed
132
133
capture_pane_contents() {
	local pane_id="$1"
134
	local start_line="-$2"
135
	local pane_contents_area="$3"
136
137
138
139
140
	if pane_has_any_content "$pane_id"; then
		if [ "$pane_contents_area" = "visible" ]; then
			start_line="0"
		fi
		# the printf hack below removes *trailing* empty lines
141
		printf '%s\n' "$(tmux capture-pane -epJ -S "$start_line" -t "$pane_id")" > "$(pane_contents_file "save" "$pane_id")"
142
	fi
quentin's avatar
quentin committed
143
144
}

145
save_shell_history() {
146
147
	if [ "$pane_command" = "bash" ]; then
		local history_w='history -w'
148
		local history_r='history -r'
149
150
151
152
153
154
155
156
157
158
		local accept_line='C-m'
		local end_of_line='C-e'
		local backward_kill_line='C-u'
	elif [ "$pane_command" = "zsh" ]; then
		# fc -W does not work with -L
		# fc -l format is different from what's written by fc -W
		# fc -R either reads the format produced by fc -W or considers
		# the entire line to be a command. That's why we need -n.
		# fc -l only list the last 16 items by default, I think 64 is more reasonable.
		local history_w='fc -lLn -64 >'
159
		local history_r='fc -R'
160
161
162
163
164
165
166
167
168

		local zsh_bindkey="$(zsh -i -c bindkey)"
		local accept_line="$(expr "$(echo "$zsh_bindkey" | grep -m1 '\saccept-line$')" : '^"\(.*\)".*')"
		local end_of_line="$(expr "$(echo "$zsh_bindkey" | grep -m1 '\send-of-line$')" : '^"\(.*\)".*')"
		local backward_kill_line="$(expr "$(echo "$zsh_bindkey" | grep -m1 '\sbackward-kill-line$')" : '^"\(.*\)".*')"
	else
		return
	fi

169
170
	local pane_id="$1"
	local pane_command="$2"
171
	local full_command="$3"
172
	if [ "$full_command" = ":" ]; then
173
174
		# leading space prevents the command from being saved to history
		# (assuming default HISTCONTROL settings)
175
		local write_command=" $history_w '$(resurrect_history_file "$pane_id" "$pane_command")'"
176
		local read_command=" $history_r '$(resurrect_history_file "$pane_id" "$pane_command")'"
177
178
		# C-e C-u is a Bash shortcut sequence to clear whole line. It is necessary to
		# delete any pending input so it does not interfere with our history command.
179
		tmux send-keys -t "$pane_id" "$end_of_line" "$backward_kill_line" "$write_command" "$accept_line"
180
181
		# Immediately restore after saving
		tmux send-keys -t "$pane_id" "$end_of_line" "$backward_kill_line" "$read_command" "$accept_line"
182
183
184
	fi
}

185
186
187
188
189
190
191
192
193
194
195
196
get_active_window_index() {
	local session_name="$1"
	tmux list-windows -t "$session_name" -F "#{window_flags} #{window_index}" |
		awk '$1 ~ /\*/ { print $2; }'
}

get_alternate_window_index() {
	local session_name="$1"
	tmux list-windows -t "$session_name" -F "#{window_flags} #{window_index}" |
		awk '$1 ~ /-/ { print $2; }'
}

Bruno Sutic's avatar
Bruno Sutic committed
197
198
199
200
201
202
203
dump_grouped_sessions() {
	local current_session_group=""
	local original_session
	tmux list-sessions -F "$(grouped_sessions_format)" |
		grep "^1" |
		cut -c 3- |
		sort |
204
		while IFS=$d read session_group session_id session_name; do
Bruno Sutic's avatar
Bruno Sutic committed
205
206
207
208
209
210
			if [ "$session_group" != "$current_session_group" ]; then
				# this session is the original/first session in the group
				original_session="$session_name"
				current_session_group="$session_group"
			else
				# this session "points" to the original session
211
212
				active_window_index="$(get_active_window_index "$session_name")"
				alternate_window_index="$(get_alternate_window_index "$session_name")"
213
				echo "grouped_session${d}${session_name}${d}${original_session}${d}:${alternate_window_index}${d}:${active_window_index}"
Bruno Sutic's avatar
Bruno Sutic committed
214
215
216
217
218
219
220
			fi
		done
}

fetch_and_dump_grouped_sessions(){
	local grouped_sessions_dump="$(dump_grouped_sessions)"
	get_grouped_sessions "$grouped_sessions_dump"
221
222
223
	if [ -n "$grouped_sessions_dump" ]; then
		echo "$grouped_sessions_dump"
	fi
Bruno Sutic's avatar
Bruno Sutic committed
224
225
}

Bruno Sutic's avatar
Bruno Sutic committed
226
227
228
229
# translates pane pid to process command running inside a pane
dump_panes() {
	local full_command
	dump_panes_raw |
230
		while IFS=$d read line_type session_name window_number window_name window_active window_flags pane_index dir pane_active pane_command pane_pid history_size; do
Bruno Sutic's avatar
Bruno Sutic committed
231
232
233
234
			# not saving panes from grouped sessions
			if is_session_grouped "$session_name"; then
				continue
			fi
Bruno Sutic's avatar
Bruno Sutic committed
235
			full_command="$(pane_full_command $pane_pid)"
236
			dir=$(echo $dir | sed 's/ /\\ /') # escape all spaces in directory path
Bruno Sutic's avatar
Bruno Sutic committed
237
238
239
240
			echo "${line_type}${d}${session_name}${d}${window_number}${d}${window_name}${d}${window_active}${d}${window_flags}${d}${pane_index}${d}${dir}${d}${pane_active}${d}${pane_command}${d}:${full_command}"
		done
}

241
dump_windows() {
Bruno Sutic's avatar
Bruno Sutic committed
242
	dump_windows_raw |
243
		while IFS=$d read line_type session_name window_index window_active window_flags window_layout; do
Bruno Sutic's avatar
Bruno Sutic committed
244
245
246
247
248
249
			# not saving windows from grouped sessions
			if is_session_grouped "$session_name"; then
				continue
			fi
			echo "${line_type}${d}${session_name}${d}${window_index}${d}${window_active}${d}${window_flags}${d}${window_layout}"
		done
250
251
}

252
253
dump_state() {
	tmux display-message -p "$(state_format)"
Bruno Sutic's avatar
Bruno Sutic committed
254
255
}

quentin's avatar
quentin committed
256
dump_pane_contents() {
257
	local pane_contents_area="$(get_tmux_option "$pane_contents_area_option" "$default_pane_contents_area")"
258
259
260
	dump_panes_raw |
		while IFS=$d read line_type session_name window_number window_name window_active window_flags pane_index dir pane_active pane_command pane_pid history_size; do
			capture_pane_contents "${session_name}:${window_number}.${pane_index}" "$history_size" "$pane_contents_area"
quentin's avatar
quentin committed
261
262
263
		done
}

264
dump_shell_history() {
265
	dump_panes |
266
		while IFS=$d read line_type session_name window_number window_name window_active window_flags pane_index dir pane_active pane_command full_command; do
267
			save_shell_history "$session_name:$window_number.$pane_index" "$pane_command" "$full_command"
268
269
270
		done
}

Xu Cheng's avatar
Xu Cheng committed
271
remove_old_backups() {
Bruno Sutic's avatar
Bruno Sutic committed
272
	# remove resurrect files older than 30 days, but keep at least 5 copies of backup.
Xu Cheng's avatar
Xu Cheng committed
273
	local -a files
274
	files=($(ls -t $(resurrect_dir)/${RESURRECT_FILE_PREFIX}_*.${RESURRECT_FILE_EXTENSION} | tail -n +6))
275
	[[ ${#files[@]} -eq 0 ]] ||
276
		find "${files[@]}" -type f -mtime +30 -exec rm -v "{}" \;
Xu Cheng's avatar
Xu Cheng committed
277
278
}

279
280
save_all() {
	local resurrect_file_path="$(resurrect_file_path)"
281
	local last_resurrect_file="$(last_resurrect_file)"
282
	mkdir -p "$(resurrect_dir)"
Bruno Sutic's avatar
Bruno Sutic committed
283
284
	fetch_and_dump_grouped_sessions > "$resurrect_file_path"
	dump_panes   >> "$resurrect_file_path"
285
286
	dump_windows >> "$resurrect_file_path"
	dump_state   >> "$resurrect_file_path"
Ash Berlin-Taylor's avatar
Ash Berlin-Taylor committed
287
	execute_hook "post-save-layout" "$resurrect_file_path"
288
289
290
291
292
	if files_differ "$resurrect_file_path" "$last_resurrect_file"; then
		ln -fs "$(basename "$resurrect_file_path")" "$last_resurrect_file"
	else
		rm "$resurrect_file_path"
	fi
quentin's avatar
quentin committed
293
	if capture_pane_contents_option_on; then
294
		mkdir -p "$(pane_contents_dir "save")"
quentin's avatar
quentin committed
295
		dump_pane_contents
296
		pane_contents_create_archive
297
		rm "$(pane_contents_dir "save")"/*
quentin's avatar
quentin committed
298
	fi
299
	if save_shell_history_option_on; then
300
		dump_shell_history
301
	fi
Xu Cheng's avatar
Xu Cheng committed
302
	remove_old_backups
Ash Berlin-Taylor's avatar
Ash Berlin-Taylor committed
303
	execute_hook "post-save-all"
Bruno Sutic's avatar
Bruno Sutic committed
304
305
}

Bruno Sutic's avatar
Bruno Sutic committed
306
307
308
309
show_output() {
	[ "$SCRIPT_OUTPUT" != "quiet" ]
}

Bruno Sutic's avatar
Bruno Sutic committed
310
main() {
311
	if supported_tmux_version_ok; then
Bruno Sutic's avatar
Bruno Sutic committed
312
313
314
		if show_output; then
			start_spinner "Saving..." "Tmux environment saved!"
		fi
315
		save_all
Bruno Sutic's avatar
Bruno Sutic committed
316
317
318
319
		if show_output; then
			stop_spinner
			display_message "Tmux environment saved!"
		fi
320
	fi
Bruno Sutic's avatar
Bruno Sutic committed
321
322
}
main