From fde464ae454a25bcba36528d2e71d20f8d2be377 Mon Sep 17 00:00:00 2001 From: Mike Smith <10135646+mikesmithgh@users.noreply.github.com> Date: Tue, 19 Dec 2023 16:19:12 -0500 Subject: [PATCH] chore: refactor feed kitty --- tests/helpers.lua | 165 ++++++++++++++++-- .../kitty_scrollback_config_demo_spec.lua | 33 ++-- .../kitty_scrollback_cursor_position_spec.lua | 32 ++-- .../kitty_scrollback_demo_spec.lua | 64 +++---- .../kitty_scrollback_spec.lua | 60 +++---- .../kitty_scrollback_tabline_spec.lua | 26 +-- 6 files changed, 232 insertions(+), 148 deletions(-) diff --git a/tests/helpers.lua b/tests/helpers.lua index 74b051b9..4a7074a2 100644 --- a/tests/helpers.lua +++ b/tests/helpers.lua @@ -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 diff --git a/tests/kitty-scrollback/kitty_scrollback_config_demo_spec.lua b/tests/kitty-scrollback/kitty_scrollback_config_demo_spec.lua index 45832dcb..77b34a61 100644 --- a/tests/kitty-scrollback/kitty_scrollback_config_demo_spec.lua +++ b/tests/kitty-scrollback/kitty_scrollback_config_demo_spec.lua @@ -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) @@ -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( diff --git a/tests/kitty-scrollback/kitty_scrollback_cursor_position_spec.lua b/tests/kitty-scrollback/kitty_scrollback_cursor_position_spec.lua index c28eea12..fe458e2c 100644 --- a/tests/kitty-scrollback/kitty_scrollback_cursor_position_spec.lua +++ b/tests/kitty-scrollback/kitty_scrollback_cursor_position_spec.lua @@ -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() @@ -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([[ @@ -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([[ @@ -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 @@ -124,8 +118,8 @@ $ # 26 # 27 # 28 -# 29]], - [[__open_ksb]], +# 29]])), + h.open_kitty_scrollback_nvim(), }), { stdout = h.with_status_win([[ @@ -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 @@ -200,8 +193,8 @@ $ # 29 # 27 # 28 # 29 -# 30]], - [[__open_ksb]], +# 30]])), + h.open_kitty_scrollback_nvim(), }), { stdout = h.with_status_win([[ @@ -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 @@ -278,8 +270,8 @@ $ # 30 # 28 # 29 # 30 -# 31]], - [[__open_ksb]], +# 31]])), + h.open_kitty_scrollback_nvim(), }), { stdout = h.with_status_win([[ diff --git a/tests/kitty-scrollback/kitty_scrollback_demo_spec.lua b/tests/kitty-scrollback/kitty_scrollback_demo_spec.lua index 81c3c601..4bcd4d13 100644 --- a/tests/kitty-scrollback/kitty_scrollback_demo_spec.lua +++ b/tests/kitty-scrollback/kitty_scrollback_demo_spec.lua @@ -72,15 +72,13 @@ describe('kitty-scrollback.nvim', function() :wait() h.feed_kitty({ - [[cd ]] .. ksb_work_dir, - [[\n]], -- enter - [[ + h.send_as_string([[cd ]] .. ksb_work_dir), + h.send_as_string([[ echo '-- demo' >> README.md echo '-- demo' >> lua/kitty-scrollback/init.lua echo '-- demo' >> lua/kitty-scrollback/api.lua echo '-- demo' >> lua/kitty-scrollback/health.lua -clear -]], +clear]]), }) end) @@ -93,39 +91,33 @@ clear h.assert_screen_equals( h.feed_kitty({ [[git status]], - [[\n]], -- enter - [[__open_ksb]], + h.open_kitty_scrollback_nvim(), [[?README.md]], - [[\n]], -- enter - [[\x16]], -- control+v - [[jjj$yddggI]], - [[git checkout ]], - [[\x1b]], -- esc - [[j0]], - [[\x16]], -- control+v - [[GI]], - [[git add ]], - [[\x1b]], -- esc - [[\x1b[13;5u]], -- control+enter - [[pause]], - [[pause]], - [[git status]], - [[\n]], - [[__open_ksb]], - [[6k3VyggOlolcat <