Ben Grande b38834d66b
fix: avoid operand evaluation as argument
Explicit end option parsing as the shell can be quite dangerous without
it.
2024-08-06 17:13:11 +02:00

136 lines
3.9 KiB
Bash
Executable File

#!/bin/sh
## SPDX-FileCopyrightText: 2023 Benjamin Grande M. S. <ben.grande.b@gmail.com>
##
## SPDX-License-Identifier: AGPL-3.0-or-later
set -eu
command -v tmux >/dev/null || exit 1
: "${TMUX:=}"
session="${1:-}"
## Attach to session if it exists or not and from inside tmux or not.
## If name is supplied and doesn't exist, create session with given name.
## If no name is supplied and there is last session, switch client to it,
## else ask for the user to choose the wanted session.
## usage: ta SESSION ARGS
## example: ta dotfiles -d
new(){
## Session name was not specified.
list="$(tmux list-sessions 2>/dev/null)"
printf '%s\n' "Choose session or create one by providing a new name:"
if test -n "${list}"; then
printf '\n%s\n' "${list}"
fi
printf '\n%s' "Enter session name: "
read -r name
if test -z "${name}"; then
printf '%s\n' "Name cannot be empty."
return 1
fi
}
main(){
if test -n "${TMUX}"; then
## Inside tmux.
if test "${session}" && tmux has-session -t "${session}" >/dev/null 2>&1
then
## Session name was specified and it exists.
tmux switch-client -t "${session}"
tmux display-message "Switched to session ${session}"
elif test -n "${session}"; then
## Session name was specified but it doesn't exist.
tmux new-session -d -s "${session}"
tmux switch-client -t "${session}"
tmux display-message "Created and switched to session ${session}"
else
## Session name was not specified.
last="$(tmux display-message -p "#{client_last_session}")"
if test -n "${last}" && tmux has-session -t "${last}"; then
## If tmux has a last session, use it.
tmux switch-client -t "${last}"
tmux display-message "Switched to session ${last}"
else
## No last session, choose one or create one.
printf '%s\n' "Last session not found."
new
if tmux has-session -t "${name}" 2>/dev/null; then
## Chosen session exists.
tmux switch-client -t "${name}"
tmux display-message "Switched to session ${name}"
else
## Chosen session doesn't exist.
tmux new-session -d -s "${name}"
tmux switch-client -t "${name}"
tmux display-message "Created and switched to session ${name}"
fi
fi
fi
else
## Outside of tmux.
if test -n "${session}" && ! tmux list-sessions >/dev/null 2>&1; then
## Session name was specified but server is not running.
tmux new-session -s "${session}"
tmux display-message "Created session ${session}"
return 0
elif ! tmux list-sessions >/dev/null 2>&1; then
## Server is not running.
new
tmux new-session -s "${name}"
tmux display-message "Created session ${name}"
return 0
fi
if test "${session}" && tmux has-session -t "${session}" >/dev/null 2>&1
then
## Session name was specified and it exists.
tmux attach-session -t "${session}"
tmux display-message "Attached to session ${session}"
elif test -n "${session}"; then
## Session name was specified but it doesn't exist.
tmux new-session -s "${session}"
tmux display-message "Created new session ${session}"
else
## Session name was not specified.
last="$(tmux display-message -p "#{session_name}")"
if test -n "${last}" && tmux has-session -t "${last}"; then
## If tmux has a session name, use it.
tmux attach-session -t "${last}"
tmux display-message "Attached to session ${last}"
else
new
if tmux has-session -t "${name}" 2>/dev/null; then
## Chosen session exists.
tmux attach-session -t "${name}"
tmux display-message "Attached to session ${name}"
else
## Chosen session doesn't exist.
tmux new-session -s "${name}"
tmux display-message "Created session ${name}"
fi
fi
fi
fi
}
main "${@}"