AutoYADM commit: 2025-04-23 18:45:05

This commit is contained in:
Daniel Fichtinger 2025-04-23 18:45:05 -04:00
parent 068b1da3c2
commit 8fb20348d6

View file

@ -9,10 +9,9 @@
# Author: Daniel Fichtinger <daniel@ficd.ca
# License: MIT
function hx-typ-zathura
argparse q/quiet k/kill-on-exit h/help -- $argv
argparse q/quiet k/kill-on-exit h/help -- $argv
if test (count $argv) -eq 0; or set -q _flag_h
if test (count $argv) -eq 0; or set -q _flag_h
echo "Helper script for opening Typst files from Helix in Zathura."\n
echo "Usage: (bind the following to a key)"
echo ':sh hx-typ-zathura.fish [opts] %{buffer_name}'\n
@ -26,26 +25,26 @@ function hx-typ-zathura
echo 'Author: Daniel Fichtinger <daniel@ficd.ca>'
echo 'License: MIT'
return 0
end
end
# 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
# 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
set ret 1
else
else
set ret 0
end
end
# only echo if -q not set
function qecho
# only echo if -q not set
function qecho
if test "$ret" -ne 1
echo $argv[1]
end
end
end
# check if the user asked to kill zathura on helix exit
if set -q _flag_k
# check if the user asked to kill zathura on helix exit
if set -q _flag_k
set kill_parent
# traverse up process tree to find caller Helix PID
# We use this PID to kill zathura if Helix exits first!
@ -78,10 +77,10 @@ function hx-typ-zathura
qecho "Couldn't find parent hx process!"
return $ret
end
end
end
# opens zathura, optionally watching for helix closing
function zopen --wraps zathura
# opens zathura, optionally watching for helix closing
function zopen --wraps zathura
# this should be set if the user asked to watch
if set -q kill_parent
# create background sub-process
@ -97,10 +96,10 @@ function hx-typ-zathura
zathura "$argv[1]" &>/dev/null &
end
true
end
end
# try to find the target pdf file searching from root
function find_pdf
# try to find the target pdf file searching from root
function find_pdf
set -l root $argv[1]
set -l base $argv[2]
set -l candidate (fd --no-ignore-vcs -F -1 "$base" "$root")
@ -109,24 +108,24 @@ function hx-typ-zathura
else
return 1
end
end
end
# absolute path of %{buffer_name} file
set -l src (path resolve $argv[1])
# exit if not a typst file
if not string match -q '*.typ' $src
# absolute path of %{buffer_name} file
set -l src (path resolve $argv[1])
# exit if not a typst file
if not string match -q '*.typ' $src
qecho "$(path basename $src) is not a Typst file!"
return $ret
end
# change abs path to pdf extension
set -l targ (string replace '.typ' '.pdf' $src)
# get pdf target's base name
set -l base (path basename --no-extension $src).pdf
end
# change abs path to pdf extension
set -l targ (string replace '.typ' '.pdf' $src)
# get pdf target's base name
set -l base (path basename --no-extension $src).pdf
# if a suitable pdf exists in the same dir, open it
if test -f "$targ"
# if a suitable pdf exists in the same dir, open it
if test -f "$targ"
zopen "$targ"
else
else
# no such file in current dir, time to search!
# if we're in a git repo, search from its root
# if we're not, search from cwd
@ -142,7 +141,4 @@ function hx-typ-zathura
echo "$base couldn't be found at root $root!"
end
end
end
end
hx-typ-zathura $argv