diff --git a/lua/kitty-scrollback/kitty_commands.lua b/lua/kitty-scrollback/kitty_commands.lua index de0c0fdb..7e4fbeb2 100644 --- a/lua/kitty-scrollback/kitty_commands.lua +++ b/lua/kitty-scrollback/kitty_commands.lua @@ -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) @@ -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) @@ -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 @@ -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, @@ -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) diff --git a/lua/kitty-scrollback/launch.lua b/lua/kitty-scrollback/launch.lua index cc3bd39d..db501a9f 100644 --- a/lua/kitty-scrollback/launch.lua +++ b/lua/kitty-scrollback/launch.lua @@ -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() diff --git a/lua/kitty-scrollback/util.lua b/lua/kitty-scrollback/util.lua index 31a77eaa..7173b8c4 100644 --- a/lua/kitty-scrollback/util.lua +++ b/lua/kitty-scrollback/util.lua @@ -120,7 +120,35 @@ M.plug_mapping_names = { PASTE_CMD = '(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 '' @@ -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,