]> git.huck.website - zshmux.git/commitdiff
Autoload single session
authorHuck Boles <huboles@protonmail.com>
Sun, 25 Sep 2022 17:10:39 +0000 (12:10 -0500)
committerHuck Boles <huboles@protonmail.com>
Sun, 25 Sep 2022 17:10:39 +0000 (12:10 -0500)
README.md
zshmux.zsh

index 30553eb84e552f90ce763ded80a94bd2bd37bd4e..d50c432b9156fd64955c1ee651331c9629d6264f 100644 (file)
--- a/README.md
+++ b/README.md
@@ -2,11 +2,14 @@
 
 If a new shell is launched, *zshmux* quickly checks if there are any tmux sessions, or if the current shell is already in a tmux instance.
 If you are not already in a tmux session *zshmux* will list all currently active sessions, and ask if you want to attach a currently running session, or make a new session in the shell.
+If there is only one tmux session running and not attached to any clients, *zshmux* just automatically attach the session to the current shell.
+
 
 ## How to use:
 -Clone this repository: `git clone https://github.com/huboles/zshmux`
 
--Source *zshmux* in your .zshrc: `source <path-to-zshmux>/zshmux/zshmux.zsh`
+-Source *zshmux* in your .zshrc: `[[ -e <path-to-zshmux>/zshmux/zshmux.zsh ]] && . <path-to-zshmux>/zshmux/zshmux.zsh`
 
 -Open a new shell
+
 ![zshmux](https://user-images.githubusercontent.com/80217378/192125380-151a4b09-f2ef-4fac-ae34-743cadcc1709.gif)
index 54ef7cd0de1be2c50fbc7cf71f03099c5bf32b06..55adbe02443208ec7b7623123cb10ad7658973a4 100644 (file)
@@ -1,19 +1,37 @@
 #!/bin/zsh
 
 if [[ -z $TMUX ]]; then
+        
     if tmux list-sessions &> /dev/null ; then
-        printf '\033[32;1mCurrent \033[33;1mtmux \033[32;1msessions\033[0m:\n'
-        tmux list-sessions -F"#S - (#{?session_attached,Attached,Not Attached})"
+
+        if [[ $(tmux list-sessions -F"#{?session_attached,,x}" | grep "x" | wc -l) -eq 1 ]]; then
+            tmux attach-session -t $(tmux list-sessions -F"#S")
+        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: '
+
+        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
-    printf '\033[34;1mAttach session?\033[0;m: [\033[35;1mname\033[0m] \033[37;2m(empty if none)\033[0m: '
+
     read session
-    if [ -n "$session" ]; then
-        if tmux list-sessions | grep -q "$session"; then
+
+    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
     fi
+
+    return
 fi