Restoring tmux sessions works

This commit is contained in:
Bruno Sutic 2014-08-26 13:20:15 +02:00
parent b0f5fe18d8
commit 09869110fc
No known key found for this signature in database
GPG Key ID: 66D96E4F2F7EF26C

84
scripts/session_restorer.sh Executable file
View File

@ -0,0 +1,84 @@
#!/usr/bin/env bash
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source "$CURRENT_DIR/helpers.sh"
window_exists() {
local session_name="$1"
local window_number="$2"
tmux list-windows -t "$session_name" -F "#{window_index}" 2>/dev/null |
\grep -q "^$window_number$"
}
session_exists() {
local session_name="$1"
tmux has-session -t "$session_name" 2>/dev/null
}
first_window_num() {
tmux show -gv base-index
}
tmux_socket() {
echo $TMUX | cut -d',' -f1
}
new_window() {
local session_name="$1"
local window_number="$2"
local window_name="$3"
local dir="$4"
tmux new-window -d -t "${session_name}:${window_number}" -n "$window_name" -c "$dir"
}
new_session() {
local session_name="$1"
local window_number="$2"
local window_name="$3"
local dir="$4"
TMUX="" tmux -S "$(tmux_socket)" new-session -d -s "$session_name" -n "$window_name" -c "$dir"
# change first window number if necessary
local created_window_num="$(first_window_num)"
if [ $created_window_num -ne $window_number ]; then
tmux move-window -s "${session_name}:${created_window_num}" -t "${session_name}:${window_number}"
fi
}
new_pane() {
local session_name="$1"
local window_number="$2"
local window_name="$3"
local dir="$4"
tmux split-window -d -t "${session_name}:${window_number}" -c "$dir"
}
restore_pane() {
local pane="$1"
echo "$pane" |
while IFS=$'\t' read session_name window_number window_name dir; do
# echo "$session_name - $window_number - $window_name - $dir"
if window_exists "$session_name" "$window_number"; then
echo "new pane $session_name $window_number"
new_pane "$session_name" "$window_number" "$window_name" "$dir"
elif session_exists "$session_name"; then
echo "new window $session_name $window_number"
new_window "$session_name" "$window_number" "$window_name" "$dir"
else
echo "new session $session_name"
new_session "$session_name" "$window_number" "$window_name" "$dir"
fi
done
}
restore_all_sessions() {
while read line; do
restore_pane "$line"
done < $HOME/.tmux/session
display_message "Restored all Tmux sessions!"
}
main() {
restore_all_sessions
}
main