Updated launch-in-tmux
* Don't exit shells, just the function * Don't leave dangling tmux sessions if possible * Restore original session/window name if using an active tmux session
This commit is contained in:
parent
a053931c17
commit
b7c790438a
1 changed files with 18 additions and 11 deletions
|
|
@ -13,16 +13,16 @@ function ask() {
|
|||
done
|
||||
}
|
||||
|
||||
die () {
|
||||
function err () {
|
||||
echo "$0:" "$@" >&2
|
||||
exit 1
|
||||
return 1
|
||||
}
|
||||
|
||||
function launch_in_tmux() {
|
||||
# Check for required vars
|
||||
[[ -n "${SESSION_NAME:-}" ]] || die "Required variable missing (SESSION_NAME)"
|
||||
[[ -n "${WINDOW_NAME:-}" ]] || die "Required variable missing (WINDOW_NAME)"
|
||||
[[ -n "${TMUX_CMD:-}" ]] || die "Required variable missing (TMUX_CMD)"
|
||||
[[ -n "${SESSION_NAME:-}" ]] || return $(err "Required variable missing (SESSION_NAME)")
|
||||
[[ -n "${WINDOW_NAME:-}" ]] || return $(err "Required variable missing (WINDOW_NAME)")
|
||||
[[ -n "${TMUX_CMD:-}" ]] || return $(err "Required variable missing (TMUX_CMD)")
|
||||
|
||||
# Check for running session
|
||||
if tmux list-session | grep -q "$SESSION_NAME"; then
|
||||
|
|
@ -32,11 +32,15 @@ function launch_in_tmux() {
|
|||
if [[ -n "${TMUX:-}" ]]; then
|
||||
# Running inside TMUX, switch to session
|
||||
tmux 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"
|
||||
fi
|
||||
exit 0
|
||||
return 0
|
||||
elif ask "Kill current session and start new session?"; then
|
||||
tmux kill-session -t "$SESSION_NAME" || \
|
||||
die "Failed to kill session: $SESSION_NAME"
|
||||
|
|
@ -45,18 +49,21 @@ function launch_in_tmux() {
|
|||
echo ""
|
||||
echo -n "Press Enter to exit... "
|
||||
read -r
|
||||
exit 0
|
||||
return 1
|
||||
fi
|
||||
fi
|
||||
|
||||
# Start/Rename session
|
||||
# Start session
|
||||
if [[ -n "${TMUX:-}" ]]; then
|
||||
# Running inside TMUX, rename session/window and open the menu
|
||||
# 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"
|
||||
"$TMUX_CMD" "$@"
|
||||
tmux rename-session "${SESSION_NAME}_DONE"
|
||||
tmux rename-window "${WINDOW_NAME}_DONE"
|
||||
# Restore previous session/window names
|
||||
tmux rename-session "${ORIGINAL_SESSION_NAME}"
|
||||
tmux rename-window "${ORIGINAL_WINDOW_NAME}"
|
||||
else
|
||||
# Running outside TMUX, start/attach to session
|
||||
tmux new-session -s "$SESSION_NAME" -n "$WINDOW_NAME" "$TMUX_CMD" "$@"
|
||||
|
|
|
|||
Loading…
Reference in a new issue