From 2b00827160131df2be7624af967d21b40520a180 Mon Sep 17 00:00:00 2001 From: Huck Boles Date: Mon, 10 Apr 2023 13:37:32 -0500 Subject: [PATCH] better tmux server handling --- README.md | 22 --------------------- zshmux.zsh | 57 ++++++++++++++++++++++++++++-------------------------- 2 files changed, 30 insertions(+), 49 deletions(-) delete mode 100644 README.md diff --git a/README.md b/README.md deleted file mode 100644 index c09e8ca..0000000 --- a/README.md +++ /dev/null @@ -1,22 +0,0 @@ -# zshmux - -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 will just automatically attach the session to the current shell. - -## How to get zshmux - -If you use Arch Linux, zshmux is available on the AUR - -Manual installation: - - Download and extract this archive - $ curl "https://download.huck.website/zshmux-[VERSION].tar.gz" - $ tar -xzvf zshmux-[VERSION].tar.gz - - Install: - $ cd zshmux-[VERSION] - # mkdir /usr/lib/zshmux - # cp zshmux.zsh /usr/lib/zshmux - -Once you've gotten zshmux, add this snippet to your zshrc: - [[ -d /usr/lib/zshmux ]] && source /usr/lib/zshmux/zshmux.zsh - --Open a new shell - diff --git a/zshmux.zsh b/zshmux.zsh index e51efa7..f82c848 100644 --- a/zshmux.zsh +++ b/zshmux.zsh @@ -1,37 +1,40 @@ #!/bin/zsh -if [[ -z $TMUX ]] && [[ -z $SSH_CLIENT ]]; 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 ]]; 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 -- 2.44.2