fix(util.job): scroll to top when process exits

This commit is contained in:
Folke Lemaitre 2025-10-27 23:37:02 +01:00
parent b0644884ef
commit b5441575e0
No known key found for this signature in database
GPG key ID: 9B52594D560070AB

View file

@ -105,23 +105,8 @@ function Job:setup()
end
self.opts.on_stdout = wrap(on_output, self.opts.on_stdout)
self.opts.on_stderr = wrap(on_output, self.opts.on_stderr)
self.opts.on_exit = wrap(function(job_id, code)
if not self:buf_valid() then
return
end
self:emit()
if self.opts.on_lines then
self.opts.on_lines(job_id, self.lines)
end
if self.opts.term then
self:hide_process_exited()
end
if not self.killed and code ~= 0 then
self:error(
("Job exited with code `%s`"):format(code),
("\n- `vim.o.shell = %q`\n\nOutput:\n%s"):format(vim.o.shell, vim.trim(table.concat(self.lines, "\n")))
)
end
self.opts.on_exit = wrap(function(_, code)
self:on_exit(code)
end, self.opts.on_exit)
if not self.opts.term and not self.opts.ansi then
self.opts.on_line = self.opts.on_line or function(_, text, line)
@ -130,6 +115,40 @@ function Job:setup()
end
end
function Job:on_exit(code)
if not self:buf_valid() then
return
end
self:emit()
if self.opts.on_lines then
self.opts.on_lines(self.id, self.lines)
end
if self.opts.term then
self:hide_process_exited()
end
self:norm("gg")
if not self.killed and code ~= 0 then
self:error(
("Job exited with code `%s`"):format(code),
("\n- `vim.o.shell = %q`\n\nOutput:\n%s"):format(vim.o.shell, vim.trim(table.concat(self.lines, "\n")))
)
end
end
---@param cmd string
function Job:norm(cmd)
if not self:buf_valid() then
return
end
for _, win in ipairs(vim.fn.win_findbuf(self.buf)) do
vim.api.nvim_win_call(win, function()
vim.cmd("normal! " .. cmd)
end)
end
end
---@param text string
---@param line number
function Job:on_line(text, line)