From 96a83236f4ddb61731e6e794c96f651ac6d22ba9 Mon Sep 17 00:00:00 2001 From: LordMZTE Date: Tue, 19 Nov 2024 08:24:29 +0100 Subject: [PATCH] mzte-nv: TypstWatch: use fs events instead of timer to open viewer --- mzte-nv/conf/ftplugin/typst.fnl | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/mzte-nv/conf/ftplugin/typst.fnl b/mzte-nv/conf/ftplugin/typst.fnl index be6cff0..380699b 100644 --- a/mzte-nv/conf/ftplugin/typst.fnl +++ b/mzte-nv/conf/ftplugin/typst.fnl @@ -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}))