From 15a80a31ff56a24da9918a6bfcfe51aab809aa19 Mon Sep 17 00:00:00 2001 From: A Farzat Date: Tue, 29 Oct 2024 01:48:33 +0900 Subject: Add a script to run commands in tmux --- bin/scripts/tmux_cmd_dmenu | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100755 bin/scripts/tmux_cmd_dmenu diff --git a/bin/scripts/tmux_cmd_dmenu b/bin/scripts/tmux_cmd_dmenu new file mode 100755 index 0000000..407ab0d --- /dev/null +++ b/bin/scripts/tmux_cmd_dmenu @@ -0,0 +1,33 @@ +#!/usr/bin/env sh + +# A script to run a command in a tmux session of choice. +# The first argument is the session name. If empty the user uses dmenu to choose. +# If the session is already attached, the window is activated. +# Else a new terminal is opened attaching the session. + +# The default terminal is st. +TERMINAL="${TERMINAL:-st}" + +# If the first argument is empty use dmenu to obtain a session name. +if [ -n "$1" ]; then chosen="$1" +else chosen="$(tmux ls -F '#{session_name}' | dmenu -p "Choose which tmux session to use")" +fi + +# If no session was chosen then exit, else remove session_name from arg list. +if [ -z "$chosen" ]; then exit; fi +shift + +# If the session doesn't exist yet, create it running the provided program. +if ! tmux has-session -t "$chosen" 2> /dev/null +then exec $TERMINAL -T "tmux: $chosen" zshrc tmux new -s "$chosen" -- "$@" +fi + +# As the session exists, create a new window to run the program. +tmux neww -t "$chosen": -- "$@" +# Obtain the pid of the client running the target session. If it exists activate the window. +# Else attach the session to a new terminal. +client_pid="$(tmux lsc -F '#{session_name}:#{client_pid}' | session="$chosen" awk -F : '{ if ($1 == ENVIRON["session"]) print $2 }')" +if [ -n "$client_pid" ] +then xdotool windowactivate "$(xdotool search --pid "$(ps -o ppid= "$client_pid")")" +else exec $TERMINAL -T "tmux: $chosen" zshrc tmux attach -t "$chosen" +fi -- cgit v1.2.3-70-g09d2