2014-08-26 10:24:31 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
|
|
|
|
2014-08-30 19:42:39 +00:00
|
|
|
source "$CURRENT_DIR/variables.sh"
|
2014-08-26 10:24:31 +00:00
|
|
|
source "$CURRENT_DIR/helpers.sh"
|
2014-08-29 17:51:47 +00:00
|
|
|
source "$CURRENT_DIR/spinner_helpers.sh"
|
2014-08-26 10:24:31 +00:00
|
|
|
|
2015-02-09 01:11:35 +00:00
|
|
|
# delimiters
|
|
|
|
d=$'\t'
|
|
|
|
delimiter=$'\t'
|
|
|
|
|
2015-02-12 11:53:19 +00:00
|
|
|
# if "quiet" script produces no output
|
|
|
|
SCRIPT_OUTPUT="$1"
|
|
|
|
|
2015-02-09 01:11:35 +00:00
|
|
|
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"
|
|
|
|
}
|
|
|
|
|
2014-08-26 15:23:20 +00:00
|
|
|
pane_format() {
|
2014-08-26 10:24:31 +00:00
|
|
|
local format
|
2014-08-26 15:23:20 +00:00
|
|
|
format+="pane"
|
|
|
|
format+="${delimiter}"
|
2014-08-26 10:24:31 +00:00
|
|
|
format+="#{session_name}"
|
|
|
|
format+="${delimiter}"
|
|
|
|
format+="#{window_index}"
|
|
|
|
format+="${delimiter}"
|
2014-08-26 15:51:56 +00:00
|
|
|
format+=":#{window_name}"
|
2014-08-26 15:28:40 +00:00
|
|
|
format+="${delimiter}"
|
2014-08-26 16:54:39 +00:00
|
|
|
format+="#{window_active}"
|
|
|
|
format+="${delimiter}"
|
|
|
|
format+=":#{window_flags}"
|
|
|
|
format+="${delimiter}"
|
|
|
|
format+="#{pane_index}"
|
|
|
|
format+="${delimiter}"
|
2014-08-28 22:41:13 +00:00
|
|
|
format+=":#{pane_current_path}"
|
2014-08-26 16:54:39 +00:00
|
|
|
format+="${delimiter}"
|
|
|
|
format+="#{pane_active}"
|
2014-08-26 22:11:13 +00:00
|
|
|
format+="${delimiter}"
|
|
|
|
format+="#{pane_current_command}"
|
|
|
|
format+="${delimiter}"
|
|
|
|
format+="#{pane_pid}"
|
2015-05-09 13:05:30 +00:00
|
|
|
format+="${delimiter}"
|
|
|
|
format+="#{history_size}"
|
2014-08-26 10:24:31 +00:00
|
|
|
echo "$format"
|
|
|
|
}
|
|
|
|
|
2014-08-26 18:19:34 +00:00
|
|
|
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"
|
|
|
|
}
|
|
|
|
|
2014-08-26 15:23:20 +00:00
|
|
|
state_format() {
|
|
|
|
local format
|
|
|
|
format+="state"
|
|
|
|
format+="${delimiter}"
|
|
|
|
format+="#{client_session}"
|
|
|
|
format+="${delimiter}"
|
|
|
|
format+="#{client_last_session}"
|
|
|
|
echo "$format"
|
|
|
|
}
|
|
|
|
|
2014-08-26 22:11:13 +00:00
|
|
|
dump_panes_raw() {
|
2015-05-09 13:05:30 +00:00
|
|
|
tmux list-panes -a -F "$(pane_format)"
|
2014-08-26 15:23:20 +00:00
|
|
|
}
|
|
|
|
|
2015-02-09 01:11:35 +00:00
|
|
|
dump_windows_raw(){
|
|
|
|
tmux list-windows -a -F "$(window_format)"
|
|
|
|
}
|
|
|
|
|
2015-02-12 12:29:49 +00:00
|
|
|
toggle_window_zoom() {
|
|
|
|
local target="$1"
|
|
|
|
tmux resize-pane -Z -t "$target"
|
|
|
|
}
|
|
|
|
|
2014-09-20 21:47:15 +00:00
|
|
|
_save_command_strategy_file() {
|
2014-09-21 13:12:35 +00:00
|
|
|
local save_command_strategy="$(get_tmux_option "$save_command_strategy_option" "$default_save_command_strategy")"
|
2014-09-20 21:47:15 +00:00
|
|
|
local strategy_file="$CURRENT_DIR/../save_command_strategies/${save_command_strategy}.sh"
|
2014-09-21 13:12:35 +00:00
|
|
|
local default_strategy_file="$CURRENT_DIR/../save_command_strategies/${default_save_command_strategy}.sh"
|
2014-09-20 21:47:15 +00:00
|
|
|
if [ -e "$strategy_file" ]; then # strategy file exists?
|
|
|
|
echo "$strategy_file"
|
|
|
|
else
|
|
|
|
echo "$default_strategy_file"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2014-08-26 22:11:13 +00:00
|
|
|
pane_full_command() {
|
2014-09-20 20:33:14 +00:00
|
|
|
local pane_pid="$1"
|
2014-09-20 21:47:15 +00:00
|
|
|
local strategy_file="$(_save_command_strategy_file)"
|
|
|
|
# execute strategy script to get pane full command
|
|
|
|
$strategy_file "$pane_pid"
|
2014-08-26 22:11:13 +00:00
|
|
|
}
|
|
|
|
|
2015-07-07 21:57:11 +00:00
|
|
|
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 ]
|
|
|
|
}
|
|
|
|
|
2015-03-17 02:17:37 +00:00
|
|
|
capture_pane_contents() {
|
|
|
|
local pane_id="$1"
|
2015-03-18 21:38:38 +00:00
|
|
|
local start_line="-$2"
|
2015-05-09 10:22:48 +00:00
|
|
|
local pane_contents_area="$3"
|
2015-07-07 21:57:11 +00:00
|
|
|
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
|
2017-05-30 04:40:48 +00:00
|
|
|
printf '%s\n' "$(tmux capture-pane -epJ -S "$start_line" -t "$pane_id")" > "$(pane_contents_file "save" "$pane_id")"
|
2015-05-09 10:22:48 +00:00
|
|
|
fi
|
2015-03-17 02:17:37 +00:00
|
|
|
}
|
|
|
|
|
2014-10-12 22:47:44 +00:00
|
|
|
save_shell_history() {
|
2015-09-16 16:02:35 +00:00
|
|
|
if [ "$pane_command" = "bash" ]; then
|
|
|
|
local history_w='history -w'
|
2015-10-08 23:35:43 +00:00
|
|
|
local history_r='history -r'
|
2015-09-16 16:02:35 +00:00
|
|
|
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 >'
|
2015-10-08 23:35:43 +00:00
|
|
|
local history_r='fc -R'
|
2015-09-16 16:02:35 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2014-10-12 22:47:44 +00:00
|
|
|
local pane_id="$1"
|
|
|
|
local pane_command="$2"
|
2014-10-25 23:30:00 +00:00
|
|
|
local full_command="$3"
|
2015-09-16 16:02:35 +00:00
|
|
|
if [ "$full_command" = ":" ]; then
|
2014-10-12 22:47:44 +00:00
|
|
|
# leading space prevents the command from being saved to history
|
|
|
|
# (assuming default HISTCONTROL settings)
|
2015-09-16 16:02:35 +00:00
|
|
|
local write_command=" $history_w '$(resurrect_history_file "$pane_id" "$pane_command")'"
|
2015-10-08 23:35:43 +00:00
|
|
|
local read_command=" $history_r '$(resurrect_history_file "$pane_id" "$pane_command")'"
|
2014-10-12 22:47:44 +00:00
|
|
|
# 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.
|
2015-09-16 16:02:35 +00:00
|
|
|
tmux send-keys -t "$pane_id" "$end_of_line" "$backward_kill_line" "$write_command" "$accept_line"
|
2015-10-08 23:35:43 +00:00
|
|
|
# Immediately restore after saving
|
|
|
|
tmux send-keys -t "$pane_id" "$end_of_line" "$backward_kill_line" "$read_command" "$accept_line"
|
2014-10-12 22:47:44 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2015-02-09 14:57:48 +00:00
|
|
|
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; }'
|
|
|
|
}
|
|
|
|
|
2015-02-09 01:11:35 +00:00
|
|
|
dump_grouped_sessions() {
|
|
|
|
local current_session_group=""
|
|
|
|
local original_session
|
|
|
|
tmux list-sessions -F "$(grouped_sessions_format)" |
|
|
|
|
grep "^1" |
|
|
|
|
cut -c 3- |
|
|
|
|
sort |
|
2015-02-09 15:34:24 +00:00
|
|
|
while IFS=$d read session_group session_id session_name; do
|
2015-02-09 01:11:35 +00:00
|
|
|
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
|
2015-02-09 14:57:48 +00:00
|
|
|
active_window_index="$(get_active_window_index "$session_name")"
|
|
|
|
alternate_window_index="$(get_alternate_window_index "$session_name")"
|
2015-02-09 15:11:49 +00:00
|
|
|
echo "grouped_session${d}${session_name}${d}${original_session}${d}:${alternate_window_index}${d}:${active_window_index}"
|
2015-02-09 01:11:35 +00:00
|
|
|
fi
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
fetch_and_dump_grouped_sessions(){
|
|
|
|
local grouped_sessions_dump="$(dump_grouped_sessions)"
|
|
|
|
get_grouped_sessions "$grouped_sessions_dump"
|
2015-02-09 15:05:36 +00:00
|
|
|
if [ -n "$grouped_sessions_dump" ]; then
|
|
|
|
echo "$grouped_sessions_dump"
|
|
|
|
fi
|
2015-02-09 01:11:35 +00:00
|
|
|
}
|
|
|
|
|
2014-08-26 22:11:13 +00:00
|
|
|
# translates pane pid to process command running inside a pane
|
|
|
|
dump_panes() {
|
|
|
|
local full_command
|
|
|
|
dump_panes_raw |
|
2015-05-09 13:05:30 +00:00
|
|
|
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
|
2015-02-09 01:11:35 +00:00
|
|
|
# not saving panes from grouped sessions
|
|
|
|
if is_session_grouped "$session_name"; then
|
|
|
|
continue
|
|
|
|
fi
|
2014-08-26 22:11:13 +00:00
|
|
|
full_command="$(pane_full_command $pane_pid)"
|
2019-03-01 15:37:01 +00:00
|
|
|
dir=$(echo $dir | sed 's/ /\\ /') # escape all spaces in directory path
|
2014-08-26 22:11:13 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2014-08-26 18:19:34 +00:00
|
|
|
dump_windows() {
|
2015-02-09 01:11:35 +00:00
|
|
|
dump_windows_raw |
|
2015-02-09 15:34:24 +00:00
|
|
|
while IFS=$d read line_type session_name window_index window_active window_flags window_layout; do
|
2015-02-09 01:11:35 +00:00
|
|
|
# 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
|
2014-08-26 18:19:34 +00:00
|
|
|
}
|
|
|
|
|
2014-08-26 15:23:20 +00:00
|
|
|
dump_state() {
|
|
|
|
tmux display-message -p "$(state_format)"
|
2014-08-26 10:24:31 +00:00
|
|
|
}
|
|
|
|
|
2015-03-17 02:17:37 +00:00
|
|
|
dump_pane_contents() {
|
2015-05-09 10:22:48 +00:00
|
|
|
local pane_contents_area="$(get_tmux_option "$pane_contents_area_option" "$default_pane_contents_area")"
|
2015-05-09 13:05:30 +00:00
|
|
|
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"
|
2015-03-17 02:17:37 +00:00
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2015-09-16 16:08:54 +00:00
|
|
|
dump_shell_history() {
|
2014-10-25 23:30:00 +00:00
|
|
|
dump_panes |
|
2015-02-09 15:34:24 +00:00
|
|
|
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
|
2014-10-25 23:30:00 +00:00
|
|
|
save_shell_history "$session_name:$window_number.$pane_index" "$pane_command" "$full_command"
|
2014-10-17 15:45:49 +00:00
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2017-06-02 06:39:11 +00:00
|
|
|
remove_old_backups() {
|
2017-06-03 22:32:53 +00:00
|
|
|
# remove resurrect files older than 30 days, but keep at least 5 copies of backup.
|
2017-06-02 06:39:11 +00:00
|
|
|
local -a files
|
2017-06-03 22:31:17 +00:00
|
|
|
files=($(ls -t $(resurrect_dir)/${RESURRECT_FILE_PREFIX}_*.${RESURRECT_FILE_EXTENSION} | tail -n +6))
|
2017-06-03 22:52:37 +00:00
|
|
|
[[ ${#files[@]} -eq 0 ]] ||
|
2017-10-25 12:30:50 +00:00
|
|
|
find "${files[@]}" -type f -mtime +30 -exec rm -v "{}" \;
|
2017-06-02 06:39:11 +00:00
|
|
|
}
|
|
|
|
|
2014-08-29 16:59:14 +00:00
|
|
|
save_all() {
|
|
|
|
local resurrect_file_path="$(resurrect_file_path)"
|
2017-05-30 03:14:14 +00:00
|
|
|
local last_resurrect_file="$(last_resurrect_file)"
|
2014-08-29 16:59:14 +00:00
|
|
|
mkdir -p "$(resurrect_dir)"
|
2015-02-09 01:11:35 +00:00
|
|
|
fetch_and_dump_grouped_sessions > "$resurrect_file_path"
|
|
|
|
dump_panes >> "$resurrect_file_path"
|
2014-11-07 13:52:21 +00:00
|
|
|
dump_windows >> "$resurrect_file_path"
|
|
|
|
dump_state >> "$resurrect_file_path"
|
2018-08-17 19:38:33 +00:00
|
|
|
execute_hook "post-save-layout" "$resurrect_file_path"
|
2017-05-30 03:14:14 +00:00
|
|
|
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
|
2015-03-17 02:17:37 +00:00
|
|
|
if capture_pane_contents_option_on; then
|
2017-05-30 04:40:48 +00:00
|
|
|
mkdir -p "$(pane_contents_dir "save")"
|
2015-03-17 02:17:37 +00:00
|
|
|
dump_pane_contents
|
2015-07-08 16:45:43 +00:00
|
|
|
pane_contents_create_archive
|
2017-05-30 04:40:48 +00:00
|
|
|
rm "$(pane_contents_dir "save")"/*
|
2015-03-17 02:17:37 +00:00
|
|
|
fi
|
2015-09-16 16:06:41 +00:00
|
|
|
if save_shell_history_option_on; then
|
2015-09-16 16:08:54 +00:00
|
|
|
dump_shell_history
|
2014-10-17 20:33:29 +00:00
|
|
|
fi
|
2017-06-02 06:39:11 +00:00
|
|
|
remove_old_backups
|
2018-08-17 19:38:33 +00:00
|
|
|
execute_hook "post-save-all"
|
2014-08-26 10:24:31 +00:00
|
|
|
}
|
|
|
|
|
2015-02-12 11:53:19 +00:00
|
|
|
show_output() {
|
|
|
|
[ "$SCRIPT_OUTPUT" != "quiet" ]
|
|
|
|
}
|
|
|
|
|
2014-08-26 10:24:31 +00:00
|
|
|
main() {
|
2014-08-26 13:47:31 +00:00
|
|
|
if supported_tmux_version_ok; then
|
2015-02-12 11:53:19 +00:00
|
|
|
if show_output; then
|
|
|
|
start_spinner "Saving..." "Tmux environment saved!"
|
|
|
|
fi
|
2014-08-29 16:59:14 +00:00
|
|
|
save_all
|
2015-02-12 11:53:19 +00:00
|
|
|
if show_output; then
|
|
|
|
stop_spinner
|
|
|
|
display_message "Tmux environment saved!"
|
|
|
|
fi
|
2014-08-26 13:47:31 +00:00
|
|
|
fi
|
2014-08-26 10:24:31 +00:00
|
|
|
}
|
|
|
|
main
|