feat(ui): press `<c-c>` to abort any running tasks. Fixes #258

This commit is contained in:
Folke Lemaitre 2022-12-31 10:38:03 +01:00
parent ed0583e82b
commit d6b5d6e756
No known key found for this signature in database
GPG Key ID: 41F8B1FBACAE2040
4 changed files with 69 additions and 4 deletions

View File

@ -2,6 +2,44 @@ local Config = require("lazy.core.config")
local M = {} local M = {}
---@type table<vim.loop.Process, true>
M.running = {}
M.signals = {
"HUP",
"INT",
"QUIT",
"ILL",
"TRAP",
"ABRT",
"BUS",
"FPE",
"KILL",
"USR1",
"SEGV",
"USR2",
"PIPE",
"ALRM",
"TERM",
"CHLD",
"CONT",
"STOP",
"TSTP",
"TTIN",
"TTOU",
"URG",
"XCPU",
"XFSZ",
"VTALRM",
"PROF",
"WINCH",
"IO",
"PWR",
"EMT",
"SYS",
"INFO",
}
---@diagnostic disable-next-line: no-unknown ---@diagnostic disable-next-line: no-unknown
local uv = vim.loop local uv = vim.loop
@ -14,6 +52,7 @@ local uv = vim.loop
---@field env? string[] ---@field env? string[]
---@param opts? ProcessOpts ---@param opts? ProcessOpts
---@param cmd string
function M.spawn(cmd, opts) function M.spawn(cmd, opts)
opts = opts or {} opts = opts or {}
opts.timeout = opts.timeout or (Config.options.git and Config.options.git.timeout * 1000) opts.timeout = opts.timeout or (Config.options.git and Config.options.git.timeout * 1000)
@ -44,9 +83,8 @@ function M.spawn(cmd, opts)
if opts.timeout then if opts.timeout then
timeout = uv.new_timer() timeout = uv.new_timer()
timeout:start(opts.timeout, 0, function() timeout:start(opts.timeout, 0, function()
if handle and not handle:is_closing() then if M.kill(handle) then
killed = true killed = true
uv.process_kill(handle, "sigint")
end end
end) end)
end end
@ -57,6 +95,7 @@ function M.spawn(cmd, opts)
cwd = opts.cwd, cwd = opts.cwd,
env = env, env = env,
}, function(exit_code, signal) }, function(exit_code, signal)
M.running[handle] = nil
if timeout then if timeout then
timeout:stop() timeout:stop()
timeout:close() timeout:close()
@ -74,6 +113,8 @@ function M.spawn(cmd, opts)
output = output:gsub("[^\r\n]+\r", "") output = output:gsub("[^\r\n]+\r", "")
if killed then if killed then
output = output .. "\n" .. "Process was killed because it reached the timeout" output = output .. "\n" .. "Process was killed because it reached the timeout"
elseif signal ~= 0 then
output = output .. "\n" .. "Process was killed with SIG" .. M.signals[signal]
end end
vim.schedule(function() vim.schedule(function()
@ -89,6 +130,7 @@ function M.spawn(cmd, opts)
end end
return return
end end
M.running[handle] = true
---@param data? string ---@param data? string
local function on_output(err, data) local function on_output(err, data)
@ -112,6 +154,20 @@ function M.spawn(cmd, opts)
return handle return handle
end end
function M.kill(handle)
if handle and not handle:is_closing() then
M.running[handle] = nil
uv.process_kill(handle, "sigint")
return true
end
end
function M.abort()
for handle in pairs(M.running) do
M.kill(handle)
end
end
---@param cmd string[] ---@param cmd string[]
---@param opts? {cwd:string, env:table} ---@param opts? {cwd:string, env:table}
function M.exec(cmd, opts) function M.exec(cmd, opts)

View File

@ -31,6 +31,7 @@ M.keys = {
details = "<cr>", details = "<cr>",
profile_sort = "<C-s>", profile_sort = "<C-s>",
profile_filter = "<C-f>", profile_filter = "<C-f>",
abort = "<C-c>",
} }
---@type table<string,LazyViewCommand> ---@type table<string,LazyViewCommand>

View File

@ -62,6 +62,11 @@ function M.create()
self:update() self:update()
end) end)
vim.keymap.set("n", ViewConfig.keys.abort, function()
require("lazy.manage.process").abort()
return "<c-c>"
end, { silent = true, buffer = self.buf, expr = true })
-- plugin details -- plugin details
self:on_key(ViewConfig.keys.details, function() self:on_key(ViewConfig.keys.details, function()
local plugin = self.render:get_plugin() local plugin = self.render:get_plugin()

View File

@ -154,7 +154,9 @@ end
function M:help() function M:help()
self:append("Help", "LazyH2"):nl():nl() self:append("Help", "LazyH2"):nl():nl()
self:append("You can press "):append("<CR>", "LazySpecial"):append(" on a plugin to show its details."):nl() self:append("Use "):append("<C-c>", "LazySpecial"):append(" to abort all running tasks."):nl():nl()
self:append("You can press "):append("<CR>", "LazySpecial"):append(" on a plugin to show its details."):nl():nl()
self:append("Most properties can be hovered with ") self:append("Most properties can be hovered with ")
self:append("<K>", "LazySpecial") self:append("<K>", "LazySpecial")
@ -164,7 +166,8 @@ function M:help()
:append("<K>", "LazySpecial") :append("<K>", "LazySpecial")
:append(" on a plugin anywhere else, a diff will be opened if there are updates") :append(" on a plugin anywhere else, a diff will be opened if there are updates")
:nl() :nl()
self:append("or the plugin was just updated. Otherwise the plugin webpage will open."):nl() self:append("or the plugin was just updated. Otherwise the plugin webpage will open."):nl():nl()
self:append("Use "):append("<d>", "LazySpecial"):append(" on a commit or plugin to open the diff view"):nl() self:append("Use "):append("<d>", "LazySpecial"):append(" on a commit or plugin to open the diff view"):nl()
self:nl() self:nl()