mzte-nv: TypstWatch: use fs events instead of timer to open viewer

This commit is contained in:
LordMZTE 2024-11-19 08:24:29 +01:00
parent 33e82ac1ac
commit 96a83236f4
Signed by: LordMZTE
GPG key ID: B64802DC33A64FF6

View file

@ -6,8 +6,8 @@
(let [Terminal (. (require :toggleterm.terminal) :Terminal)
lspc-util (require :lspconfig.util)
file (vim.api.nvim_buf_get_name 0)
outfile (.. :/tmp/
(string.gsub (vim.fs.basename file) "%.typ$" :.pdf))
outfile-base (string.gsub (vim.fs.basename file) "%.typ$" :.pdf)
outfile (.. :/tmp/ outfile-base)
root ((lspc-util.root_pattern :.typstroot) file)
term (Terminal:new {:direction :horizontal
:cmd (if root
@ -15,13 +15,14 @@
" " file " " outfile)
(.. "typst watch " file " "
outfile))})
viewer-timer (vim.uv.new_timer)]
(term:open)
(viewer-timer:start 2000 0
(fn []
(viewer-timer:stop)
(viewer-timer:close)
(vim.uv.spawn :zathura {:args [outfile]}
(fn [code signal]
(vim.schedule #(term:shutdown))))))))]
fsev (vim.uv.new_fs_event)
on-file-appear (fn [err file events]
(when (and (not err) (= file outfile-base))
(print :open)
(fsev:stop)
(vim.uv.spawn :zathura {:args [outfile]}
(fn [code signal]
(vim.schedule #(term:shutdown))))))]
(fsev:start :/tmp {} on-file-appear)
(term:open)))]
(vim.api.nvim_create_user_command :TypstWatch cb {:nargs 0}))