summary refs log tree commit diff
path: root/.vim
diff options
context:
space:
mode:
authorRobert Günzler <r@gnzler.io>2022-09-28 18:46:07 +0200
committerRobert Günzler <r@gnzler.io>2022-09-28 18:46:07 +0200
commita8b08209a6b789cbe15e8258a46e2d5ae028aa17 (patch)
tree45ffd2c6138cf102fa9a23e85c4999acce59b0f3 /.vim
parent3f2bd15152bbef02241b929c28cd9030c7336215 (diff)
vim: make link_preview async
Diffstat (limited to '.vim')
-rw-r--r--.vim/lua/misc.lua35
1 files changed, 19 insertions, 16 deletions
diff --git a/.vim/lua/misc.lua b/.vim/lua/misc.lua
index 37ef854..6c642f4 100644
--- a/.vim/lua/misc.lua
+++ b/.vim/lua/misc.lua
@@ -148,28 +148,31 @@ function M.fold_block() -- {{{
   vim.api.nvim_win_set_cursor(0, inital_cusor_pos)
 end -- }}}
 
-local function render_link_preview(buf, ln, link)
-  local j = require('plenary.job'):new({
-    command = vim.env.SHELL,
-    args = {'-c', [[ curl -sSfL ]] .. link .. [[ | grep -iPo '(?<=property="og:title" content=")([^\"]+)(?=")' ]]},
-    enable_recording = true,
-  })
-  local results, code = j:sync()
-  if not code == 0 or #results == 0 then return end
-
-  local ns = vim.api.nvim_create_namespace('')
-  vim.api.nvim_buf_set_extmark(buf, ns, ln, 0, {
-    virt_text = {{results[1], 'Error'}},
-    virt_text_pos = 'eol'
-  })
-end
 function M.link_preview()
   local link = vim.fn.expand('<cfile>')
   if not link then return end
 
+  local buf = vim.api.nvim_get_current_buf()
   local ln = (vim.api.nvim_win_get_cursor(0)[1]) - 1
 
-  render_link_preview(0, ln, link)
+  local j = require('job').jobstart({
+    format_title = function() return "link-preview"  end,
+    command = vim.env.SHELL,
+    args = {'-c', [[ curl -sSfL ]] .. link .. [[ | grep -iPo '(?<=property="og:title" content=")([^\"]+)(?=")' ]]},
+    populate_quickfix = false,
+    enable_recording = true,
+  })
+
+  j:after_success(vim.schedule_wrap(function(j, code, _)
+      local results = j:result()
+      if not code == 0 or #results == 0 then return end
+
+      local ns = vim.api.nvim_create_namespace('')
+      vim.api.nvim_buf_set_extmark(buf, ns, ln, 0, {
+        virt_text = {{results[1], 'Error'}},
+        virt_text_pos = 'eol'
+      })
+  end))
 end
 
 return M