diff --git a/save_command_strategies/ps.sh b/save_command_strategies/ps.sh index 15bb5aa9..04683c13 100755 --- a/save_command_strategies/ps.sh +++ b/save_command_strategies/ps.sh @@ -10,11 +10,29 @@ 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() { - ps -ao "ppid,args" | - sed "s/^ *//" | - grep "^${PANE_PID}" | - cut -d' ' -f2- + # 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. + ps -p "${PANE_PID}" -o args | tail -n +2 } main() {