]> git.huck.website - zsh.git/commitdiff
zshmux symlink
authorHuck Boles <huck@huck.website>
Sat, 22 Apr 2023 13:10:38 +0000 (08:10 -0500)
committerHuck Boles <huck@huck.website>
Sat, 22 Apr 2023 13:10:38 +0000 (08:10 -0500)
include/zshmux.zsh

index 2bcad32c57b389b36b8d4b10bba69739913dc245..f82c848245bacd8d89ff74cb381c6fa5f3c91dbb 100644 (file)
@@ -1,37 +1,40 @@
 #!/bin/zsh
-if [[ -z $TMUX ]]; then
-if         
-    if [[ $(tmux list-sessions | wc -l) -ne 0 ]]; then
 
-        if [[ $(tmux list-sessions -F"#{?session_attached,,x}" | grep "x" | wc -l) -eq 1 ]] && [[ -z $SSH_CLIENT ]]; then
-            tmux attach-session -t $(tmux list-sessions -F"#{?session_attached,,#S}"  )
-            return
-        else
-            printf '\033[32;1mCurrent \033[33;1mtmux \033[32;1msessions\033[0m:\n'
-            tmux list-sessions -F"#S - (#{?session_attached,Attached,Not Attached})"
-            printf '\033[34;1mAttach session?\033[0;m: [\033[35;1mname\033[0m] \033[37;2m(empty if none)\033[0m: '
+# start tmux server if it's not already running
+[[ ! $(pgrep tmux) ]] && tmux start
 
-        fi
+# exit if already in a tmux session
+[[ -n $TMUX ]] && return
 
+# check if any sessions exit
+if [[ $(tmux list-sessions | wc -l) -ne 0 ]]; then
+    if [[ $(tmux list-sessions -F"#{?session_attached,,x}" | grep "x" | wc -l) -eq 1 ]] && [[ -z $SSH_CLIENT ]]; then
+        tmux attach-session -t $(tmux list-sessions -F"#{?session_attached,,#S}"  )
+        return
     else
-        printf '\033[34;1mNo \033[33;1mtmux \033[34;1msessions active\n'
-        printf '\033[34;1mCreate new session?\033[0;m: [\033[35;1mname\033[0m] \033[37;2m(empty if none)\033[0m: '
+        printf '\033[32;1mCurrent \033[33;1mtmux \033[32;1msessions\033[0m:\n'
+        tmux list-sessions -F"#S - (#{?session_attached,Attached,Not Attached})"
+        printf '\033[34;1mAttach session?\033[0;m: [\033[35;1mname\033[0m] \033[37;2m(empty if none)\033[0m: '
     fi
+else
+    printf '\033[34;1mNo \033[33;1mtmux \033[34;1msessions active\n'
+    printf '\033[34;1mCreate new session?\033[0;m: [\033[35;1mname\033[0m] \033[37;2m(empty if none)\033[0m: '
+fi
 
-    read session
+read session
 
-    if [[ -n $session ]]; then
-        if tmux list-sessions -F"#S" | grep -q "$session"; then
-            tmux attach-session -t $session 
-            return
-        elif [[ $session == new ]]; then
-            tmux new-session
-            return
-        else
-            tmux new-session -s $session
-            return
-        fi
+# start requested session - since it uses grep you just have to put enough of a unique identifier to select
+if [[ -n $session ]]; then
+    if tmux list-sessions -F"#S" | grep -q "$session"; then
+        tmux attach-session -t $session 
+        return
+    elif [[ $session == new ]]; then
+        tmux new-session
+        return
+    else
+        tmux new-session -s $session
+        return
     fi
-
-    return
 fi
+
+return