Skip to content

Commit

Permalink
fix: improve jobstart error handling (#299)
Browse files Browse the repository at this point in the history
closes #282
  • Loading branch information
mikesmithgh authored Jan 7, 2025
1 parent 9f4e068 commit d72d742
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 12 deletions.
16 changes: 8 additions & 8 deletions lua/kitty-scrollback/kitty_commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ local function defer_resize_term(min_cols)
return orig_columns
end

M.open_term_command = vim.fn.has('nvim-0.11') <= 0 and 'termopen' or 'jobstart'

---@param get_text_opts KsbKittyGetTextArguments
---@param on_exit_cb function
M.get_text_term = function(get_text_opts, on_exit_cb)
Expand All @@ -81,9 +83,8 @@ M.get_text_term = function(get_text_opts, on_exit_cb)
-- set the shell used to sh to avoid imcompatabiliies with other shells (e.g., nushell, fish, etc)
vim.o.shell = 'sh'

local open_term_fn = vim.fn.jobstart
local open_term_fn = vim.fn[M.open_term_command]
local open_term_options = {
term = true,
stdout_buffered = true,
stderr_buffered = true,
on_stdout = function(_, data)
Expand Down Expand Up @@ -115,7 +116,7 @@ M.get_text_term = function(get_text_opts, on_exit_cb)
end

if error_index > 0 then
ksb_util.display_error(scrollback_cmd, {
ksb_util.display_cmd_error(scrollback_cmd, {
entrypoint = 'open_term_fn() :: exit_code = 0 and error_index > 0',
full_cmd = full_cmd,
code = 1, -- exit code is not returned through pipe but we can assume 1 due to error message
Expand All @@ -142,7 +143,7 @@ M.get_text_term = function(get_text_opts, on_exit_cb)
:gsub([[\x1b\\]], '')
:gsub(';k=s', '')
or nil
ksb_util.display_error(full_cmd, {
ksb_util.display_cmd_error(full_cmd, {
entrypoint = 'open_term_fn() :: exit_code ~= 0',
code = exit_code,
channel_id = id,
Expand All @@ -152,14 +153,13 @@ M.get_text_term = function(get_text_opts, on_exit_cb)
end
end,
}
if vim.fn.has('nvim-0.11') <= 0 then
open_term_fn = vim.fn.termopen
open_term_options.term = nil
if M.open_term_command == 'jobstart' then
open_term_options.term = true
end

local success, error = pcall(open_term_fn, full_cmd, open_term_options)
if not success then
ksb_util.display_error(full_cmd, {
ksb_util.display_cmd_error(full_cmd, {
entrypoint = 'open_term_fn() :: pcall(open_term_fn) error returned',
stderr = error or nil,
}, error_header)
Expand Down
23 changes: 21 additions & 2 deletions lua/kitty-scrollback/launch.lua
Original file line number Diff line number Diff line change
Expand Up @@ -409,8 +409,27 @@ M.launch = function()
win = 0,
}
)
---@diagnostic disable-next-line: param-type-mismatch
vim.api.nvim_buf_delete(vim.fn.bufnr('#'), { force = true }) -- delete alt buffer after rename

local alternate_file_bufnr = vim.fn.bufnr('#')
if alternate_file_bufnr > 0 then
vim.api.nvim_buf_delete(alternate_file_bufnr, { force = true }) -- delete alt buffer after rename
else
ksb_util.display_error({
[[- ERROR alternate file not found]],
[[ `vim.fn.bufnr('#')` is ]]
.. alternate_file_bufnr
.. [[. Most likely `]]
.. ksb_kitty_cmds.open_term_command
.. [[` failed. ]],
[[ Please report the issue at https://github.com/mikesmithgh/kitty-scrollback.nvim/issues]],
[[ and provide the `KittyScrollbackCheckHealth` report.]],
})
ksb_api.close_kitty_loading_window()
if block_input_timer then
vim.fn.timer_stop(block_input_timer)
end
return
end

if opts.restore_options then
restore_orig_options()
Expand Down
32 changes: 30 additions & 2 deletions lua/kitty-scrollback/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,35 @@ M.plug_mapping_names = {
PASTE_CMD = '<Plug>(KsbPasteCmd)',
}

M.display_error = function(cmd, r, header)
M.display_error = function(msg)
local error_header = {
'',
'==============================================================================',
'kitty-scrollback.nvim',
'',
'A fatal error occurred ~',
}
msg = msg or {}
local error_bufid = vim.api.nvim_create_buf(false, true)
vim.api.nvim_buf_set_name(error_bufid, vim.fn.tempname() .. '.ksb_errorbuf')
vim.api.nvim_set_current_buf(error_bufid)
vim.o.conceallevel = 2
vim.o.concealcursor = 'n'
vim.o.foldenable = false
vim.api.nvim_set_option_value('filetype', 'checkhealth', {
buf = error_bufid,
})
M.restore_and_redraw()
local prompt_msg = 'kitty-scrollback.nvim: Fatal error, see logs.'
vim.api.nvim_buf_set_lines(error_bufid, 0, -1, false, vim.list_extend(error_header, msg))
M.restore_and_redraw()
local response = vim.fn.confirm(prompt_msg, '&Quit\n&Continue')
if response ~= 2 then
M.quitall()
end
end

M.display_cmd_error = function(cmd, r, header)
local msg = vim.list_extend({}, header or {})
local stdout = r.stdout or ''
local stderr = r.stderr or ''
Expand Down Expand Up @@ -203,7 +231,7 @@ M.system_handle_error = function(cmd, error_header, sys_opts, ignore_error)
local ok = result.code == 0

if not ignore_error and not ok then
M.display_error(table.concat(cmd, ' '), {
M.display_cmd_error(table.concat(cmd, ' '), {
entrypoint = 'vim.system()',
pid = proc.pid,
code = result.code,
Expand Down

0 comments on commit d72d742

Please sign in to comment.