chore(tmux): update configuration

This commit is contained in:
Yorick Barbanneau 2025-05-07 01:38:32 +02:00
parent 3f349f7230
commit 72f0ec1c2c
2 changed files with 43 additions and 19 deletions

View file

@ -22,8 +22,12 @@ debug() {
process_args() {
until [[ -z $1 ]]; do
case $1 in
-s | --session-name | --session)
SESSION="$2"
-i | --session-id | --id)
SESSION_ID="$2"
shift
;;
-n | --session-name | --name)
SESSION_NAME="$2"
shift
;;
-p | --project-name | --window)
@ -42,8 +46,7 @@ process_args() {
}
has_session() {
local session_name="${1}"
tmux has-session -t "${session_name}" 2>/dev/null
tmux has-session -t "${SESSION_NAME:-$SESSION_ID}" 2>/dev/null
}
get_project_path() {
@ -66,13 +69,14 @@ is_running_in_tmux() {
check_script_inputs() {
debug "check script inputs"
if [[ -z "$SESSION" ]]; then
error "You must provide a session name"
if [[ -z "$SESSION_ID" ]] && [[ -z "$SESSION_NAME" ]]; then
error "You must provide session ID or a session name"
exit 5
fi
SESSION_NAME=${SESSION_NAME:-$SESSION_ID}
}
process_commands() {
process_command() {
local input="$1"
debug "Process command '$input'"
local cmd arg
@ -92,19 +96,19 @@ process_commands() {
tmux_vsplit() {
local size="${1:-20}"
debug "execute vsplit with size '${size}'"
tmux split-window -t "$SESSION" -c "$CURRENT_PATH" -v -p "$size"
tmux split-window -t "${SESSION_NAME}" -c "$CURRENT_PATH" -v -p "$size"
}
tmux_hsplit() {
local size="${1:-20}"
debug "execute hsplit with size '${size}'"
tmux split-window -t "$SESSION" -c "$CURRENT_PATH" -h -p "$size"
tmux split-window "${SESSION_NAME}" -c "$CURRENT_PATH" -h -p "$size"
}
tmux_neww() {
local name="$1"
debug "execute neww with name '${name}'"
local command=(tmux new-window -t "${SESSION}:$" -c "$CURRENT_PATH" -a)
local command=(tmux new-window -t "${SESSION_NAME}" -c "$CURRENT_PATH" -a)
if [[ -n "$name" ]]; then
command+=(-n "$name")
fi
@ -117,7 +121,7 @@ tmux_run() {
if [[ -z "$shell_command" ]]; then
error "tmux-run need a command"
else
tmux send-keys -t "$SESSION" "$shell_command" "C-m"
tmux send-keys -t "${SESSION_NAME}" "$shell_command" "C-m"
fi
}
@ -127,21 +131,27 @@ tmux_project() {
get_project_path "$arg"
}
create_session() {
tmux new-session -ds "${SESSION_NAME}" -c "${CURRENT_PATH}" -n "${PROJECT}"
if [[ "$SESSION_NAME" == "$SESSION_ID" ]]; then
tmux rename-session -t "$SESSION_ID" "$SESSION_ID ${CURRENT_PATH##*/}"
fi
}
main() {
process_args "$@"
is_running_in_tmux
check_script_inputs
if ! has_session "$SESSION"; then
get_project_path "${PROJECT:-$SESSION}"
tmux new-session -ds "${SESSION}" -c "${CURRENT_PATH}" -n "${PROJECT}"
# Process commands passed with command line
if ! has_session "${SESSION_NAME}"; then
get_project_path "${PROJECT}"
create_session
# Process tmux commands passed with command line
for command in "${COMMANDS[@]}"; do
process_commands "$command"
process_command "$command"
done
fi
tmux switch-client -t "${SESSION}"
tmux switch-client -t "${SESSION_NAME:-$SESSION_ID}"
}
main "${@}"