feat: Updates tmux-sessionator to accept a directory option and updates tns function to use that option.

This commit is contained in:
2024-12-03 08:56:56 -05:00
parent 9e48ba0a77
commit 82341d87df
2 changed files with 16 additions and 33 deletions

View File

@@ -8,9 +8,11 @@ debug="${DEBUG}"
declare -a paths=() declare -a paths=()
declare chooseOpt= declare chooseOpt=
declare -a directory=()
zparseopts -D -- \ zparseopts -D -- \
{c,-choose}=chooseOpt {c,-choose}=chooseOpt \
{d,-directory}:=directory
#################### Helpers #################### #################### Helpers ####################
@@ -42,6 +44,7 @@ function setup_fuzzy_find_paths() {
#################### MAIN #################### #################### MAIN ####################
declare choose="${chooseOpt[-1]}" declare choose="${chooseOpt[-1]}"
declare selected=
if [[ -n $choose ]]; then if [[ -n $choose ]]; then
debug_print "Choose from existing." debug_print "Choose from existing."
@@ -49,6 +52,14 @@ if [[ -n $choose ]]; then
tmux list-sessions -F \#S | gum filter \ tmux list-sessions -F \#S | gum filter \
--placeholder "Pick a session..." --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 elif [[ "$#" -eq 1 ]]; then
debug_print "Using existing session: $1" debug_print "Using existing session: $1"
selected=$1 selected=$1

View File

@@ -9,37 +9,9 @@
# the current working directory. # the current working directory.
function tns() { function tns() {
local directory=$1
local session_name= if [ -n "$directory" ]; then
local tmux_dir= directory=${PWD}
# 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
fi fi
tmux-sessionator --directory "$directory"
# 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"
} }