Skip to content

Commit

Permalink
feat!: add support for new neovim default colorscheme (#103)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Existing highlight groups were renamed. If you were overriding any kitty-scrollback.nvim highlight groups, please update the names referencing the table below.

    | Previous highlight name    | New highlight name                      |
    | -------------------------- | --------------------------------------- |
    | KittyScrollbackNvimNormal  | KittyScrollbackNvimStatusWinNormal      |       
    | KittyScrollbackNvimHeart   | KittyScrollbackNvimStatusWinHeartIcon   |       
    | KittyScrollbackNvimSpinner | KittyScrollbackNvimStatusWinSpinnerIcon |       
    | KittyScrollbackNvimReady   | KittyScrollbackNvimStatusWinReadyIcon   |       
    | KittyScrollbackNvimKitty   | KittyScrollbackNvimStatusWinKittyIcon   |       
    | KittyScrollbackNvimVim     | KittyScrollbackNvimStatusWinNvimIcon    |
  • Loading branch information
mikesmithgh authored Dec 12, 2023
1 parent 6e2bd60 commit dfbc16c
Show file tree
Hide file tree
Showing 10 changed files with 291 additions and 365 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## [2.4.4](https://github.com/mikesmithgh/kitty-scrollback.nvim/compare/v2.4.3...v2.4.4) (2023-12-12)


### Bug Fixes

* explicitly set colorscheme to vim ([#106](https://github.com/mikesmithgh/kitty-scrollback.nvim/issues/106)) ([6e2bd60](https://github.com/mikesmithgh/kitty-scrollback.nvim/commit/6e2bd60d14fdfb9839a7ac6e352c880f463cc9ba)), closes [#93](https://github.com/mikesmithgh/kitty-scrollback.nvim/issues/93)

## [2.4.3](https://github.com/mikesmithgh/kitty-scrollback.nvim/compare/v2.4.2...v2.4.3) (2023-12-11)


Expand Down
185 changes: 56 additions & 129 deletions README.md

Large diffs are not rendered by default.

267 changes: 99 additions & 168 deletions doc/kitty-scrollback.nvim.txt

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions doc/kitty-scrollback.nvim_spec.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ KsbPrivate *kitty-scrollback.launch.KsbPrivate*

Fields: ~
{orig_columns} (number)
{orig_normal_hl} (table|nil)
{bufid} (number|nil)
{paste_bufid} (number|nil)
{kitty_loading_winid} (number|nil)
Expand Down
12 changes: 6 additions & 6 deletions lua/kitty-scrollback/configs/example.lua
Original file line number Diff line number Diff line change
Expand Up @@ -148,27 +148,27 @@ return {
end
return {
highlight_overrides = {
KittyScrollbackNvimNormal = {
KittyScrollbackNvimStatusWinNormal = {
fg = '#ee82ee',
bg = '#ee82ee',
},
KittyScrollbackNvimHeart = {
KittyScrollbackNvimStatusWinHeartIcon = {
fg = '#ff0000',
bg = '#4b0082',
},
KittyScrollbackNvimSpinner = {
KittyScrollbackNvimStatusWinSpinnerIcon = {
fg = '#000099',
bg = '#4b0082',
},
KittyScrollbackNvimReady = {
KittyScrollbackNvimStatusWinReadyIcon = {
fg = '#4b0082',
bg = '#ffa500',
},
KittyScrollbackNvimKitty = {
KittyScrollbackNvimStatusWinKittyIcon = {
fg = '#ffa500',
bg = '#000099',
},
KittyScrollbackNvimVim = {
KittyScrollbackNvimStatusWinNvimIcon = {
fg = '#008000',
bg = '#000099',
},
Expand Down
124 changes: 84 additions & 40 deletions lua/kitty-scrollback/highlights.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,40 +11,72 @@ local opts

---see `:help nvim_set_hl()` for highlight group definition format
---@class KsbHighlights
---@field KittyScrollbackNvimNormal table|nil status window Normal highlight group
---@field KittyScrollbackNvimHeart table|nil status window heart icon highlight group
---@field KittyScrollbackNvimSpinner table|nil status window spinner icon highlight group
---@field KittyScrollbackNvimReady table|nil status window ready icon highlight group
---@field KittyScrollbackNvimKitty table|nil status window kitty icon highlight group
---@field KittyScrollbackNvimVim table|nil status window vim icon highlight group
---@field KittyScrollbackNvimStatusWinNormal table|nil status window Normal highlight group
---@field KittyScrollbackNvimStatusWinHeartIcon table|nil status window heart icon highlight group
---@field KittyScrollbackNvimStatusWinSpinnerIcon table|nil status window spinner icon highlight group
---@field KittyScrollbackNvimStatusWinReadyIcon table|nil status window ready icon highlight group
---@field KittyScrollbackNvimStatusWinKittyIcon table|nil status window kitty icon highlight group
---@field KittyScrollbackNvimStatusWinNvimIcon table|nil status window vim icon highlight group
---@field KittyScrollbackNvimPasteWinNormal table|nil paste window Normal highlight group
---@field KittyScrollbackNvimPasteWinFloatBorder table|nil paste window FloatBorder highlight group
---@field KittyScrollbackNvimPasteWinFloatTitle table|nil paste window FloatTitle highlight group
---@field KittyScrollbackNvimVisual table|nil scrollback buffer window visual selection highlight group
---@field KittyScrollbackNvimNormal table|nil scrollback buffer window normal highlight group

---@see nvim_set_hl
-- local function has_vim_colorscheme()
-- return vim.fn.getcompletion('vim$', 'color')[1] ~= nil
-- end

M.has_default_or_vim_colorscheme = function()
return vim.g.colors_name == nil or vim.g.colors_name == 'default' or vim.g.colors_name == 'vim'
end

local function fg_or_fallback(hl_def)
local fg = type(hl_def.fg) == 'number' and string.format('#%06x', hl_def.fg) or hl_def.fg
return hl_def.fg and fg or (vim.o.background == 'dark' and '#ffffff' or '#000000')
end

local function bg_or_fallback(hl_def)
local bg = type(hl_def.bg) == 'number' and string.format('#%06x', hl_def.bg) or hl_def.bg
return hl_def.bg and bg or (vim.o.background == 'dark' and '#000000' or '#ffffff')
end

local function normal_color()
local hl_def = vim.api.nvim_get_hl(0, { name = 'Normal', link = false })
hl_def = next(hl_def) and hl_def or {} -- can return vim.empty_dict() so convert to lua table
local normal_fg_color = M.has_default_or_vim_colorscheme() and p.kitty_colors.foreground
or fg_or_fallback(hl_def)
local normal_bg_color = M.has_default_or_vim_colorscheme() and p.kitty_colors.background
or bg_or_fallback(hl_def)
return {
fg = normal_fg_color,
bg = normal_bg_color,
}
end

local function pastewin_color()
local hl_as_normal = opts.paste_window.highlight_as_normal_win
or M.has_default_or_vim_colorscheme()
local hl_name = hl_as_normal and 'Normal' or 'NormalFloat'
local hl_def = vim.api.nvim_get_hl(0, { name = hl_name, link = false })
local pastewin_hl = next(hl_def) and hl_def or {} -- can return vim.empty_dict() so convert to lua table
if hl_as_normal then
pastewin_hl = normal_color()
end
return pastewin_hl
end

---@return KsbHighlights
local function highlight_definitions()
if not p.kitty_colors or not next(p.kitty_colors) then
return {}
end
local hl_as_normal_fn = opts.paste_window.highlight_as_normal_win
or function()
return vim.g.colors_name == nil
or vim.g.colors_name == 'default'
or vim.g.colors_name == 'vim'
end
local hl_name = hl_as_normal_fn() and 'Normal' or 'NormalFloat'
local hl_def = vim.api.nvim_get_hl(0, { name = hl_name, link = false })
hl_def = next(hl_def) and hl_def or {} -- can return vim.empty_dict() so convert to lua table
local normal_bg_color = hl_def.bg and string.format('#%06x', hl_def.bg)
or p.kitty_colors.background
local floatborder_fg_color =
ksb_util.darken(p.kitty_colors.foreground, 0.3, p.kitty_colors.background)

local visual_hl_def = vim.api.nvim_get_hl(0, { name = 'Visual', link = false })
visual_hl_def = next(visual_hl_def) and visual_hl_def or {} -- can return vim.empty_dict() so convert to lua table
local pastewin_hl = pastewin_color()
local visual_hl_configs = {
reverse = {
default = true,
Expand All @@ -58,67 +90,84 @@ local function highlight_definitions()
},
darken = {
default = true,
bg = ksb_util.darken(
hl_def.fg and string.format('#%06x', hl_def.fg)
or (vim.o.background == 'dark' and '#ffffff' or '#000000'),
0.2,
normal_bg_color
),
bg = ksb_util.darken(fg_or_fallback(pastewin_hl), 0.2, pastewin_hl.bg),
},
nvim = visual_hl_def,
}
local visual_hl = visual_hl_configs[opts.visual_selection_highlight_mode]
or visual_hl_configs.nvim
local normal_hl = normal_color()

return {
KittyScrollbackNvimVisual = visual_hl,
KittyScrollbackNvimNormal = {
default = true,
fg = normal_hl.fg,
bg = normal_hl.bg,
},
-- status window
KittyScrollbackNvimStatusWinNormal = {
default = true,
fg = '#968c81',
bg = normal_hl.bg,
},
KittyScrollbackNvimHeart = {
KittyScrollbackNvimStatusWinHeartIcon = {
default = true,
fg = '#d55b54',
bg = normal_hl.bg,
},
KittyScrollbackNvimSpinner = {
KittyScrollbackNvimStatusWinSpinnerIcon = {
default = true,
fg = '#d3869b',
bg = normal_hl.bg,
},
KittyScrollbackNvimReady = {
KittyScrollbackNvimStatusWinReadyIcon = {
default = true,
fg = '#8faa80',
bg = normal_hl.bg,
},
KittyScrollbackNvimKitty = {
KittyScrollbackNvimStatusWinKittyIcon = {
default = true,
fg = '#754b33',
bg = normal_hl.bg,
},
KittyScrollbackNvimVim = {
KittyScrollbackNvimStatusWinNvimIcon = {
default = true,
fg = '#87987e',
bg = normal_hl.bg,
},
-- paste window
KittyScrollbackNvimPasteWinNormal = {
default = true,
bg = normal_bg_color,
bg = pastewin_hl.bg,
blend = opts.paste_window.winblend or 0,
},
KittyScrollbackNvimPasteWinFloatBorder = {
default = true,
bg = normal_bg_color,
bg = pastewin_hl.bg,
fg = floatborder_fg_color,
blend = opts.paste_window.winblend or 0,
},
KittyScrollbackNvimPasteWinFloatTitle = {
default = true,
bg = floatborder_fg_color,
fg = normal_bg_color,
fg = pastewin_hl.bg,
blend = opts.paste_window.winblend or 0,
},
KittyScrollbackNvimVisual = visual_hl,
}
end

---@param private KsbPrivate
---@param options KsbOpts
---@return true|false
M.setup = function(private, options)
p = private
opts = options ---@diagnostic disable-line: unused-local
p.orig_normal_hl = vim.api.nvim_get_hl(0, { name = 'Normal', link = false })
if M.has_default_or_vim_colorscheme() then
-- clear Normal highlight to avoid flicker when bg color is set
vim.api.nvim_set_hl(0, 'Normal', {})
end
local ok, colors = ksb_api.get_kitty_colors(p.kitty_data)
if ok then
p.kitty_colors = colors
Expand All @@ -127,7 +176,7 @@ M.setup = function(private, options)
end

---Format nvim highlights to arguments passed to kitty launch command
---E.g., KittyScrollbackNvimVim with #188b25 to --env KITTY_SCROLLBACK_NVIM_VIM=#188b25
---E.g., KittyScrollbackNvimStatusWinNvimIcon with #188b25 to --env KITTY_SCROLLBACK_NVIM_VIM=#188b25
---@return table list of environment variable arguments
M.get_highlights_as_env = function()
local env = {}
Expand All @@ -137,12 +186,7 @@ M.get_highlights_as_env = function()
hl_def = next(hl_def) and hl_def or {} -- nvim_get_hl can return vim.empty_dict() so convert to lua table
table.insert(
env,
string.format(
'%s=#%06x',
ksb_util.screaming_snakecase(name),
hl_def.fg
or (vim.o.background == 'dark' and tonumber('ffffff', 16) or tonumber('000000', 16))
)
string.format('%s_HIGHLIGHT=%s', ksb_util.screaming_snakecase(name), fg_or_fallback(hl_def))
)
end
return env
Expand Down
16 changes: 12 additions & 4 deletions lua/kitty-scrollback/launch.lua
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ local M = {}

---@class KsbPrivate
---@field orig_columns number
---@field orig_normal_hl table|nil
---@field bufid number|nil
---@field paste_bufid number|nil
---@field kitty_loading_winid number|nil
Expand Down Expand Up @@ -332,6 +333,9 @@ M.setup = function(kitty_data_str)
if ok then
ksb_hl.set_highlights()
ksb_kitty_cmds.open_kitty_loading_window(ksb_hl.get_highlights_as_env()) -- must be after opts and set highlights
if ksb_hl.has_default_or_vim_colorscheme() then
vim.api.nvim_set_hl(0, 'Normal', p.orig_normal_hl)
end
end

if
Expand Down Expand Up @@ -399,10 +403,14 @@ M.launch = function()
local term_buf_name = vim.api.nvim_buf_get_name(p.bufid)
term_buf_name = term_buf_name:gsub('^(term://.-:).*', '%1kitty-scrollback.nvim')
vim.api.nvim_buf_set_name(p.bufid, term_buf_name)
vim.api.nvim_set_option_value('winhighlight', 'Visual:KittyScrollbackNvimVisual', {
scope = 'local',
win = 0,
})
vim.api.nvim_set_option_value(
'winhighlight',
'Normal:KittyScrollbackNvimNormal,Visual:KittyScrollbackNvimVisual',
{
scope = 'local',
win = 0,
}
)
vim.api.nvim_buf_delete(vim.fn.bufnr('#'), { force = true }) -- delete alt buffer after rename

if opts.restore_options then
Expand Down
26 changes: 15 additions & 11 deletions lua/kitty-scrollback/windows.lua
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,14 @@ M.show_status_window = function()
noautocmd = true,
})
)
vim.api.nvim_set_option_value('winhighlight', 'NormalFloat:KittyScrollbackNvimNormal', {
win = popup_winid,
scope = 'local',
})
vim.api.nvim_set_option_value(
'winhighlight',
'NormalFloat:KittyScrollbackNvimStatusWinNormal',
{
win = popup_winid,
scope = 'local',
}
)
local count = 0
local spinner = { '', '', '', '', '', '', '' }
if opts.status_window.style_simple then
Expand Down Expand Up @@ -219,28 +223,28 @@ M.show_status_window = function()
if spinner_icon ~= '' then
endcol = #spinner_icon + 2
vim.api.nvim_buf_set_extmark(popup_bufid, nid, 0, startcol, {
hl_group = count >= #spinner and 'KittyScrollbackNvimReady'
or 'KittyScrollbackNvimSpinner',
hl_group = count >= #spinner and 'KittyScrollbackNvimStatusWinReadyIcon'
or 'KittyScrollbackNvimStatusWinSpinnerIcon',
end_col = endcol,
})
end
if not opts.status_window.style_simple then
startcol = endcol
endcol = endcol + #kitty_icon + 1
vim.api.nvim_buf_set_extmark(popup_bufid, nid, 0, startcol, {
hl_group = 'KittyScrollbackNvimKitty',
hl_group = 'KittyScrollbackNvimStatusWinKittyIcon',
end_col = endcol,
})
startcol = endcol
endcol = endcol + #love_icon + 1
vim.api.nvim_buf_set_extmark(popup_bufid, nid, 0, startcol, {
hl_group = 'KittyScrollbackNvimHeart',
hl_group = 'KittyScrollbackNvimStatusWinHeartIcon',
end_col = endcol,
})
startcol = endcol
endcol = #fmt_msg
vim.api.nvim_buf_set_extmark(popup_bufid, nid, 0, startcol, {
hl_group = 'KittyScrollbackNvimVim',
hl_group = 'KittyScrollbackNvimStatusWinNvimIcon',
end_col = endcol,
})
end
Expand Down Expand Up @@ -282,14 +286,14 @@ M.show_status_window = function()
else
if count > #spinner then
local hl_def = vim.api.nvim_get_hl(0, {
name = 'KittyScrollbackNvimReady',
name = 'KittyScrollbackNvimStatusWinReadyIcon',
link = false,
})
hl_def = next(hl_def) and hl_def or {} -- nvim_get_hl can return vim.empty_dict() so convert to lua table
local fg_dec = hl_def.fg or 16777215 -- default to #ffffff
local fg_hex = string.format('#%06x', fg_dec)
local darken_hex = ksb_util.darken(fg_hex, 0.7)
vim.api.nvim_set_hl(0, 'KittyScrollbackNvimReady', {
vim.api.nvim_set_hl(0, 'KittyScrollbackNvimStatusWinReadyIcon', {
fg = darken_hex,
})
if count > #spinner + (#spinner / 2) then
Expand Down
3 changes: 1 addition & 2 deletions python/kitty_scrollback_nvim.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,12 +186,11 @@ def handle_result(args: List[str],

nvim_args = parse_nvim_args(args) + (
'--cmd',
' lua '
' lua'
' vim.api.nvim_create_autocmd([[VimEnter]], {'
' group = vim.api.nvim_create_augroup([[KittyScrollBackNvimVimEnter]], { clear = true }),'
' pattern = [[*]],'
' callback = function()'
' vim.cmd.colorscheme({ args = {[[vim]]}, mods = { emsg_silent = true }})'
f' vim.opt.runtimepath:append([[{ksb_dir}]])'
' vim.api.nvim_exec_autocmds([[User]], { pattern = [[KittyScrollbackLaunch]], modeline = false })'
f' require([[kitty-scrollback.launch]]).setup_and_launch([[{kitty_data}]])'
Expand Down
Loading

0 comments on commit dfbc16c

Please sign in to comment.