Skip to content

Commit

Permalink
feat: update paste_window options to be either a table of function (#220
Browse files Browse the repository at this point in the history
)
  • Loading branch information
mikesmithgh committed Apr 3, 2024
1 parent 0e65c8c commit f1df70c
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 31 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -586,17 +586,17 @@ The configuration precedence is `default` > `global` > `builtin` > `user` where

-- KsbPasteWindowOpts? options for paste window that sends commands to Kitty
paste_window = {
-- fun(): boolean? If function returns true, use Normal highlight group. If false, use NormalFloat
--- BoolOrFn? If true, use Normal highlight group. If false, use NormalFloat
highlight_as_normal_win = nil,
-- string? The filetype of the paste window
filetype = nil,
-- boolean? If true, hide mappings in the footer when the paste window is initially opened
hide_footer = false,
-- integer? The winblend setting of the window, see :help winblend
winblend = 0,
-- fun(paste_winopts: KsbWinOpts): table<string,any>? Paste float window overrides, see nvim_open_win() for configuration
-- KsbWinOptsOverride? Paste float window overrides, see nvim_open_win() for configuration
winopts_overrides = nil,
-- fun(footer_winopts: KsbWinOpts, paste_winopts: KsbWinOpts): table<string,any>? Paste footer window overrides, see nvim_open_win() for configuration
-- KsbFooterWinOptsOverride? Paste footer window overrides, see nvim_open_win() for configuration
footer_winopts_overrides = nil,
-- string? register used during yanks to paste window, see :h registers
yank_register = '',
Expand Down
6 changes: 3 additions & 3 deletions doc/kitty-scrollback.nvim.txt
Original file line number Diff line number Diff line change
Expand Up @@ -582,17 +582,17 @@ CONFIGURATION OPTIONS

-- KsbPasteWindowOpts? options for paste window that sends commands to Kitty
paste_window = {
-- fun(): boolean? If function returns true, use Normal highlight group. If false, use NormalFloat
--- BoolOrFn? If true, use Normal highlight group. If false, use NormalFloat
highlight_as_normal_win = nil,
-- string? The filetype of the paste window
filetype = nil,
-- boolean? If true, hide mappings in the footer when the paste window is initially opened
hide_footer = false,
-- integer? The winblend setting of the window, see :help winblend
winblend = 0,
-- fun(paste_winopts: KsbWinOpts): table<string,any>? Paste float window overrides, see nvim_open_win() for configuration
-- KsbWinOptsOverride? Paste float window overrides, see nvim_open_win() for configuration
winopts_overrides = nil,
-- fun(footer_winopts: KsbWinOpts, paste_winopts: KsbWinOpts): table<string,any>? Paste footer window overrides, see nvim_open_win() for configuration
-- KsbFooterWinOptsOverride? Paste footer window overrides, see nvim_open_win() for configuration
footer_winopts_overrides = nil,
-- string? register used during yanks to paste window, see :h registers
yank_register = '',
Expand Down
34 changes: 26 additions & 8 deletions doc/kitty-scrollback.nvim_spec.txt
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,12 @@ KsbStatusWindowOpts *default_opts.KsbStatusWindowOpts*
{icons} (KsbStatusWindowIcons) Icons displayed in the status window, defaults to 󰄛 󰣐 


BoolOrFn *default_opts.BoolOrFn*

Type: ~
boolean|fun():boolean


KsbWinOpts *default_opts.KsbWinOpts*

Type: ~
Expand All @@ -146,24 +152,36 @@ KsbWinOptsOverrideFunction *default_opts.KsbWinOptsOverrideFunction*
fun(paste_winopts:KsbWinOpts):KsbWinOpts


KsbWinOptsOverride *default_opts.KsbWinOptsOverride*

Type: ~
KsbWinOpts|fun(paste_winopts:KsbWinOpts):KsbWinOpts


*default_opts.KsbFooterWinOptsOverrideFunction*
KsbFooterWinOptsOverrideFunction

Type: ~
fun(footer_winopts:KsbWinOpts,paste_winopts:KsbWinOpts):KsbWinOpts


KsbFooterWinOptsOverride *default_opts.KsbFooterWinOptsOverride*

Type: ~
KsbWinOpts|KsbFooterWinOptsOverrideFunction


KsbPasteWindowOpts *default_opts.KsbPasteWindowOpts*

Fields: ~
{highlight_as_normal_win} (nil|fun():boolean) If function returns true, use Normal highlight group. If false, use NormalFloat
{filetype} (string|nil) The filetype of the paste window
{hide_footer} (boolean|nil) If true, hide mappings in the footer when the paste window is initially opened
{winblend} (integer|nil) The winblend setting of the window, see :help winblend
{winopts_overrides} (KsbWinOptsOverrideFunction|nil) Paste float window overrides, see nvim_open_win() for configuration
{footer_winopts_overrides} (KsbFooterWinOptsOverrideFunction|nil) Paste footer window overrides, see nvim_open_win() for configuration
{yank_register} (string|nil) register used during yanks to paste window, see :h registers
{yank_register_enabled} (boolean|nil) If true, the `yank_register` copies content to the paste window. If false, disable yank to paste window
{highlight_as_normal_win} (BoolOrFn|nil) If true, use Normal highlight group. If false, use NormalFloat
{filetype} (string|nil) The filetype of the paste window
{hide_footer} (boolean|nil) If true, hide mappings in the footer when the paste window is initially opened
{winblend} (integer|nil) The winblend setting of the window, see :help winblend
{winopts_overrides} (KsbWinOptsOverride|nil) Paste float window overrides, see nvim_open_win() for configuration
{footer_winopts_overrides} (KsbFooterWinOptsOverride|nil) Paste footer window overrides, see nvim_open_win() for configuration
{yank_register} (string|nil) register used during yanks to paste window, see :h registers
{yank_register_enabled} (boolean|nil) If true, the `yank_register` copies content to the paste window. If false, disable yank to paste window


KsbOpts *default_opts.KsbOpts*
Expand Down
10 changes: 6 additions & 4 deletions lua/kitty-scrollback/configs/defaults.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,20 @@
---@field show_timer boolean If true, show a timer in the status window while kitty-scrollback.nvim is loading
---@field icons KsbStatusWindowIcons Icons displayed in the status window, defaults to 󰄛 󰣐 

---@alias BoolOrFn boolean|fun():boolean
---@alias KsbWinOpts table<string, any>

---@alias KsbWinOptsOverrideFunction fun(paste_winopts:KsbWinOpts):KsbWinOpts
---@alias KsbWinOptsOverride KsbWinOpts|fun(paste_winopts:KsbWinOpts):KsbWinOpts
---@alias KsbFooterWinOptsOverrideFunction fun(footer_winopts:KsbWinOpts, paste_winopts:KsbWinOpts):KsbWinOpts
---@alias KsbFooterWinOptsOverride KsbWinOpts|KsbFooterWinOptsOverrideFunction

---@class KsbPasteWindowOpts
---@field highlight_as_normal_win nil|fun():boolean If function returns true, use Normal highlight group. If false, use NormalFloat
---@field highlight_as_normal_win BoolOrFn|nil If true, use Normal highlight group. If false, use NormalFloat
---@field filetype string|nil The filetype of the paste window
---@field hide_footer boolean|nil If true, hide mappings in the footer when the paste window is initially opened
---@field winblend integer|nil The winblend setting of the window, see :help winblend
---@field winopts_overrides KsbWinOptsOverrideFunction|nil Paste float window overrides, see nvim_open_win() for configuration
---@field footer_winopts_overrides KsbFooterWinOptsOverrideFunction|nil Paste footer window overrides, see nvim_open_win() for configuration
---@field winopts_overrides KsbWinOptsOverride|nil Paste float window overrides, see nvim_open_win() for configuration
---@field footer_winopts_overrides KsbFooterWinOptsOverride|nil Paste footer window overrides, see nvim_open_win() for configuration
---@field yank_register string|nil register used during yanks to paste window, see :h registers
---@field yank_register_enabled boolean|nil If true, the `yank_register` copies content to the paste window. If false, disable yank to paste window

Expand Down
5 changes: 4 additions & 1 deletion lua/kitty-scrollback/footer_win.lua
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,15 @@ M.footer_winopts = function(paste_winopts)
style = 'minimal',
}

if opts.paste_window.footer_winopts_overrides then
local footer_winopts_overrides = opts.paste_window.footer_winopts_overrides
if footer_winopts_overrides and type(footer_winopts_overrides) == 'function' then
footer_winopts = vim.tbl_deep_extend(
'force',
footer_winopts,
opts.paste_window.footer_winopts_overrides(footer_winopts, paste_winopts) or {}
)
elseif type(footer_winopts_overrides) == 'table' then
footer_winopts = vim.tbl_deep_extend('force', footer_winopts, footer_winopts_overrides)
end

return footer_winopts
Expand Down
8 changes: 7 additions & 1 deletion lua/kitty-scrollback/highlights.lua
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,14 @@ end

local function pastewin_color()
local hl_as_normal = M.has_default_or_vim_colorscheme()
if opts.paste_window.highlight_as_normal_win then

if
opts.paste_window.highlight_as_normal_win
and type(opts.paste_window.highlight_as_normal_win) == 'function'
then
hl_as_normal = opts.paste_window.highlight_as_normal_win()
else
hl_as_normal = opts.paste_window.highlight_as_normal_win == true
end
local hl_name = hl_as_normal and 'Normal' or 'NormalFloat'
local hl_def = vim.api.nvim_get_hl(0, { name = hl_name, link = false })
Expand Down
5 changes: 4 additions & 1 deletion lua/kitty-scrollback/windows.lua
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,12 @@ M.paste_winopts = function(row, col, height_offset)
end
end

if opts.paste_window.winopts_overrides then
local winopts_overrides = opts.paste_window.winopts_overrides
if winopts_overrides and type(winopts_overrides) == 'function' then
winopts =
vim.tbl_deep_extend('force', winopts, opts.paste_window.winopts_overrides(winopts) or {})
elseif type(winopts_overrides) == 'table' then
winopts = vim.tbl_deep_extend('force', winopts, winopts_overrides)
end

return winopts
Expand Down
4 changes: 1 addition & 3 deletions tests/example.lua
Original file line number Diff line number Diff line change
Expand Up @@ -240,9 +240,7 @@ require('kitty-scrollback').setup({
},
ksb_example_paste_win_highlight_as_float = {
paste_window = {
highlight_as_normal_win = function()
return false
end,
highlight_as_normal_win = false,
},
},
ksb_example_paste_win_register = {
Expand Down
57 changes: 50 additions & 7 deletions tests/kitty-scrollback/kitty_scrollback_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,28 @@ describe('kitty-scrollback.nvim', function()
{
test_pastewin_opts = {
paste_window = {
winopts_overrides = function(winopts)
winopts.border = { '▛', '▀', '▜', '▐', '▟', '▄', '▙', '▌' }
return winopts
end,
winopts_overrides = {
border = { '▛', '▀', '▜', '▐', '▟', '▄', '▙', '▌' }
},
},
},
test_footer_opts = {
paste_window = {
footer_winopts_overrides = {
border = { '▛', '▀', '▜', '▐', '▟', '▄', '▙', '▌' }
},
},
},
test_pastewin_and_footer_opts = {
paste_window = {
winopts_overrides = function(winopts)
winopts.border = { '▛', '▀', '▜', '▐', '▟', '▄', '▙', '▌' }
return winopts
end,
footer_winopts_overrides = function(winopts)
winopts.border = { '▛', '▀', '▜', '▐', '▟', '▄', '▙', '▌' }
return winopts
end,
winopts.border = { '▛', '▀', '▜', '▐', '▟', '▄', '▙', '▌' }
return winopts
end,
},
},
test_after_paste_window_ready = {
Expand Down Expand Up @@ -367,6 +377,39 @@ $🭽▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
)
end)

it('should set both winopts_overrides and footer_winopts_overrides', function()
h.assert_screen_equals(
h.feed_kitty({
h.open_kitty_scrollback_nvim({
'--config',
'test_pastewin_and_footer_opts',
}),
h.send_without_newline([[a]]),
}),
{
stdout = [[
$▛▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▜
▌ ▐
▌ ▐
▌ ▐
▌ ▐
▌ ▐
▌ ▐
▌ ▐
▌ ▐
▌ ▐
▌ ▐
▙▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▟
▛▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▜
▌ \y Yank <C-CR> Execute <S-CR> Paste :w Paste g? Toggle Mappings ▐
▙▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▟
]],
cursor_y = 2,
cursor_x = 3,
}
)
end)

it('should call after_paste_window_ready', function()
h.assert_screen_equals(
h.feed_kitty({
Expand Down

0 comments on commit f1df70c

Please sign in to comment.