# sss
# Reattach a GNU Screen session, or a TMux session.
#
# For GNU Screen a custom lock tool (LOCKPRG) can be used
# if ~/bin/ttylocker exists or TTYLOCKER is set (not yet
# implemented for tmux).
# Mikael Berthe, 2015-08
function sss() {
local TTYLOCKER="${TTYLOCKER:-$HOME/bin/ttylocker}"
local SCREENBIN="/usr/bin/screen"
local TMUXBIN="/usr/bin/tmux"
local session="$1"
if [[ -x $SCREENBIN ]]; then
$SCREENBIN -q -list $session
local r=$?
if (( r == 11 )); then
# There is 1 usable Screen session, let's go
[[ -x "$TTYLOCKER" ]] && export LOCKPRG="$TTYLOCKER"
exec $SCREENBIN -r -x $session
elif (( r > 11 )); then
# There are several usable Screen sessions, heh
echo "There are several usable Screen sessions..."
return
fi
fi
# No usable GNU/Screen session, let's try tmux then.
if [[ -x "$TMUXBIN" ]]; then
if [[ -n $session ]]; then
$TMUXBIN has-session -t "$session" &&
exec $TMUXBIN attach-session -t "$1"
return # Exit function with error code
fi
# Number of tmux sessions
integer n=$($TMUXBIN list-sessions 2>/dev/null | wc -l)
if (( n == 1 )); then
# Attach the session
exec $TMUXBIN attach # -t "$($TMUXBIN list-session -F '#{session_id}')"
elif (( n > 1 )); then
# There are several usable tmux sessions, heh
echo "There are several usable tmux sessions..."
$TMUXBIN list-session
return
fi
fi
echo "No terminal multiplexer session found."
}
# From http://stackoverflow.com/questions/7814612/is-there-any-way-to-redraw-tmux-window-when-switching-smaller-monitor-to-bigger
# A big thank-you to Chris Johnsen for this one.
# (Code slightly modified - Mikael)
function tmux_takeover() {
# create a temporary session that displays the "how to go back" message
tmp='takeover_temp_session'
if ! tmux has-session -t "$tmp" >/dev/null 2>&1; then
tmux new-session -d -s "$tmp"
tmux set-option -t "$tmp" set-remain-on-exit on >/dev/null
tmux new-window -kt "$tmp":0 \
'echo -e "\nUse Prefix + L (i.e. ^B L) to return to session."'
fi
# switch any clients attached to the target session to the temp session
session="$1"
for client in $(tmux list-clients -t "$session" -F '#{client_tty}'); do
tmux switch-client -c "$client" -t "$tmp"
done
# attach to the target session
tmux attach -t "$session"
}