2022-10-22 06:21:51 +00:00
|
|
|
if [ -d "$HOME/.tmux/resurrect" ]; then
|
|
|
|
default_resurrect_dir="$HOME/.tmux/resurrect"
|
|
|
|
else
|
|
|
|
default_resurrect_dir="${XDG_DATA_HOME:-$HOME/.local/share}"/tmux/resurrect
|
|
|
|
fi
|
2014-08-29 16:59:14 +00:00
|
|
|
resurrect_dir_option="@resurrect-dir"
|
2014-08-27 11:12:32 +00:00
|
|
|
|
2014-08-26 13:47:31 +00:00
|
|
|
SUPPORTED_VERSION="1.9"
|
2017-06-03 22:31:17 +00:00
|
|
|
RESURRECT_FILE_PREFIX="tmux_resurrect"
|
|
|
|
RESURRECT_FILE_EXTENSION="txt"
|
2017-06-01 15:03:39 +00:00
|
|
|
_RESURRECT_DIR=""
|
|
|
|
_RESURRECT_FILE_PATH=""
|
2014-08-26 13:47:31 +00:00
|
|
|
|
2015-02-09 01:11:35 +00:00
|
|
|
d=$'\t'
|
|
|
|
|
2014-08-26 13:31:47 +00:00
|
|
|
# helper functions
|
2014-08-25 20:29:03 +00:00
|
|
|
get_tmux_option() {
|
|
|
|
local option="$1"
|
|
|
|
local default_value="$2"
|
|
|
|
local option_value=$(tmux show-option -gqv "$option")
|
|
|
|
if [ -z "$option_value" ]; then
|
|
|
|
echo "$default_value"
|
|
|
|
else
|
|
|
|
echo "$option_value"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
# Ensures a message is displayed for 5 seconds in tmux prompt.
|
|
|
|
# Does not override the 'display-time' tmux option.
|
|
|
|
display_message() {
|
|
|
|
local message="$1"
|
|
|
|
|
|
|
|
# display_duration defaults to 5 seconds, if not passed as an argument
|
|
|
|
if [ "$#" -eq 2 ]; then
|
|
|
|
local display_duration="$2"
|
|
|
|
else
|
|
|
|
local display_duration="5000"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# saves user-set 'display-time' option
|
|
|
|
local saved_display_time=$(get_tmux_option "display-time" "750")
|
|
|
|
|
|
|
|
# sets message display time to 5 seconds
|
|
|
|
tmux set-option -gq display-time "$display_duration"
|
|
|
|
|
|
|
|
# displays message
|
|
|
|
tmux display-message "$message"
|
|
|
|
|
|
|
|
# restores original 'display-time' value
|
|
|
|
tmux set-option -gq display-time "$saved_display_time"
|
|
|
|
}
|
2014-08-26 13:31:47 +00:00
|
|
|
|
2014-08-26 13:47:31 +00:00
|
|
|
|
|
|
|
supported_tmux_version_ok() {
|
|
|
|
$CURRENT_DIR/check_tmux_version.sh "$SUPPORTED_VERSION"
|
|
|
|
}
|
|
|
|
|
2014-08-28 21:39:53 +00:00
|
|
|
remove_first_char() {
|
|
|
|
echo "$1" | cut -c2-
|
|
|
|
}
|
|
|
|
|
2015-03-17 02:17:37 +00:00
|
|
|
capture_pane_contents_option_on() {
|
|
|
|
local option="$(get_tmux_option "$pane_contents_option" "off")"
|
|
|
|
[ "$option" == "on" ]
|
|
|
|
}
|
|
|
|
|
2017-05-30 03:14:14 +00:00
|
|
|
files_differ() {
|
|
|
|
! cmp -s "$1" "$2"
|
|
|
|
}
|
|
|
|
|
2015-02-09 01:11:35 +00:00
|
|
|
get_grouped_sessions() {
|
|
|
|
local grouped_sessions_dump="$1"
|
2015-02-09 15:34:24 +00:00
|
|
|
export GROUPED_SESSIONS="${d}$(echo "$grouped_sessions_dump" | cut -f2 -d"$d" | tr "\\n" "$d")"
|
2015-02-09 01:11:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
is_session_grouped() {
|
|
|
|
local session_name="$1"
|
|
|
|
[[ "$GROUPED_SESSIONS" == *"${d}${session_name}${d}"* ]]
|
|
|
|
}
|
|
|
|
|
2015-07-08 16:45:43 +00:00
|
|
|
# pane content file helpers
|
|
|
|
|
|
|
|
pane_contents_create_archive() {
|
2017-05-30 04:40:48 +00:00
|
|
|
tar cf - -C "$(resurrect_dir)/save/" ./pane_contents/ |
|
2015-07-10 17:29:40 +00:00
|
|
|
gzip > "$(pane_contents_archive_file)"
|
2015-07-08 16:45:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pane_content_files_restore_from_archive() {
|
|
|
|
local archive_file="$(pane_contents_archive_file)"
|
|
|
|
if [ -f "$archive_file" ]; then
|
2017-05-30 04:40:48 +00:00
|
|
|
mkdir -p "$(pane_contents_dir "restore")"
|
2015-07-10 17:29:40 +00:00
|
|
|
gzip -d < "$archive_file" |
|
2017-05-30 04:40:48 +00:00
|
|
|
tar xf - -C "$(resurrect_dir)/restore/"
|
2015-07-08 16:45:43 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2014-08-26 13:47:31 +00:00
|
|
|
# path helpers
|
|
|
|
|
2014-08-29 16:59:14 +00:00
|
|
|
resurrect_dir() {
|
2017-06-01 15:03:39 +00:00
|
|
|
if [ -z "$_RESURRECT_DIR" ]; then
|
2023-07-07 03:25:04 +00:00
|
|
|
local path="$(get_tmux_option "$resurrect_dir_option" "$default_resurrect_dir")/$(basename $(tmux display-message -p -F "#{socket_path}"))"
|
2017-06-01 15:18:39 +00:00
|
|
|
# expands tilde, $HOME and $HOSTNAME if used in @resurrect-dir
|
|
|
|
echo "$path" | sed "s,\$HOME,$HOME,g; s,\$HOSTNAME,$(hostname),g; s,\~,$HOME,g"
|
2017-06-01 15:03:39 +00:00
|
|
|
else
|
|
|
|
echo "$_RESURRECT_DIR"
|
|
|
|
fi
|
2014-08-26 13:40:50 +00:00
|
|
|
}
|
2017-06-01 15:18:39 +00:00
|
|
|
_RESURRECT_DIR="$(resurrect_dir)"
|
2014-08-26 13:40:50 +00:00
|
|
|
|
2014-08-29 16:59:14 +00:00
|
|
|
resurrect_file_path() {
|
2017-06-01 15:03:39 +00:00
|
|
|
if [ -z "$_RESURRECT_FILE_PATH" ]; then
|
2018-09-11 11:23:58 +00:00
|
|
|
local timestamp="$(date +"%Y%m%dT%H%M%S")"
|
2017-06-03 22:31:17 +00:00
|
|
|
echo "$(resurrect_dir)/${RESURRECT_FILE_PREFIX}_${timestamp}.${RESURRECT_FILE_EXTENSION}"
|
2017-06-01 15:03:39 +00:00
|
|
|
else
|
|
|
|
echo "$_RESURRECT_FILE_PATH"
|
|
|
|
fi
|
2014-08-26 13:31:47 +00:00
|
|
|
}
|
2017-06-01 15:18:39 +00:00
|
|
|
_RESURRECT_FILE_PATH="$(resurrect_file_path)"
|
2014-08-26 13:31:47 +00:00
|
|
|
|
2014-08-29 16:59:14 +00:00
|
|
|
last_resurrect_file() {
|
|
|
|
echo "$(resurrect_dir)/last"
|
2014-08-26 13:31:47 +00:00
|
|
|
}
|
2014-10-12 22:47:44 +00:00
|
|
|
|
2015-07-07 22:14:40 +00:00
|
|
|
pane_contents_dir() {
|
2017-05-30 04:40:48 +00:00
|
|
|
echo "$(resurrect_dir)/$1/pane_contents/"
|
2015-07-07 22:14:40 +00:00
|
|
|
}
|
|
|
|
|
2015-07-07 21:35:00 +00:00
|
|
|
pane_contents_file() {
|
2017-05-30 04:40:48 +00:00
|
|
|
local save_or_restore="$1"
|
|
|
|
local pane_id="$2"
|
|
|
|
echo "$(pane_contents_dir "$save_or_restore")/pane-${pane_id}"
|
2015-03-17 02:17:37 +00:00
|
|
|
}
|
|
|
|
|
2015-07-07 21:57:11 +00:00
|
|
|
pane_contents_file_exists() {
|
|
|
|
local pane_id="$1"
|
2017-05-30 04:40:48 +00:00
|
|
|
[ -f "$(pane_contents_file "restore" "$pane_id")" ]
|
2015-07-07 21:57:11 +00:00
|
|
|
}
|
|
|
|
|
2015-07-08 16:45:43 +00:00
|
|
|
pane_contents_archive_file() {
|
|
|
|
echo "$(resurrect_dir)/pane_contents.tar.gz"
|
|
|
|
}
|
|
|
|
|
2018-08-17 19:38:33 +00:00
|
|
|
execute_hook() {
|
|
|
|
local kind="$1"
|
|
|
|
shift
|
|
|
|
local args="" hook=""
|
2018-01-19 16:36:13 +00:00
|
|
|
|
2018-08-17 19:38:33 +00:00
|
|
|
hook=$(get_tmux_option "$hook_prefix$kind" "")
|
2018-01-19 16:36:13 +00:00
|
|
|
|
2018-08-17 19:38:33 +00:00
|
|
|
# If there are any args, pass them to the hook (in a way that preserves/copes
|
|
|
|
# with spaces and unusual characters.
|
|
|
|
if [ "$#" -gt 0 ]; then
|
|
|
|
printf -v args "%q " "$@"
|
|
|
|
fi
|
2018-01-19 16:36:13 +00:00
|
|
|
|
2018-08-17 19:38:33 +00:00
|
|
|
if [ -n "$hook" ]; then
|
|
|
|
eval "$hook $args"
|
|
|
|
fi
|
|
|
|
}
|