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
|
|
|
|
2014-08-26 15:23:20 +00:00
|
|
|
pane_format() {
|
2014-08-26 10:24:31 +00:00
|
|
|
local delimiter=$'\t'
|
|
|
|
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}"
|
2014-08-26 10:24:31 +00:00
|
|
|
echo "$format"
|
|
|
|
}
|
|
|
|
|
2014-08-26 18:19:34 +00:00
|
|
|
window_format() {
|
|
|
|
local delimiter=$'\t'
|
|
|
|
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 delimiter=$'\t'
|
|
|
|
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() {
|
2014-08-26 15:23:20 +00:00
|
|
|
tmux list-panes -a -F "$(pane_format)"
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-10-12 22:47:44 +00:00
|
|
|
save_shell_history() {
|
|
|
|
local pane_id="$1"
|
|
|
|
local pane_command="$2"
|
|
|
|
if [ "$pane_command" = "bash" ]; then
|
|
|
|
# leading space prevents the command from being saved to history
|
|
|
|
# (assuming default HISTCONTROL settings)
|
|
|
|
local write_command=" history -w '$(resurrect_history_file "$pane_id")'"
|
|
|
|
# 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.
|
|
|
|
tmux send-keys -t "$pane_id" C-e C-u "$write_command" C-m
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2014-08-26 22:11:13 +00:00
|
|
|
# translates pane pid to process command running inside a pane
|
|
|
|
dump_panes() {
|
|
|
|
local full_command
|
|
|
|
local d=$'\t' # delimiter
|
|
|
|
dump_panes_raw |
|
|
|
|
while IFS=$'\t' read line_type session_name window_number window_name window_active window_flags pane_index dir pane_active pane_command pane_pid; do
|
2014-10-24 23:19:56 +00:00
|
|
|
# check if current pane is part of a maximized window and if the pane is active
|
|
|
|
if [[ "${window_flags}" == *Z* ]] && [[ ${pane_active} == 1 ]]; then
|
2014-10-24 23:12:39 +00:00
|
|
|
# unmaximize the pane
|
|
|
|
tmux resize-pane -Z -t "${session_name}:${window_number}"
|
|
|
|
fi
|
2014-08-26 22:11:13 +00:00
|
|
|
full_command="$(pane_full_command $pane_pid)"
|
|
|
|
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() {
|
|
|
|
tmux list-windows -a -F "$(window_format)"
|
|
|
|
}
|
|
|
|
|
2014-08-26 15:23:20 +00:00
|
|
|
dump_state() {
|
|
|
|
tmux display-message -p "$(state_format)"
|
2014-08-26 10:24:31 +00:00
|
|
|
}
|
|
|
|
|
2014-10-17 15:45:49 +00:00
|
|
|
dump_bash_history() {
|
|
|
|
dump_panes_raw |
|
|
|
|
while IFS=$'\t' read line_type session_name window_number window_name window_active window_flags pane_index dir pane_active pane_command pane_pid; do
|
|
|
|
save_shell_history "$session_name:$window_number.$pane_index" "$pane_command"
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2014-08-29 16:59:14 +00:00
|
|
|
save_all() {
|
|
|
|
local resurrect_file_path="$(resurrect_file_path)"
|
|
|
|
mkdir -p "$(resurrect_dir)"
|
|
|
|
dump_panes > $resurrect_file_path
|
|
|
|
dump_windows >> $resurrect_file_path
|
|
|
|
dump_state >> $resurrect_file_path
|
|
|
|
ln -fs "$resurrect_file_path" "$(last_resurrect_file)"
|
2014-10-17 20:33:29 +00:00
|
|
|
if save_bash_history_option_on; then
|
|
|
|
dump_bash_history
|
|
|
|
fi
|
2014-10-24 23:12:39 +00:00
|
|
|
restore_zoomed_windows
|
2014-08-26 10:24:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
main() {
|
2014-08-26 13:47:31 +00:00
|
|
|
if supported_tmux_version_ok; then
|
2014-08-29 17:51:47 +00:00
|
|
|
start_spinner "Saving..." "Tmux environment saved!"
|
2014-08-29 16:59:14 +00:00
|
|
|
save_all
|
2014-08-29 17:51:47 +00:00
|
|
|
stop_spinner
|
|
|
|
display_message "Tmux environment saved!"
|
2014-08-26 13:47:31 +00:00
|
|
|
fi
|
2014-08-26 10:24:31 +00:00
|
|
|
}
|
|
|
|
main
|