From 2d244272544cceea84f1249fbbb94297d907bbc9 Mon Sep 17 00:00:00 2001 From: Mike <10135646+mikesmithgh@users.noreply.github.com> Date: Tue, 31 Dec 2024 16:28:05 -0500 Subject: [PATCH] feat: use jobstart for Neovim v0.11+ instead of termopen (#298) closes #295 --- lua/kitty-scrollback/kitty_commands.lua | 26 +++++++++++++++++-------- lua/kitty-scrollback/launch.lua | 2 +- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/lua/kitty-scrollback/kitty_commands.lua b/lua/kitty-scrollback/kitty_commands.lua index ade8afc2..de0c0fdb 100644 --- a/lua/kitty-scrollback/kitty_commands.lua +++ b/lua/kitty-scrollback/kitty_commands.lua @@ -74,12 +74,16 @@ M.get_text_term = function(get_text_opts, on_exit_cb) -- increase the number of columns temporary so that the width is used during the -- terminal command kitty @ get-text. this avoids hard wrapping lines to the -- current window size. Note: a larger min_cols appears to impact performance - -- defer is used as a timing workaround because this is expected to be called right before termopen + -- defer is used as a timing workaround because this is expected to be called right before + -- opening the terminal p.orig_columns = defer_resize_term(300) - -- set the shell used for termopen to sh to avoid imcompatabiliies with other shells (e.g., nushell, fish, etc) + -- set the shell used to sh to avoid imcompatabiliies with other shells (e.g., nushell, fish, etc) vim.o.shell = 'sh' - local success, error = pcall(vim.fn.termopen, full_cmd, { + + local open_term_fn = vim.fn.jobstart + local open_term_options = { + term = true, stdout_buffered = true, stderr_buffered = true, on_stdout = function(_, data) @@ -112,7 +116,7 @@ M.get_text_term = function(get_text_opts, on_exit_cb) if error_index > 0 then ksb_util.display_error(scrollback_cmd, { - entrypoint = 'termopen() :: exit_code = 0 and error_index > 0', + 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 channel_id = id, @@ -139,7 +143,7 @@ M.get_text_term = function(get_text_opts, on_exit_cb) :gsub(';k=s', '') or nil ksb_util.display_error(full_cmd, { - entrypoint = 'termopen() :: exit_code ~= 0', + entrypoint = 'open_term_fn() :: exit_code ~= 0', code = exit_code, channel_id = id, stdout = out, @@ -147,15 +151,21 @@ M.get_text_term = function(get_text_opts, on_exit_cb) }, error_header) end end, - }) + } + if vim.fn.has('nvim-0.11') <= 0 then + open_term_fn = vim.fn.termopen + open_term_options.term = nil + end + + local success, error = pcall(open_term_fn, full_cmd, open_term_options) if not success then ksb_util.display_error(full_cmd, { - entrypoint = 'termopen() :: pcall(vim.fn.termopen) error returned', + entrypoint = 'open_term_fn() :: pcall(open_term_fn) error returned', stderr = error or nil, }, error_header) end - -- restore the original shell after processing termopen + -- restore the original shell after processing vim.o.shell = p.orig_options.shell end diff --git a/lua/kitty-scrollback/launch.lua b/lua/kitty-scrollback/launch.lua index a5236f2f..cc3bd39d 100644 --- a/lua/kitty-scrollback/launch.lua +++ b/lua/kitty-scrollback/launch.lua @@ -380,7 +380,7 @@ M.launch = function() if no_buf_content then p.bufid = vim.api.nvim_get_current_buf() else - -- buffer must be empty for termopen, dashboard plugins may write to the first buffer before kitty-scrollback.nvim loads + -- buffer must be empty for the terminal, dashboard plugins may write to the first buffer before kitty-scrollback.nvim loads p.bufid = vim.api.nvim_create_buf(true, true) vim.api.nvim_set_current_buf(p.bufid) end