Fix running tmux in live macOS env

This commit is contained in:
2Shirt 2020-01-30 13:02:41 -07:00
parent 98032a0fed
commit 41130a38ed
Signed by: 2Shirt
GPG key ID: 152FAC923B0E132C

View file

@ -2,6 +2,12 @@
#
## Wizard Kit: TMUX Launcher
# Live macOS env workaround
tmux_args=()
if [[ -e "/.wk-live-macos" ]]; then
tmux_args=(-f "/etc/tmux.conf" -S "$(mktemp).socket")
fi
function ask() {
while :; do
read -p "$1 [Y/N] " -r answer
@ -25,24 +31,24 @@ function launch_in_tmux() {
[[ -n "${TMUX_CMD:-}" ]] || return $(err "Required variable missing (TMUX_CMD)")
# Check for running session
if tmux list-session | grep -q "$SESSION_NAME"; then
if tmux "${tmux_args[@]}" list-session | grep -q "$SESSION_NAME"; then
echo "WARNING: tmux session $SESSION_NAME already exists."
echo ""
if ask "Connect to current session?"; then
if [[ -n "${TMUX:-}" ]]; then
# Running inside TMUX, switch to session
tmux switch-client -t "$SESSION_NAME"
tmux "${tmux_args[@]}" switch-client -t "$SESSION_NAME"
if ! jobs %% >/dev/null 2>&1; then
# No running jobs, try exiting abandoned tmux session
exit 0
fi
else
# Running outside TMUX, attach to session
tmux attach-session -t "$SESSION_NAME"
tmux "${tmux_args[@]}" attach-session -t "$SESSION_NAME"
fi
return 0
elif ask "Kill current session and start new session?"; then
tmux kill-session -t "$SESSION_NAME" || \
tmux "${tmux_args[@]}" kill-session -t "$SESSION_NAME" || \
die "Failed to kill session: $SESSION_NAME"
else
echo "Aborted."
@ -53,16 +59,16 @@ function launch_in_tmux() {
# Start session
if [[ -n "${TMUX:-}" ]]; then
# Running inside TMUX, save current session/window names
ORIGINAL_SESSION_NAME="$(tmux display-message -p '#S')"
ORIGINAL_WINDOW_NAME="$(tmux display-message -p '#W')"
tmux rename-session "$SESSION_NAME"
tmux rename-window "$WINDOW_NAME"
ORIGINAL_SESSION_NAME="$(tmux "${tmux_args[@]}" display-message -p '#S')"
ORIGINAL_WINDOW_NAME="$(tmux "${tmux_args[@]}" display-message -p '#W')"
tmux "${tmux_args[@]}" rename-session "$SESSION_NAME"
tmux "${tmux_args[@]}" rename-window "$WINDOW_NAME"
"$TMUX_CMD" "$@"
# Restore previous session/window names
tmux rename-session "${ORIGINAL_SESSION_NAME}"
tmux rename-window "${ORIGINAL_WINDOW_NAME}"
tmux "${tmux_args[@]}" rename-session "${ORIGINAL_SESSION_NAME}"
tmux "${tmux_args[@]}" rename-window "${ORIGINAL_WINDOW_NAME}"
else
# Running outside TMUX, start/attach to session
tmux new-session -s "$SESSION_NAME" -n "$WINDOW_NAME" "$TMUX_CMD" "$@"
tmux "${tmux_args[@]}" new-session -s "$SESSION_NAME" -n "$WINDOW_NAME" "$TMUX_CMD" "$@"
fi
}