mirror of
https://github.com/m-housh/dotfiles.git
synced 2026-02-14 06:12:34 +00:00
feat: Moves local scripts directory. Handles systemd configurations.
This commit is contained in:
112
env/.local/scripts/tmux-sessionator
vendored
Executable file
112
env/.local/scripts/tmux-sessionator
vendored
Executable file
@@ -0,0 +1,112 @@
|
||||
#!/usr/bin/env zsh
|
||||
|
||||
# Adapted from: https://github.com/ThePrimeagen/.dotfiles/blob/master/bin/.local/scripts/tmux-sessionizer
|
||||
|
||||
debug="${DEBUG}"
|
||||
|
||||
#################### Options ####################
|
||||
|
||||
declare -a paths=()
|
||||
declare chooseOpt=
|
||||
declare -a directory=()
|
||||
|
||||
zparseopts -D -- \
|
||||
{c,-choose}=chooseOpt \
|
||||
{d,-directory}:=directory
|
||||
|
||||
#################### Helpers ####################
|
||||
|
||||
function debug_print {
|
||||
if [ -n "$debug" ]; then
|
||||
echo "DEBUG: $1"
|
||||
fi
|
||||
}
|
||||
|
||||
|
||||
function path_prepend() {
|
||||
declare arg
|
||||
for arg in "$@"; do
|
||||
debug_print "arg: $arg"
|
||||
[ -d $arg ] && debug_print "arg is a directory" && paths=($arg $paths) \
|
||||
|| debug_print "arg is not a directory"
|
||||
done
|
||||
}
|
||||
|
||||
function setup_fuzzy_find_paths() {
|
||||
local path="$TMUX_SESSIONATOR_PATH"
|
||||
debug_print "path: $path"
|
||||
for arg in ${(s[:])path}; do
|
||||
path_prepend "$arg"
|
||||
done
|
||||
debug_print "paths: $paths"
|
||||
}
|
||||
|
||||
function create_session() {
|
||||
local selected_name=$1
|
||||
local selected=$2
|
||||
|
||||
tmux new-session -ds "$selected_name" -c "$selected" -n "editor"
|
||||
tmux send-keys -t editor 'n' Enter
|
||||
tmux new-window -d -n "terminal" -c "$selected"
|
||||
tmux new-window -d -n 'files' -c "$selected"
|
||||
tmux send-keys -t files 'yazi' Enter
|
||||
}
|
||||
|
||||
#################### MAIN ####################
|
||||
|
||||
declare choose="${chooseOpt[-1]}"
|
||||
declare selected=
|
||||
|
||||
if [[ -n $choose ]]; then
|
||||
debug_print "Choose from existing."
|
||||
selected=$(
|
||||
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
|
||||
else
|
||||
setup_fuzzy_find_paths
|
||||
debug_print "fuzzy find paths: ${paths}"
|
||||
|
||||
if [ -n "$DEBUG" ]; then
|
||||
debug_print "Exiting because in debug mode."
|
||||
exit 0
|
||||
fi
|
||||
|
||||
selected=$(find ${paths} -mindepth 1 -maxdepth 1 -type d | fzf)
|
||||
fi
|
||||
|
||||
if [[ -z $selected ]]; then
|
||||
exit 0
|
||||
fi
|
||||
|
||||
selected_name=$(basename "$selected" | tr . _)
|
||||
tmux_running=$(pgrep tmux)
|
||||
|
||||
if [[ -z $TMUX ]] && [[ -z $tmux_running ]]; then
|
||||
create_session "$selected_name" "$selected"
|
||||
# tmux new-session -s "$selected_name" -c "$selected"
|
||||
# exit 0
|
||||
elif ! tmux has-session -t "$selected_name" 2> /dev/null; then
|
||||
create_session "$selected_name" "$selected"
|
||||
fi
|
||||
|
||||
# Create a session if it doesn't exist.
|
||||
# if ! tmux has-session -t "$selected_name" 2> /dev/null; then
|
||||
# tmux new-session -ds "$selected_name" -c "$selected" -n "editor"
|
||||
# tmux send-keys -t editor 'n' Enter
|
||||
# tmux new-window -d -n "terminal" -c "$selected"
|
||||
# fi
|
||||
|
||||
[ -z $TMUX ] && tmux attach -t "$selected_name" \
|
||||
|| tmux switch-client -t "$selected_name"
|
||||
Reference in New Issue
Block a user