summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorA Farzat <a@farzat.xyz>2025-10-29 18:12:27 +0300
committerA Farzat <a@farzat.xyz>2025-10-29 18:12:27 +0300
commite58812fa6ba1d213f18e8c78f3c8e42b8c6a88cd (patch)
tree748f6b22b676c28aed99c73457c7ece7e53ea6eb
parent93ce190f319bebb783b57b95ff34766431f75357 (diff)
downloaddotfiles-master.tar.gz
dotfiles-master.zip
Add a script to find tmux panes of given pidsHEADmaster
-rwxr-xr-xbin/scripts/find_tmux_pane44
1 files changed, 44 insertions, 0 deletions
diff --git a/bin/scripts/find_tmux_pane b/bin/scripts/find_tmux_pane
new file mode 100755
index 0000000..e8ad35b
--- /dev/null
+++ b/bin/scripts/find_tmux_pane
@@ -0,0 +1,44 @@
+#!/usr/bin/env sh
+
+# Usage:
+# pgrep <command> | xargs find_tmux_pane
+# Or if you already have the pids:
+# find_tmux_pane <pid1> <pid2> ...
+
+server_pid="$(tmux ls -F '#{pid}' | head -n 1)"
+pane_list="$(tmux lsp -aF '#{session_name}:#{window_index}.#{pane_index} #{pane_pid}')"
+
+get_pane_pid() {
+ current_pid="$pid"
+ if ! parent_pid="$(printf %d "$(ps -p "$current_pid" -o ppid=)")"
+ then return $?
+ fi
+ while [ "$parent_pid" != "$server_pid" ]
+ do
+ current_pid="$parent_pid"
+ parent_pid="$(printf %d "$(ps -p "$current_pid" -o ppid=)")"
+ if [ "$parent_pid" = 0 ]
+ then printf "%s is not a tmux process\n" "$pid" >&2; return 1
+ fi
+ done
+ printf %d "$current_pid"
+ return 0
+}
+
+get_pane_name() {
+ while IFS= read -r line
+ do
+ if [ "${line#* }" = "$1" ]
+ then printf %s "${line% *}"
+ fi
+ done <<-PANE_PID
+ $pane_list
+ PANE_PID
+}
+
+for pid in "$@"
+do
+ if pane_pid="$(get_pane_pid "$pid")"
+ then printf "%d\t%s\n" "$pid" "$(get_pane_name "$pane_pid")"
+ fi
+done