Skip to content

Commit

Permalink
Merge branch 'main' into config-refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
mikesmithgh committed Jan 10, 2024
2 parents af3a032 + b37611b commit ddbc903
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 71 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ jobs:
- name: Install dependencies
run: |
sudo apt update
sudo apt install -y xfce4 libxcb-xkb1 xclip
sudo apt install -y xfce4 libxcb-xkb1 xsel
# homebrew is not used but is required to reproduce an issue for a test case
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Expand Down
21 changes: 21 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
## [3.1.6](https://github.com/mikesmithgh/kitty-scrollback.nvim/compare/v3.1.5...v3.1.6) (2024-01-10)


### Bug Fixes

* use nul instead of enquiry ([#150](https://github.com/mikesmithgh/kitty-scrollback.nvim/issues/150)) ([141f678](https://github.com/mikesmithgh/kitty-scrollback.nvim/commit/141f67804a4d4fe4796d8b65807e95ccca1ff1a8)), closes [#147](https://github.com/mikesmithgh/kitty-scrollback.nvim/issues/147)

## [3.1.5](https://github.com/mikesmithgh/kitty-scrollback.nvim/compare/v3.1.4...v3.1.5) (2024-01-10)


### Bug Fixes

* prefer xsel over xclip ([#157](https://github.com/mikesmithgh/kitty-scrollback.nvim/issues/157)) ([088b3fc](https://github.com/mikesmithgh/kitty-scrollback.nvim/commit/088b3fcf04e4882dbd2f08822eb33dae2c33b1a2))

## [3.1.4](https://github.com/mikesmithgh/kitty-scrollback.nvim/compare/v3.1.3...v3.1.4) (2024-01-09)


### Bug Fixes

* prevent caught deadly signal message on exit ([#155](https://github.com/mikesmithgh/kitty-scrollback.nvim/issues/155)) ([00a0b5f](https://github.com/mikesmithgh/kitty-scrollback.nvim/commit/00a0b5f97030687a4fb6b454499f5ce55eb52144)), closes [#135](https://github.com/mikesmithgh/kitty-scrollback.nvim/issues/135)

## [3.1.3](https://github.com/mikesmithgh/kitty-scrollback.nvim/compare/v3.1.2...v3.1.3) (2024-01-03)


Expand Down
2 changes: 1 addition & 1 deletion doc/kitty-scrollback.nvim_spec.txt
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ M.setup() *kitty-scrollback.api.setup*


M.quit_all() *kitty-scrollback.api.quit_all*
Attempt to gracefully quit Neovim. How do you exit vim? Why would you exit vim?
Attempt to force quit Neovim. How do you exit vim? Why would you exit vim?


M.close_or_quit_all() *kitty-scrollback.api.close_or_quit_all*
Expand Down
6 changes: 2 additions & 4 deletions lua/kitty-scrollback/api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@ M.setup = function(private, options)
opts = options ---@diagnostic disable-line: unused-local
end

---Attempt to gracefully quit Neovim. How do you exit vim? Why would you exit vim?
---Attempt to force quit Neovim. How do you exit vim? Why would you exit vim?
M.quit_all = function()
-- quit causes nvim to exit early sometime interrupting underlying copy child process (.e.g, xclip)
-- send sigterm to gracefully terminate
vim.schedule(ksb_kitty_cmds.signal_term_to_kitty_child_process)
ksb_util.quitall()
end

---If the current buffer is the paste buffer, then close the window
Expand Down
16 changes: 13 additions & 3 deletions lua/kitty-scrollback/autocommands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,18 @@ M.set_yank_post_autocmd = function()

if yankevent.regname == '+' then
if vim.fn.has('clipboard') > 0 then
-- contents are copied to clipboard, return to kitty
ksb_api.quit_all()
-- Contents should be copied to clipboard, return to Kitty
local clipboard_tool = vim.api.nvim_call_function('provider#clipboard#Executable', {})
local defer_ms = 0
if clipboard_tool == 'xclip' then
-- The xclip child process was not spawning quick enough in the TextYankPost autocommand, resulting
-- in content not being copied to the clipboard so add a delay
-- see issue https://github.com/astrand/xclip/issues/38#ref-commit-b042f6d
defer_ms = 200
end
vim.defer_fn(function()
ksb_util.quitall()
end, defer_ms)
else
vim.schedule(function()
local prompt_msg =
Expand All @@ -147,7 +157,7 @@ M.set_yank_post_autocmd = function()
ksb_util.restore_and_redraw()
local response = vim.fn.confirm(prompt_msg, '&Quit\n&Continue')
if response ~= 2 then
ksb_api.quit_all()
ksb_util.quitall()
end
end)
end
Expand Down
36 changes: 24 additions & 12 deletions lua/kitty-scrollback/health.lua
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,25 @@ local function check_clipboard()
return s:find('^%s*$') ~= nil
end
vim.health.start('kitty-scrollback: clipboard')
local clipboard_tool = vim.fn['provider#clipboard#Executable']() -- copied from health.lua
local clipboard_tool = vim.api.nvim_call_function('provider#clipboard#Executable', {})
if vim.fn.has('clipboard') > 0 and not is_blank(clipboard_tool) then
vim.health.ok('Clipboard tool found: *' .. clipboard_tool .. '*')
if clipboard_tool == 'xclip' then
vim.health.warn([[
*xclip* may have issues copying content to the clipboard from Neovim. If you are having issues copying or
you are seeing errors similar to `Error : target STRING not available`, you should switch to *xsel* .
See *xclip* related issue: https://github.com/astrand/xclip/issues/38#ref-commit-b042f6d
See Neovim pull request preferring *xsel* over xclip: https://github.com/neovim/neovim/pull/20918
]])
end
else
vim.health.warn(
'Neovim does not have a clipboard provider.\n Some functionality will not work when there is no clipboard '
.. 'provider, such as copying Kitty scrollback buffer text to the system clipboard.',
'See `:help` |provider-clipboard| for more information on enabling system clipboard integration.'
)
vim.health.warn([[
Neovim does not have a clipboard provider.
Some functionality will not work when there is no clipboard provider, such as copying
Kitty scrollback buffer text to the system clipboard.
See `:help` |provider-clipboard| for more information on enabling system clipboard integration.
]])
end
end

Expand All @@ -104,17 +114,16 @@ local function check_sed()
return
end

local esc = vim.fn.eval([["\e"]])
local cmd = {
'sed',
'-E',
'-e',
's/$/' .. esc .. '[0m/g',
[[s/\r//g]],
'-e',
's/' .. esc .. '\\[\\?25.' .. esc .. '\\[.*;.*H' .. esc .. '\\[.*//g',
[[s/$/\x1b[0m/g]],
}
local ok, sed_proc = pcall(vim.system, cmd, {
stdin = 'expected' .. esc .. '[?25h' .. esc .. '[1;1H' .. esc .. '[notexpected',
stdin = 'expected\r',
})
local result = {}
if ok then
Expand All @@ -124,7 +133,8 @@ local function check_sed()
result.stdout = ''
result.stderr = sed_proc
end
ok = ok and result.code == 0 and result.stdout == 'expected'
local esc = vim.fn.eval([["\e"]])
ok = ok and result.code == 0 and result.stdout == 'expected' .. esc .. '[0m'
if ok then
vim.health.ok(
'`'
Expand All @@ -150,7 +160,9 @@ local function check_sed()
.. result.code
.. '* and stdout `'
.. result.stdout
.. '` (should be `expected`)\n'
.. '` (should be `expected'
.. esc
.. '[0m`)'
.. result_err
.. '`\n'
.. ' `sed: '
Expand Down
48 changes: 17 additions & 31 deletions lua/kitty-scrollback/kitty_commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ local display_error = function(cmd, r)
ksb_util.restore_and_redraw()
local response = vim.fn.confirm(prompt_msg, '&Quit\n&Continue')
if response ~= 2 then
M.signal_term_to_kitty_child_process(true)
ksb_util.quitall()
end
end

Expand Down Expand Up @@ -126,24 +126,19 @@ M.get_text_term = function(kitty_data, get_text_opts, on_exit_cb)
kitty_data.window_id,
get_text_opts
)
-- other sed filters that may come in handy
-- .. [[-e 's/(.*)\x1b]8;.*;.*\x1b\\(.*)/\1\2/g' ]] -- remove url/files/hyperlinks
-- .. [[-e 's/\x1b]133;[AC].*\x1b\\//g' ]] --replace shell integration prompt marks https://sw.kovidgoyal.net/kitty/shell-integration/#notes-for-shell-developers
-- .. [[-e 's/(.*)\x1b\[\?25.\x1b\[.*;.*H\x1b\[(\?12.|.+ q)(.*)/\1\3/g' ]] -- remove control sequence added by --add-cursor flag see https://github.com/kovidgoyal/kitty/blob/ec8b7853c55897bfcee5997dbd7cea734bdc2982/kitty/window.py#L346
local sed_cmd = [[sed -E ]]
-- wrap markers are important to add blank lines to fill the screen when setting the cursor position but
-- we don't actually want hard wraps because it will limit the width of content
-- the test should start with a small width, open the scrollback, the text that is wrapping should expand past the terminal up to 300
.. [[ -e 's/\r//g' ]] -- added to remove /r added by --add-wrap-markers, (--add-wrap-markers is used to add empty lines at end of screen)
-- .. [[-e 's/(.*)\x1b]8;.*;.*\x1b\\(.*)/\1\2/g' ]] -- remove url/files/hyperlinks
-- .. [[-e 's/\x1b]133;[AC].*\x1b\\//g' ]] --replace shell integration prompt marks https://sw.kovidgoyal.net/kitty/shell-integration/#notes-for-shell-developers
-- .. [[-e 's/(.*)\x1b\[\?25.\x1b\[.*;.*H\x1b\[(\?12.|.+ q)(.*)/\1\3/g' ]] -- remove control sequence added by --add-cursor flag see https://github.com/kovidgoyal/kitty/blob/ec8b7853c55897bfcee5997dbd7cea734bdc2982/kitty/window.py#L346
.. [[-e 's/(.+)$/\x1b[m\1/g' ]] -- append all lines with reset to avoid unintended colors
.. [[-e 's/\r//g' ]] -- added to remove /r added by --add-wrap-markers, (--add-wrap-markers is used to add empty lines at end of screen)
.. [[-e 's/$/\x1b[0m/g']] -- append all lines with reset to avoid unintended colors
local flush_stdout_cmd = p.kitty_data.kitty_path .. [[ +runpy 'sys.stdout.flush()']]
-- start to set title but do not complete see https://github.com/kovidgoyal/kitty/issues/719#issuecomment-952039731
local start_set_title_cmd = [[printf '\x1b]2;']]
local full_cmd = kitty_get_text_cmd
.. ' | '
.. sed_cmd
-- TODO: find scenario where I needed sed and possibly remove?
-- - reproduced on v1.0.0 but can't repro on this with: bat --no-pager ~/.bashrc; printf "before \x1b[1;1H after\n"
-- - may not need, but need to write tests first
.. ' && '
.. flush_stdout_cmd
.. ' && '
Expand Down Expand Up @@ -232,15 +227,19 @@ M.send_lines_to_kitty_and_quit = function(lines, execute_command)
end, lines),
'\r'
)
local esc = vim.fn.eval([["\e"]])
local enquiry = '\\x05' -- see https://en.wikipedia.org/wiki/Enquiry_character
local esc = [[\x1b]]
local nul_character = [[\x00]] -- see https://en.wikipedia.org/wiki/Null_character
local start_bracketed_paste = esc .. '[200~' -- see https://cirw.in/blog/bracketed-paste
local stop_bracketed_paste = esc .. '[201~' -- see https://cirw.in/blog/bracketed-paste

-- the beginning enquiry is used to separate any existing commands in kitty that may end with escape
-- the beginning nul is used to separate any existing commands in kitty that may end with escape
-- if escape is present, then bash autocompletion will be triggered because bracketed paste mode starts with an escape
-- the ending enquiry is used to remove deselect the text after pasting to the terminal
cmd_str = enquiry .. start_bracketed_paste .. cmd_str .. stop_bracketed_paste .. enquiry
-- the ending nul is used to remove deselect the text after pasting to the terminal
cmd_str = nul_character
.. start_bracketed_paste
.. cmd_str
.. stop_bracketed_paste
.. nul_character

if execute_command then
-- add a carriage return to execute command
Expand All @@ -254,7 +253,7 @@ M.send_lines_to_kitty_and_quit = function(lines, execute_command)
'--match=id:' .. p.kitty_data.window_id,
cmd_str,
})
M.signal_term_to_kitty_child_process()
ksb_util.quitall()
end

M.send_paste_buffer_text_to_kitty_and_quit = function(execute_command)
Expand Down Expand Up @@ -293,19 +292,6 @@ M.signal_winchanged_to_kitty_child_process = function()
})
end

M.signal_term_to_kitty_child_process = function(force)
if force then
vim.cmd.quitall({ bang = true })
else
system_handle_error({
p.kitty_data.kitty_path,
'@',
'signal-child',
'SIGTERM',
})
end
end

M.open_kitty_loading_window = function(env)
if p.kitty_loading_winid then
M.close_kitty_loading_window(true)
Expand Down
19 changes: 7 additions & 12 deletions lua/kitty-scrollback/launch.lua
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ local M = {}
---@field footer_winid number|nil
---@field footer_bufid number|nil
---@field pos table|nil
---@field block_input_timer table
local p = {}

---@type KsbOpts
Expand Down Expand Up @@ -225,19 +224,14 @@ end

local set_cursor_position = vim.schedule_wrap(function(d)
local tab_offset = ksb_util.tab_offset()
-- removed 1 because one line is no longer at the bottom since --add-cursor was removed
local add_cursor_offset = 0
local x = d.cursor_x - 1
local y = d.cursor_y - add_cursor_offset - tab_offset
local y = d.cursor_y - 1 - tab_offset
local scrolled_by = d.scrolled_by
local lines = d.lines - tab_offset
if y < 0 then
-- adjust when on first line of terminal
lines = lines + math.abs(y)
y = 0
elseif y > 0 then
-- removed 1 because one line is no longer at the bottom since --add-cursor was removed
y = y - 1
end
local last_line = vim.fn.line('$')

Expand Down Expand Up @@ -338,7 +332,7 @@ M.setup = function(kitty_data_str)
.. table.concat(ksb_health.advice().nvim_version)
local response = vim.fn.confirm(prompt_msg, '&Quit\n&Continue')
if response ~= 2 then
ksb_kitty_cmds.signal_term_to_kitty_child_process(true)
ksb_util.quitall()
end
end
if not ksb_health.check_kitty_version(true) then
Expand All @@ -348,7 +342,7 @@ M.setup = function(kitty_data_str)
.. table.concat(ksb_health.advice().kitty_version)
local response = vim.fn.confirm(prompt_msg, '&Quit\n&Continue')
if response ~= 2 then
ksb_kitty_cmds.signal_term_to_kitty_child_process(true)
ksb_util.quitall()
end
end

Expand Down Expand Up @@ -407,10 +401,11 @@ M.launch = function()
extent = '--extent=' .. extent_opt
end

-- adds /r but not cursor at end
local add_cursor = '--add-wrap-markers' -- always add cursor, add cursor has the important side effect of padding the bottom lines with empty strings
-- always add wrap markers, wrap markers are important to add blank lines with /r to
-- fill the screen when setting the cursor position
local add_wrap_markers = '--add-wrap-markers'

local get_text_opts = ansi .. ' ' .. clear_selection .. ' ' .. add_cursor .. ' ' .. extent
local get_text_opts = ansi .. ' ' .. clear_selection .. ' ' .. add_wrap_markers .. ' ' .. extent

-- 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
Expand Down
4 changes: 4 additions & 0 deletions lua/kitty-scrollback/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,8 @@ M.clear_yank_autocommand_and_get_visual_selection = function()
return reginfo.regcontents
end

M.quitall = function()
vim.cmd.quitall({ bang = true })
end

return M
8 changes: 7 additions & 1 deletion tests/kitty-scrollback/helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,13 @@ M.feed_kitty = function(input, pause_seconds_after)
local stdout = M.debug(M.kitty_remote_get_text()).stdout
local last_line = stdout:match('.*\n(.*)\n')
local start_of_line, cursor_y, cursor_x =
last_line:match('^(.*)\x1b%[%?25h\x1b%[(%d+);(%d+)H\x1b.*$')
last_line:match('^(.*)\x1b%[%?25[hl]\x1b%[(%d+);(%d+)H\x1b.*$')

if start_of_line == nil then
print(color_string('red', 'last_line is ' .. last_line:gsub('\x1b', '^[')))
assert.is_not_nil(start_of_line)
end

return {
stdout = stdout:gsub('[^\n]*\n$', start_of_line .. '\n'),
cursor_x = tonumber(cursor_x),
Expand Down
9 changes: 3 additions & 6 deletions tests/kitty-scrollback/kitty_scrollback_demo_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -178,11 +178,6 @@ $

it('should_copy_visual_selection_to_clipboard', function()
local paste = function()
-- TODO(#135): github runner has issues accessing the clipboard
-- just hardcode it for now since this test is for primarily for demo purposes anyway
if h.is_github_action then
return 'README.md'
end
return vim.fn.getreg('+')
end
h.feed_kitty({
Expand All @@ -194,7 +189,9 @@ $
}, 0)
h.assert_screen_equals(
h.feed_kitty({
h.send_without_newline([[printf "\n kitty-scrollback.nvim copied \e[35m]]),
h.with_pause_seconds_before(
h.send_without_newline([[printf "\n kitty-scrollback.nvim copied \e[35m]])
),
h.with_pause_seconds_before(h.send_without_newline(h.send_as_string(paste())), 1),
h.with_pause_seconds_before([[\e[0m to clipboard\n\n"]], 1),
}),
Expand Down

0 comments on commit ddbc903

Please sign in to comment.