Skip to content

Commit

Permalink
fix: temporarily block user input on start (#140)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikesmithgh authored Jan 3, 2024
1 parent 7f33ae8 commit ad0d6e8
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 2 deletions.
1 change: 1 addition & 0 deletions lua/kitty-scrollback/health.lua
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ local function check_kitty_scrollback_nvim_version()
end

M.check = function()
require('kitty-scrollback.backport').setup()
if
M.check_nvim_version('nvim-0.9')
and check_kitty_scrollback_nvim_version()
Expand Down
12 changes: 12 additions & 0 deletions lua/kitty-scrollback/launch.lua
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ local p = {}
---@type KsbOpts
local opts = {}

local block_input_timer

---@class KsbCallbacks
---@field after_setup fun(kitty_data:KsbKittyData, opts:KsbOpts)|nil callback executed after initializing kitty-scrollback.nvim
---@field after_launch fun(kitty_data:KsbKittyData, opts:KsbOpts)|nil callback executed after launch started to process the scrollback buffer
Expand Down Expand Up @@ -239,6 +241,7 @@ local set_cursor_position = vim.schedule_wrap(function(d)
vim.o.scrolloff = 0
vim.o.laststatus = 0
vim.o.virtualedit = 'all'
---@diagnostic disable-next-line: param-type-mismatch
vim.fn.cursor(last_line, 1) -- cursor last line
-- using normal commands instead of cursor pos due to virtualedit
if lines ~= 0 then
Expand Down Expand Up @@ -288,6 +291,11 @@ end
---Setup and configure kitty-scrollback.nvim
---@param kitty_data_str string
M.setup = function(kitty_data_str)
-- block user input for a short period of time during startup to prevent unwanted mode changes and unexpected behavior
block_input_timer = vim.fn.timer_start(25, function()
pcall(vim.fn.getchar, 0)
end, { ['repeat'] = 80 }) -- 2 seconds

p.kitty_data = vim.fn.json_decode(kitty_data_str)
load_requires() -- must be after p.kitty_data initialized

Expand Down Expand Up @@ -431,6 +439,7 @@ 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

if opts.restore_options then
Expand All @@ -450,6 +459,9 @@ M.launch = function()
end)
end
ksb_api.close_kitty_loading_window()
if block_input_timer then
vim.fn.timer_stop(block_input_timer)
end
end)
end)
if
Expand Down
2 changes: 1 addition & 1 deletion lua/kitty-scrollback/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ M.tab_offset = function()
return 0
end

function M.clear_yank_autocommand_and_get_visual_selection()
M.clear_yank_autocommand_and_get_visual_selection = function()
vim.api.nvim_clear_autocmds({
group = vim.api.nvim_create_augroup('KittyScrollBackNvimTextYankPost', { clear = true }),
})
Expand Down
48 changes: 47 additions & 1 deletion tests/kitty-scrollback/kitty_scrollback_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ $ brew search a
)
end)

it('should successfully open checkhealth', function()
it('should successfully open KittyScrollbackCheckHealth', function()
local actual = h.feed_kitty({
h.send_as_string(
[[nvim +'lua vim.opt.rtp:append("../..") vim.opt.rtp:append("../../kitty-scrollback.nvim") require("kitty-scrollback").setup() vim.cmd("KittyScrollbackCheckHealth")']]
Expand All @@ -79,6 +79,36 @@ $ brew search a
──────────────────────────────────────────────────────────────────────────────
kitty-scrollback: require("kitty-scrollback.health").check()
kitty-scrollback: Neovim version
]],
cursor_y = 1,
cursor_x = 1,
}, 'kitty-scrollback.nvim checkhealth content did not start with expected content')
end)

it('should successfully open checkhealth and warn user no kitty data available', function()
local actual = h.feed_kitty({
h.send_as_string(
[[nvim +'lua vim.opt.rtp:append("../..") vim.opt.rtp:append("../../kitty-scrollback.nvim") require("kitty-scrollback").setup() vim.cmd("checkhealth kitty-scrollback")']]
),
h.send_without_newline([[zR]]),
})
h.assert_screen_not_match(
actual,
{ pattern = 'ERROR', cursor_y = 1, cursor_x = 1 },
'kitty-scrollback.nvim checkhealth had an unexpected health check ERROR'
)
h.assert_screen_match(actual, {
pattern = 'WARNING No Kitty data available unable to perform a complete healthcheck',
cursor_y = 1,
cursor_x = 1,
})
h.assert_screen_starts_with(actual, {
stdout = [[
──────────────────────────────────────────────────────────────────────────────
kitty-scrollback: require("kitty-scrollback.health").check()
kitty-scrollback: Neovim version
]],
cursor_y = 1,
Expand Down Expand Up @@ -205,4 +235,20 @@ $
'kitty-scrollback.nvim did not have expected wrap marker results'
)
end)

it('should temporarily block user input on start', function()
h.kitty_remote_kitten_kitty_scrollback_nvim()
h.assert_screen_equals(
h.feed_kitty({
h.with_pause_seconds_before([[a]], 0.1),
}),
{
stdout = h.with_status_win([[
$
]]),
cursor_y = 1,
cursor_x = 3,
}
)
end)
end)

0 comments on commit ad0d6e8

Please sign in to comment.