AutoYADM commit: 2025-04-22 18:00:05
This commit is contained in:
parent
31527457be
commit
5bb20dda7c
1 changed files with 42 additions and 28 deletions
|
@ -12,6 +12,8 @@ if test (count $argv) -eq 0; or set -q _flag_h
|
||||||
end
|
end
|
||||||
|
|
||||||
# only return status 1 if -q not set
|
# only return status 1 if -q not set
|
||||||
|
# (if return status != 0, helix will not
|
||||||
|
# display anything that was echoed to stdout!)
|
||||||
if set -q _flag_q
|
if set -q _flag_q
|
||||||
set ret 1
|
set ret 1
|
||||||
else
|
else
|
||||||
|
@ -20,33 +22,40 @@ end
|
||||||
|
|
||||||
# only echo if -q not set
|
# only echo if -q not set
|
||||||
function qecho
|
function qecho
|
||||||
if not set -q _flag_q
|
if test "$ret" -ne 1
|
||||||
echo $argv[1]
|
echo $argv[1]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# traverse up process tree to find caller Helix PID
|
# check if the user asked to kill zathura on helix exit
|
||||||
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
|
|
||||||
|
|
||||||
if set -q _flag_k
|
if set -q _flag_k
|
||||||
set kill_parent
|
set kill_parent
|
||||||
echo "kill flag set"
|
# traverse up process tree to find caller Helix PID
|
||||||
|
# We use this PID to kill zathura if Helix exits first!
|
||||||
|
# we only need to define this function
|
||||||
|
# inside this scope
|
||||||
|
function find_parent_process -a target
|
||||||
|
# initialize current_pid as this shell's pid
|
||||||
|
set -f current_pid $fish_pid
|
||||||
|
|
||||||
|
# Stop when we reach init (PID 1)
|
||||||
|
while test $current_pid -ne 1
|
||||||
|
# parent of current_pid
|
||||||
|
set parent (ps -o ppid= -p $current_pid | string trim)
|
||||||
|
# get parent's command name
|
||||||
|
set cmd (ps -o comm= -p $parent | string trim)
|
||||||
|
|
||||||
|
# if the cmd matches our target command we return its pid
|
||||||
|
if string match -q -- $target $cmd
|
||||||
|
echo "$parent"
|
||||||
|
return 0
|
||||||
|
end
|
||||||
|
|
||||||
|
set -f current_pid $parent
|
||||||
|
end
|
||||||
|
return 1
|
||||||
|
end
|
||||||
|
|
||||||
set parent_pid (find_parent_process hx)
|
set parent_pid (find_parent_process hx)
|
||||||
if test -z "$parent_pid"
|
if test -z "$parent_pid"
|
||||||
qecho "Couldn't find parent hx process!"
|
qecho "Couldn't find parent hx process!"
|
||||||
|
@ -54,20 +63,25 @@ if set -q _flag_k
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# opens zathura, watching for helix closing
|
# opens zathura, optionally watching for helix closing
|
||||||
function zopen --wraps zathura
|
function zopen --wraps zathura
|
||||||
|
# this should be set if the user asked to watch
|
||||||
if set -q kill_parent
|
if set -q kill_parent
|
||||||
|
# create background sub-process
|
||||||
|
# otherwise helix will hang
|
||||||
begin
|
begin
|
||||||
zathura "$argv[1]" &>/dev/null &
|
zathura "$argv[1]" &>/dev/null &
|
||||||
set zathura_pid $last_pid
|
set zathura_pid $last_pid
|
||||||
waitpid "$parent_pid"
|
waitpid -c 1 "$parent_pid" "$zathura_pid"
|
||||||
kill $zathura_pid
|
kill $zathura_pid &>/dev/null
|
||||||
end &
|
end &
|
||||||
else
|
else
|
||||||
|
# user didn't ask for watch, so open normally
|
||||||
zathura "$argv[1]" &>/dev/null &
|
zathura "$argv[1]" &>/dev/null &
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# try to find the target pdf file
|
||||||
function find_pdf
|
function find_pdf
|
||||||
set -l root $argv[1]
|
set -l root $argv[1]
|
||||||
set -l base $argv[2]
|
set -l base $argv[2]
|
||||||
|
@ -79,15 +93,18 @@ function find_pdf
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# absolute path of %{buffer_name} file
|
||||||
set -l src (path resolve $argv[1])
|
set -l src (path resolve $argv[1])
|
||||||
|
# exit if not a typst file
|
||||||
if not string match -q '*.typ' $src
|
if not string match -q '*.typ' $src
|
||||||
qecho "$(path basename $src) is not a Typst file!"
|
qecho "$(path basename $src) is not a Typst file!"
|
||||||
return $ret
|
return $ret
|
||||||
end
|
end
|
||||||
|
# change to pdf extension
|
||||||
set -l targ (string replace '.typ' '.pdf' $src)
|
set -l targ (string replace '.typ' '.pdf' $src)
|
||||||
set -l base (path basename --no-extension $src).pdf
|
set -l base (path basename --no-extension $src).pdf
|
||||||
|
|
||||||
if test -f "$targ" -a '' != ''
|
if test -f "$targ"
|
||||||
# zathura "$targ" &>/dev/null &
|
# zathura "$targ" &>/dev/null &
|
||||||
zopen "$targ"
|
zopen "$targ"
|
||||||
else
|
else
|
||||||
|
@ -95,11 +112,8 @@ else
|
||||||
# if we're in a git repo, search from root
|
# if we're in a git repo, search from root
|
||||||
# if we're not, search from cwd
|
# if we're not, search from cwd
|
||||||
if git rev-parse --is-inside-work-tree &>/dev/null
|
if git rev-parse --is-inside-work-tree &>/dev/null
|
||||||
# echo in git
|
|
||||||
set root (git rev-parse --show-toplevel)
|
set root (git rev-parse --show-toplevel)
|
||||||
# echo $root
|
|
||||||
else
|
else
|
||||||
echo out git
|
|
||||||
set root (pwd)
|
set root (pwd)
|
||||||
end
|
end
|
||||||
if not find_pdf "$root" "$base"
|
if not find_pdf "$root" "$base"
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue