tmux-resurrect/scripts/session_saver.sh

107 lines
2.4 KiB
Bash
Raw Normal View History

2014-08-26 10:24:31 +00:00
#!/usr/bin/env bash
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source "$CURRENT_DIR/helpers.sh"
pane_format() {
2014-08-26 10:24:31 +00:00
local delimiter=$'\t'
local format
format+="pane"
format+="${delimiter}"
2014-08-26 10:24:31 +00:00
format+="#{session_name}"
format+="${delimiter}"
format+="#{window_index}"
format+="${delimiter}"
format+=":#{window_name}"
format+="${delimiter}"
format+="#{window_active}"
format+="${delimiter}"
format+=":#{window_flags}"
format+="${delimiter}"
format+="#{pane_index}"
format+="${delimiter}"
format+="#{pane_current_path}"
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"
}
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"
}
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() {
tmux list-panes -a -F "$(pane_format)"
}
2014-08-26 22:11:13 +00:00
pane_full_command() {
pane_pid="$1"
\pgrep -lf -P "$pane_pid" |
cut -d' ' -f2-
}
# 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
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
}
dump_windows() {
tmux list-windows -a -F "$(window_format)"
}
dump_state() {
tmux display-message -p "$(state_format)"
2014-08-26 10:24:31 +00:00
}
save_all_sessions() {
local session_path="$(session_path)"
mkdir -p "$(sessions_dir)"
dump_panes > $session_path
dump_windows >> $session_path
dump_state >> $session_path
ln -fs "$session_path" "$(last_session_path)"
2014-08-26 10:24:31 +00:00
display_message "Saved all Tmux sessions!"
}
main() {
if supported_tmux_version_ok; then
save_all_sessions
fi
2014-08-26 10:24:31 +00:00
}
main