From c072a0e784fcaf40b2028d7f21824873d6fd789b Mon Sep 17 00:00:00 2001 From: Huck Boles Date: Sun, 25 Sep 2022 12:10:39 -0500 Subject: [PATCH] Autoload single session --- README.md | 5 ++++- zshmux.zsh | 28 +++++++++++++++++++++++----- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 30553eb..d50c432 100644 --- 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 /zshmux/zshmux.zsh` +-Source *zshmux* in your .zshrc: `[[ -e /zshmux/zshmux.zsh ]] && . /zshmux/zshmux.zsh` -Open a new shell + ![zshmux](https://user-images.githubusercontent.com/80217378/192125380-151a4b09-f2ef-4fac-ae34-743cadcc1709.gif) diff --git a/zshmux.zsh b/zshmux.zsh index 54ef7cd..55adbe0 100644 --- a/zshmux.zsh +++ b/zshmux.zsh @@ -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 -- 2.45.2