Skip to content

Commit

Permalink
chore: refactor feed kitty
Browse files Browse the repository at this point in the history
  • Loading branch information
mikesmithgh committed Dec 19, 2023
1 parent 7d09bf5 commit fde464a
Show file tree
Hide file tree
Showing 6 changed files with 232 additions and 148 deletions.
165 changes: 149 additions & 16 deletions tests/helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -240,28 +240,161 @@ end

local next_as_line = false

M.with_pause_before = function(input, delay)
if type(input) == 'string' then
return {
input,
opts = {
pause_before = delay or true,
},
}
else
return {
input[1],
opts = vim.tbl_extend('force', input.opts, {
pause_before = delay or true,
}),
}
end
end

M.send_as_string = function(input)
if type(input) == 'string' then
return {
input,
opts = {
send_by = 'string',
},
}
else
return {
input[1],
opts = vim.tbl_extend('force', input.opts, {
send_by = 'string',
}),
}
end
end

M.send_without_newline = function(input)
if type(input) == 'string' then
return {
input,
opts = {
newline = false,
},
}
else
return {
input[1],
opts = vim.tbl_extend('force', input.opts, {
newline = false,
}),
}
end
end

M.open_kitty_scrollback_nvim = function()
return {
opts = {
open_kitty_scrollback_nvim = true,
},
}
end

M.control_enter = function()
return {
[[\x1b[13;5u]],
opts = {
send_by = 'string',
},
}
end

M.shift_enter = function()
return {
[[\x1b[13;2u]],
opts = {
send_by = 'string',
},
}
end

M.enter = function()
return {
[[\n]],
opts = {
send_by = 'string',
},
}
end

M.control_v = function()
return {
[[\x16]],
opts = {
send_by = 'string',
},
}
end

M.esc = function()
return {
[[\x1b]],
opts = {
send_by = 'string',
},
}
end

M.control_d = function()
return {
[[\x04]],
opts = {
send_by = 'string',
},
}
end

M.feed_kitty = function(input)
for _, line in pairs(input) do
if line == 'pause' then
M.pause()
elseif line == '__open_ksb' then
local feed_opts = vim.tbl_extend('force', {
send_by = 'char',
newline = true,
open_kitty_scrollback_nvim = false,
}, (type(line) == 'table' and line.opts) and line.opts or {})

if feed_opts.pause_before then
if type(feed_opts.pause_before) == 'boolean' then
M.pause()
else
M.pause(feed_opts.pause_before)
end
end

if feed_opts.open_kitty_scrollback_nvim then
M.pause()
M.kitty_remote_kitten_kitty_scrollback_nvim()
M.pause()
elseif line == '__next_as_line' then
next_as_line = true
elseif line:match('^\\') or next_as_line then
M.pause(0.2)
M.kitty_remote_send_text(line)
M.pause(0.2)
if next_as_line then
next_as_line = false
end
else
line:gsub('.', function(c)
M.kitty_remote_send_text(c)
M.pause(0.03)
end)
local content = line
if type(line) == 'table' then
content = line[1]
end

if feed_opts.send_by == 'string' then
M.pause(0.2)
M.kitty_remote_send_text(content)
M.pause(0.2)
elseif feed_opts.send_by == 'char' then
content:gsub('.', function(c)
M.kitty_remote_send_text(c)
M.pause(0.03)
end)
end
if feed_opts.newline then
M.kitty_remote_send_text('\n')
end
end
end
M.pause(3) -- longer pause for linux
Expand Down
33 changes: 13 additions & 20 deletions tests/kitty-scrollback/kitty_scrollback_config_demo_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,8 @@ describe('kitty-scrollback.nvim', function()

h.feed_kitty({
[[source ]] .. ksb_dir .. [[tests/bashrc]],
[[\n]], -- enter
[[cd ]] .. ksb_work_dir,
[[\n]], -- enter
[[clear]],
[[\n]], -- enter
})
end)

Expand All @@ -100,29 +97,25 @@ printf "\\033[0m\\n"
eza --tree --icons ../kitty-scrollback.nvim/lua
lolcat --freq=0.15 --spread=1.5 --truecolor -
]],
[[__next_as_line]],
cowthink_stdout,
[[\x04]], -- control+d
[[
figlet -f cyberlarge -c -w 165 kitty-scrollback.nvim | lolcat]],
[[\n]], -- enter
[[
h.with_pause_before(h.send_without_newline(h.send_as_string(cowthink_stdout))),
h.send_without_newline(h.control_d()),
[[figlet -f cyberlarge -c -w 165 kitty-scrollback.nvim | lolcat]],
h.with_pause_before([[
colortest
]],
[[\n]], -- enter
[[__open_ksb]],
[[a# builtin > kitty_scrollback_nvim]],
[[\e]], -- esc
[[0o]],
[[
]]),
h.open_kitty_scrollback_nvim(),
h.send_without_newline([[a# builtin > kitty_scrollback_nvim]]),
h.send_without_newline(h.esc()),
h.send_without_newline([[0o]]),
h.send_without_newline([[
default configuration for the keymap `kitty_mod+h`
Browse scrollback buffer in kitty-scrollback.nvim
]],
[[\e]], -- esc
[[gg0]],
]]),
h.send_without_newline(h.esc()),
h.send_without_newline([[gg0]]),
}),
{
stdout = h.with_status_win(
Expand Down
32 changes: 12 additions & 20 deletions tests/kitty-scrollback/kitty_scrollback_cursor_position_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,6 @@ describe('kitty-scrollback.nvim', function()
end, 500)

assert.is_true(ready, 'kitty is not ready for remote connections, exiting')
h.pause()
h.feed_kitty({
[[clear]],
[[\n]], -- enter
})
end)

after_each(function()
Expand All @@ -60,7 +55,7 @@ describe('kitty-scrollback.nvim', function()
it('should position the cursor on first line when scrollback buffer has one line', function()
h.assert_screen_equals(
h.feed_kitty({
[[__open_ksb]],
h.open_kitty_scrollback_nvim(),
}),
{
stdout = h.with_status_win([[
Expand All @@ -76,8 +71,8 @@ $
it('should position the cursor on second line when scrollback buffer has two lines', function()
h.assert_screen_equals(
h.feed_kitty({
[[\n]], -- enter
[[__open_ksb]],
h.send_without_newline(h.enter()),
h.open_kitty_scrollback_nvim(),
}),
{
stdout = h.with_status_win([[
Expand All @@ -94,8 +89,7 @@ $
it('should show position the cursor on second to last line', function()
h.assert_screen_equals(
h.feed_kitty({
[[__next_as_line]],
[[
h.send_without_newline(h.send_as_string([[
# 1
# 2
# 3
Expand Down Expand Up @@ -124,8 +118,8 @@ $
# 26
# 27
# 28
# 29]],
[[__open_ksb]],
# 29]])),
h.open_kitty_scrollback_nvim(),
}),
{
stdout = h.with_status_win([[
Expand Down Expand Up @@ -169,8 +163,7 @@ $ # 29
it('should position the cursor on the last line', function()
h.assert_screen_equals(
h.feed_kitty({
[[__next_as_line]],
[[
h.send_without_newline(h.send_as_string([[
# 1
# 2
# 3
Expand Down Expand Up @@ -200,8 +193,8 @@ $ # 29
# 27
# 28
# 29
# 30]],
[[__open_ksb]],
# 30]])),
h.open_kitty_scrollback_nvim(),
}),
{
stdout = h.with_status_win([[
Expand Down Expand Up @@ -246,8 +239,7 @@ $ # 30
it('should show position the cursor on the last line when screen is full', function()
h.assert_screen_equals(
h.feed_kitty({
[[__next_as_line]],
[[
h.send_without_newline(h.send_as_string([[
# 1
# 2
# 3
Expand Down Expand Up @@ -278,8 +270,8 @@ $ # 30
# 28
# 29
# 30
# 31]],
[[__open_ksb]],
# 31]])),
h.open_kitty_scrollback_nvim(),
}),
{
stdout = h.with_status_win([[
Expand Down
Loading

0 comments on commit fde464a

Please sign in to comment.