Save grouped sessions

Grouped sessions are session started with the command
`tmux new-session -t <existing-session-name>`

Those sessions share the panes and windows with the existing, original
session.
We're making sure to identify and save grouped sessions.
On the other hand, we don't want to save panes and windows twice
This commit is contained in:
Bruno Sutic 2015-02-09 02:11:35 +01:00
parent 3244b809eb
commit bfc625e13f
No known key found for this signature in database
GPG Key ID: 66D96E4F2F7EF26C
2 changed files with 71 additions and 6 deletions

View File

@ -3,6 +3,8 @@ resurrect_dir_option="@resurrect-dir"
SUPPORTED_VERSION="1.9" SUPPORTED_VERSION="1.9"
d=$'\t'
# helper functions # helper functions
get_tmux_option() { get_tmux_option() {
local option="$1" local option="$1"
@ -54,6 +56,16 @@ save_bash_history_option_on() {
[ "$option" == "on" ] [ "$option" == "on" ]
} }
get_grouped_sessions() {
local grouped_sessions_dump="$1"
export GROUPED_SESSIONS="${d}$(echo "$grouped_sessions_dump" | cut -f2 -d"${d}" | tr "\\n" "${d}")"
}
is_session_grouped() {
local session_name="$1"
[[ "$GROUPED_SESSIONS" == *"${d}${session_name}${d}"* ]]
}
# path helpers # path helpers
resurrect_dir() { resurrect_dir() {

View File

@ -6,8 +6,23 @@ source "$CURRENT_DIR/variables.sh"
source "$CURRENT_DIR/helpers.sh" source "$CURRENT_DIR/helpers.sh"
source "$CURRENT_DIR/spinner_helpers.sh" source "$CURRENT_DIR/spinner_helpers.sh"
# delimiters
d=$'\t'
delimiter=$'\t'
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"
}
pane_format() { pane_format() {
local delimiter=$'\t'
local format local format
format+="pane" format+="pane"
format+="${delimiter}" format+="${delimiter}"
@ -34,7 +49,6 @@ pane_format() {
} }
window_format() { window_format() {
local delimiter=$'\t'
local format local format
format+="window" format+="window"
format+="${delimiter}" format+="${delimiter}"
@ -51,7 +65,6 @@ window_format() {
} }
state_format() { state_format() {
local delimiter=$'\t'
local format local format
format+="state" format+="state"
format+="${delimiter}" format+="${delimiter}"
@ -65,6 +78,10 @@ dump_panes_raw() {
tmux list-panes -a -F "$(pane_format)" tmux list-panes -a -F "$(pane_format)"
} }
dump_windows_raw(){
tmux list-windows -a -F "$(window_format)"
}
_save_command_strategy_file() { _save_command_strategy_file() {
local save_command_strategy="$(get_tmux_option "$save_command_strategy_option" "$default_save_command_strategy")" local save_command_strategy="$(get_tmux_option "$save_command_strategy_option" "$default_save_command_strategy")"
local strategy_file="$CURRENT_DIR/../save_command_strategies/${save_command_strategy}.sh" local strategy_file="$CURRENT_DIR/../save_command_strategies/${save_command_strategy}.sh"
@ -97,12 +114,40 @@ save_shell_history() {
fi fi
} }
dump_grouped_sessions() {
local current_session_group=""
local original_session
tmux list-sessions -F "$(grouped_sessions_format)" |
grep "^1" |
cut -c 3- |
sort |
while IFS=$'\t' read session_group session_id session_name; do
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
echo "grouped_session${d}${session_name}${d}${original_session}"
fi
done
}
fetch_and_dump_grouped_sessions(){
local grouped_sessions_dump="$(dump_grouped_sessions)"
get_grouped_sessions "$grouped_sessions_dump"
echo "$grouped_sessions_dump"
}
# translates pane pid to process command running inside a pane # translates pane pid to process command running inside a pane
dump_panes() { dump_panes() {
local full_command local full_command
local d=$'\t' # delimiter
dump_panes_raw | 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 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
# not saving panes from grouped sessions
if is_session_grouped "$session_name"; then
continue
fi
# check if current pane is part of a maximized window and if the pane is active # check if current pane is part of a maximized window and if the pane is active
if [[ "${window_flags}" == *Z* ]] && [[ ${pane_active} == 1 ]]; then if [[ "${window_flags}" == *Z* ]] && [[ ${pane_active} == 1 ]]; then
# unmaximize the pane # unmaximize the pane
@ -114,7 +159,14 @@ dump_panes() {
} }
dump_windows() { dump_windows() {
tmux list-windows -a -F "$(window_format)" dump_windows_raw |
while IFS=$'\t' read line_type session_name window_index window_active window_flags window_layout; do
# 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
} }
dump_state() { dump_state() {
@ -131,7 +183,8 @@ dump_bash_history() {
save_all() { save_all() {
local resurrect_file_path="$(resurrect_file_path)" local resurrect_file_path="$(resurrect_file_path)"
mkdir -p "$(resurrect_dir)" mkdir -p "$(resurrect_dir)"
dump_panes > "$resurrect_file_path" fetch_and_dump_grouped_sessions > "$resurrect_file_path"
dump_panes >> "$resurrect_file_path"
dump_windows >> "$resurrect_file_path" dump_windows >> "$resurrect_file_path"
dump_state >> "$resurrect_file_path" dump_state >> "$resurrect_file_path"
ln -fs "$(basename "$resurrect_file_path")" "$(last_resurrect_file)" ln -fs "$(basename "$resurrect_file_path")" "$(last_resurrect_file)"