summaryrefslogtreecommitdiff
path: root/scripts
diff options
context:
space:
mode:
Diffstat (limited to 'scripts')
-rwxr-xr-xscripts/tmux-popup-attach57
-rwxr-xr-xscripts/tmux-popup-new16
2 files changed, 73 insertions, 0 deletions
diff --git a/scripts/tmux-popup-attach b/scripts/tmux-popup-attach
new file mode 100755
index 0000000..d295d52
--- /dev/null
+++ b/scripts/tmux-popup-attach
@@ -0,0 +1,57 @@
+#!/bin/sh
+
+set -eu
+
+if [ "$#" -lt 1 ] || [ "$#" -gt 3 ]; then
+ echo "usage: tmux-popup-attach SESSION [DIRECTORY]" >&2
+ echo " tmux-popup-attach --basename DIRECTORY SESSION_SUFFIX" >&2
+ exit 2
+fi
+
+if [ "$1" = "--basename" ]; then
+ if [ "$#" -ne 3 ]; then
+ echo "usage: tmux-popup-attach --basename DIRECTORY SESSION_SUFFIX" >&2
+ exit 2
+ fi
+
+ directory=$2
+ session=$(basename "$directory")$3
+else
+ if [ "$#" -gt 2 ]; then
+ echo "usage: tmux-popup-attach SESSION [DIRECTORY]" >&2
+ exit 2
+ fi
+
+ session=$1
+ directory=${2:-}
+fi
+
+if [ -n "${TMUX:-}" ]; then
+ tmux switch-client -t "=$session" 2>/dev/null && exit 0
+else
+ tmux attach-session -t "=$session" 2>/dev/null && exit 0
+fi
+
+tmux list-sessions -F '#{session_name}:#{session_attached}' 2>/dev/null |
+ while IFS=: read -r name attached; do
+ case $name in
+ T*)
+ if [ "$attached" = 0 ]; then
+ echo "Killing $name"
+ tmux kill-session -t "=$name" || true
+ fi
+ ;;
+ esac
+ done
+
+if [ -n "$directory" ]; then
+ tmux new-session -d -s "$session" -c "$directory" 2>/dev/null || true
+else
+ tmux new-session -d -s "$session" 2>/dev/null || true
+fi
+
+if [ -n "${TMUX:-}" ]; then
+ exec tmux -2 switch-client -t "=$session"
+fi
+
+exec tmux -2 attach-session -t "=$session"
diff --git a/scripts/tmux-popup-new b/scripts/tmux-popup-new
new file mode 100755
index 0000000..ebb1800
--- /dev/null
+++ b/scripts/tmux-popup-new
@@ -0,0 +1,16 @@
+#!/bin/sh
+
+set -eu
+
+if [ "$#" -lt 3 ]; then
+ echo "usage: tmux-popup-new DIRECTORY SESSION_SUFFIX COMMAND [ARG ...]" >&2
+ exit 2
+fi
+
+directory=$1
+suffix=$2
+shift 2
+
+session=$(basename "$directory")$suffix
+
+exec tmux new-session -A -s "$session" -c "$directory" "$@"