-
Hello, some of my pickers have actions that might take a few seconds to finish. Is there a way to show a small animation in the picker while the command is running (such as the spinning thingy in the |
Beta Was this translation helpful? Give feedback.
Answered by
yairh
Apr 22, 2025
Replies: 1 comment
-
I went and dug more in the code and used some inspiration using extmarks to write the following - this displays the spinner with some text in the input window of the picker. ---@param cmd table<string> The command and its arguments.
---@param picker snacks.Picker the picker involved
---@param name string? Name of the action to display on the picker.
---@return vim.SystemObj? The handle for the started system process, or nil if the process couldn't start immediately.
M.run_async_with_extmark = function(cmd, picker, name)
-- get the name of the picker action being run or infer it from the cmd
local text = name or table.concat(cmd, "-") or ""
local stimer = vim.uv.new_timer()
local extmark_id = 999
stimer:start(0, 100, vim.schedule_wrap(function()
-- set the extmark in a timer to start spinning
vim.api.nvim_buf_set_extmark(picker.input.win.buf, ns, 0, 0,
{
id = extmark_id,
virt_text = { { text .. " ", "SnacksPickerDimmed" }, { Snacks.util.spinner() .. " ", "SnacksPickerSpinner" } },
virt_text_pos = "right_align"
})
end))
local on_exit = function(obj)
stimer:stop()
stimer:close()
-- cannot be executed in fast context
vim.schedule(function()
vim.notify(obj.stdout, "info")
vim.api.nvim_buf_del_extmark(picker.input.win.buf, ns, extmark_id)
picker:find() -- refresh the picker
end)
end
-- run the command asynchronously
local handle = vim.system(cmd, { text = true }, on_exit)
if not handle then
vim.notify("could not run", "error")
end
return handle
end |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
yairh
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I went and dug more in the code and used some inspiration using extmarks to write the following - this displays the spinner with some text in the input window of the picker.