diff --git a/scripts/scripts/tmux-sessionator b/scripts/scripts/tmux-sessionator index 2949278..252fa61 100755 --- a/scripts/scripts/tmux-sessionator +++ b/scripts/scripts/tmux-sessionator @@ -8,9 +8,11 @@ debug="${DEBUG}" declare -a paths=() declare chooseOpt= +declare -a directory=() zparseopts -D -- \ - {c,-choose}=chooseOpt + {c,-choose}=chooseOpt \ + {d,-directory}:=directory #################### Helpers #################### @@ -42,6 +44,7 @@ function setup_fuzzy_find_paths() { #################### MAIN #################### declare choose="${chooseOpt[-1]}" +declare selected= if [[ -n $choose ]]; then debug_print "Choose from existing." @@ -49,6 +52,14 @@ if [[ -n $choose ]]; then tmux list-sessions -F \#S | gum filter \ --placeholder "Pick a session..." ) +elif [ ${#directory} -gt 0 ]; then + debug_print "Using directory option." + selected=${directory[-1]} + if [ "$selected" = "." ] || [ "$selected" = "" ]; then + selected="${PWD}" + fi + debug_print "Directory: $selected" + elif [[ "$#" -eq 1 ]]; then debug_print "Using existing session: $1" selected=$1 diff --git a/zsh/config/functions/tns b/zsh/config/functions/tns index db6000e..4c0f5a8 100755 --- a/zsh/config/functions/tns +++ b/zsh/config/functions/tns @@ -9,37 +9,9 @@ # the current working directory. function tns() { - - local session_name= - local tmux_dir= - - # parse the session, if based on the directory of the argument passed in. - # otherwise use the current directory. - [ -n "$1" ] && \ - session_name=$(basename "$1" | tr . _) && \ - tmux_dir="$1" - - [ -z "$session_name" ] \ - && session_name=$(basename "$PWD" | tr . _) && \ - tmux_dir="$PWD" - - tmux_running=$(pgrep tmux) - - # check if tmux is not running / attached to a session. - # if not, then create a new session. - if [ -z $TMUX ] && [ -z $tmux_running ]; then - tmux new-session -s "$session_name" -c "$tmux_dir" && return 0 + local directory=$1 + if [ -n "$directory" ]; then + directory=${PWD} fi - - # create a session if it doesn't exist, in the background, so - # that we can switch sessions. - if ! tmux has-session -t "$session_name" 2> /dev/null; then - tmux new-session -ds "$session_name" -c "$tmux_dir" -n "editor" "$EDITOR ." - tmux new-window -d -n "terminal" -c "$tmux_dir" - fi - - # attach to the session or switch if it already exists. - [ -z $TMUX ] && \ - tmux attach -t "$session_name" \ - || tmux switch-client -t "$session_name" + tmux-sessionator --directory "$directory" }