diff --git a/.config/helix/scripts/typst-zathura.fish b/.config/helix/scripts/typst-zathura.fish index edb3087e..e9a6d9f8 100755 --- a/.config/helix/scripts/typst-zathura.fish +++ b/.config/helix/scripts/typst-zathura.fish @@ -1,9 +1,34 @@ #!/usr/bin/env fish -set parent_pid (ps -o ppid= -p $fish_pid | string trim) -echo $parent_pid - argparse q/quiet -- $argv +# only return status 1 if -q not set +if set -q _flag_q + set ret 1 +else + set ret 0 +end + +# traverse up process tree to find caller Helix PID +function find_parent_process -a target + set current_pid $fish_pid + + while test $current_pid -ne 1 # Stop when we reach init (PID 1) + set parent (ps -o ppid= -p $current_pid | string trim) + set cmd (ps -o comm= -p $parent | string trim) + + if string match -q -- $target $cmd + echo "$parent" + return 0 + end + + set current_pid $parent + end + + return 1 +end + +set parent_pid (find_parent_process hx) + function zopen --wraps zathura zathura "$argv[1]" &>/dev/null & set zathura_pid $last_pid @@ -20,13 +45,6 @@ function find_pdf end end -# only return status 1 if -q not set -if set -q _flag_q - set ret 1 -else - set ret 0 -end - # only echo if -q not set function qecho if not set -q _flag_q