Skip to content

Commit

Permalink
feat: use jobstart for Neovim v0.11+ instead of termopen (#298)
Browse files Browse the repository at this point in the history
closes #295
  • Loading branch information
mikesmithgh authored Dec 31, 2024
1 parent 402a188 commit 2d24427
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
26 changes: 18 additions & 8 deletions lua/kitty-scrollback/kitty_commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand All @@ -139,23 +143,29 @@ 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,
stderr = stderr and table.concat(stderr, '\n') or nil,
}, 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

Expand Down
2 changes: 1 addition & 1 deletion lua/kitty-scrollback/launch.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2d24427

Please sign in to comment.