From 552534a863401558ba5d4a4c8a9467f46d58f4d4 Mon Sep 17 00:00:00 2001 From: jyn Date: Tue, 13 Aug 2024 19:44:57 -0400 Subject: [PATCH 1/2] support `tmux split-pane` when restoring processes with `ps` strategy normally, `ps.sh` takes the output of `tmux list-panes -F "#{pane_pid}"` and looks for all the children of that process with a controlling terminal, because that process is usually a shell. but if the process in a pane was spawned with `tmux split-pane`, it will have no parent process other than the tmux server itself. support this use case by falling back to the active process if the active process has no children with a controlling terminal. --- save_command_strategies/ps.sh | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/save_command_strategies/ps.sh b/save_command_strategies/ps.sh index 15bb5aa9..efc5efe1 100755 --- a/save_command_strategies/ps.sh +++ b/save_command_strategies/ps.sh @@ -11,10 +11,18 @@ exit_safely_if_empty_ppid() { } full_command() { - ps -ao "ppid,args" | + # normally the PID is a shell. return the child that has an associated controlling terminal. + child=$(ps -ao "ppid,args" | sed "s/^ *//" | grep "^${PANE_PID}" | - cut -d' ' -f2- + cut -d' ' -f2-) + if [ "$child" ]; then + printf %s "$child" + return + fi + # if this command was spawned with `tmux split-pane`, it has no parent shell. + # just return the args for the PID itself. + ps -p "${PANE_PID}" -o args | tail -n +2 } main() { From a6993c529d26a13a93a00b337423789be69f7c73 Mon Sep 17 00:00:00 2001 From: jyn Date: Thu, 22 Aug 2024 16:39:27 -0400 Subject: [PATCH 2/2] lol my last fix didn't work when the child inherits the controlling terminal look for the default command instead and then filter for its children --- save_command_strategies/ps.sh | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/save_command_strategies/ps.sh b/save_command_strategies/ps.sh index efc5efe1..04683c13 100755 --- a/save_command_strategies/ps.sh +++ b/save_command_strategies/ps.sh @@ -10,15 +10,25 @@ exit_safely_if_empty_ppid() { fi } +default_command() { + tmux_command=$(tmux show-option default-command) + tmux_shell=$(tmux show-option default-shell) + echo "${tmux_command:-${tmux_shell:-${SHELL:-/bin/sh}}}" +} + full_command() { - # normally the PID is a shell. return the child that has an associated controlling terminal. - child=$(ps -ao "ppid,args" | - sed "s/^ *//" | - grep "^${PANE_PID}" | - cut -d' ' -f2-) - if [ "$child" ]; then - printf %s "$child" - return + # get the absolute path and args of the running process + parent=$(ps -p "${PANE_PID}" -o args | tail -n +2) + if echo "$parent" | grep --quiet -E -- "-?$(basename "$(default_command)")"; then + # normally the PID is a shell. return the child that has an associated controlling terminal. + child=$(ps -ao "ppid,args" | + sed "s/^ *//" | + grep "^${PANE_PID}" | + cut -d' ' -f2-) + if [ "$child" ]; then + printf %s "$child" + return + fi fi # if this command was spawned with `tmux split-pane`, it has no parent shell. # just return the args for the PID itself.