Skip to content

Commit d72d742

Browse files
authored
fix: improve jobstart error handling (#299)
closes #282
1 parent 9f4e068 commit d72d742

File tree

3 files changed

+59
-12
lines changed

3 files changed

+59
-12
lines changed

lua/kitty-scrollback/kitty_commands.lua

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ local function defer_resize_term(min_cols)
6363
return orig_columns
6464
end
6565

66+
M.open_term_command = vim.fn.has('nvim-0.11') <= 0 and 'termopen' or 'jobstart'
67+
6668
---@param get_text_opts KsbKittyGetTextArguments
6769
---@param on_exit_cb function
6870
M.get_text_term = function(get_text_opts, on_exit_cb)
@@ -81,9 +83,8 @@ M.get_text_term = function(get_text_opts, on_exit_cb)
8183
-- set the shell used to sh to avoid imcompatabiliies with other shells (e.g., nushell, fish, etc)
8284
vim.o.shell = 'sh'
8385

84-
local open_term_fn = vim.fn.jobstart
86+
local open_term_fn = vim.fn[M.open_term_command]
8587
local open_term_options = {
86-
term = true,
8788
stdout_buffered = true,
8889
stderr_buffered = true,
8990
on_stdout = function(_, data)
@@ -115,7 +116,7 @@ M.get_text_term = function(get_text_opts, on_exit_cb)
115116
end
116117

117118
if error_index > 0 then
118-
ksb_util.display_error(scrollback_cmd, {
119+
ksb_util.display_cmd_error(scrollback_cmd, {
119120
entrypoint = 'open_term_fn() :: exit_code = 0 and error_index > 0',
120121
full_cmd = full_cmd,
121122
code = 1, -- exit code is not returned through pipe but we can assume 1 due to error message
@@ -142,7 +143,7 @@ M.get_text_term = function(get_text_opts, on_exit_cb)
142143
:gsub([[\x1b\\]], '')
143144
:gsub(';k=s', '')
144145
or nil
145-
ksb_util.display_error(full_cmd, {
146+
ksb_util.display_cmd_error(full_cmd, {
146147
entrypoint = 'open_term_fn() :: exit_code ~= 0',
147148
code = exit_code,
148149
channel_id = id,
@@ -152,14 +153,13 @@ M.get_text_term = function(get_text_opts, on_exit_cb)
152153
end
153154
end,
154155
}
155-
if vim.fn.has('nvim-0.11') <= 0 then
156-
open_term_fn = vim.fn.termopen
157-
open_term_options.term = nil
156+
if M.open_term_command == 'jobstart' then
157+
open_term_options.term = true
158158
end
159159

160160
local success, error = pcall(open_term_fn, full_cmd, open_term_options)
161161
if not success then
162-
ksb_util.display_error(full_cmd, {
162+
ksb_util.display_cmd_error(full_cmd, {
163163
entrypoint = 'open_term_fn() :: pcall(open_term_fn) error returned',
164164
stderr = error or nil,
165165
}, error_header)

lua/kitty-scrollback/launch.lua

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -409,8 +409,27 @@ M.launch = function()
409409
win = 0,
410410
}
411411
)
412-
---@diagnostic disable-next-line: param-type-mismatch
413-
vim.api.nvim_buf_delete(vim.fn.bufnr('#'), { force = true }) -- delete alt buffer after rename
412+
413+
local alternate_file_bufnr = vim.fn.bufnr('#')
414+
if alternate_file_bufnr > 0 then
415+
vim.api.nvim_buf_delete(alternate_file_bufnr, { force = true }) -- delete alt buffer after rename
416+
else
417+
ksb_util.display_error({
418+
[[- ERROR alternate file not found]],
419+
[[ `vim.fn.bufnr('#')` is ]]
420+
.. alternate_file_bufnr
421+
.. [[. Most likely `]]
422+
.. ksb_kitty_cmds.open_term_command
423+
.. [[` failed. ]],
424+
[[ Please report the issue at https://github.com/mikesmithgh/kitty-scrollback.nvim/issues]],
425+
[[ and provide the `KittyScrollbackCheckHealth` report.]],
426+
})
427+
ksb_api.close_kitty_loading_window()
428+
if block_input_timer then
429+
vim.fn.timer_stop(block_input_timer)
430+
end
431+
return
432+
end
414433

415434
if opts.restore_options then
416435
restore_orig_options()

lua/kitty-scrollback/util.lua

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,35 @@ M.plug_mapping_names = {
120120
PASTE_CMD = '<Plug>(KsbPasteCmd)',
121121
}
122122

123-
M.display_error = function(cmd, r, header)
123+
M.display_error = function(msg)
124+
local error_header = {
125+
'',
126+
'==============================================================================',
127+
'kitty-scrollback.nvim',
128+
'',
129+
'A fatal error occurred ~',
130+
}
131+
msg = msg or {}
132+
local error_bufid = vim.api.nvim_create_buf(false, true)
133+
vim.api.nvim_buf_set_name(error_bufid, vim.fn.tempname() .. '.ksb_errorbuf')
134+
vim.api.nvim_set_current_buf(error_bufid)
135+
vim.o.conceallevel = 2
136+
vim.o.concealcursor = 'n'
137+
vim.o.foldenable = false
138+
vim.api.nvim_set_option_value('filetype', 'checkhealth', {
139+
buf = error_bufid,
140+
})
141+
M.restore_and_redraw()
142+
local prompt_msg = 'kitty-scrollback.nvim: Fatal error, see logs.'
143+
vim.api.nvim_buf_set_lines(error_bufid, 0, -1, false, vim.list_extend(error_header, msg))
144+
M.restore_and_redraw()
145+
local response = vim.fn.confirm(prompt_msg, '&Quit\n&Continue')
146+
if response ~= 2 then
147+
M.quitall()
148+
end
149+
end
150+
151+
M.display_cmd_error = function(cmd, r, header)
124152
local msg = vim.list_extend({}, header or {})
125153
local stdout = r.stdout or ''
126154
local stderr = r.stderr or ''
@@ -203,7 +231,7 @@ M.system_handle_error = function(cmd, error_header, sys_opts, ignore_error)
203231
local ok = result.code == 0
204232

205233
if not ignore_error and not ok then
206-
M.display_error(table.concat(cmd, ' '), {
234+
M.display_cmd_error(table.concat(cmd, ' '), {
207235
entrypoint = 'vim.system()',
208236
pid = proc.pid,
209237
code = result.code,

0 commit comments

Comments
 (0)