Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support for passing highlight group names #130

Merged
merged 2 commits into from
Jun 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,9 @@ Every component passed to the `components` list has to be a table of the form:
bg = '#rrggbb' | function(buffer) -> '#rrggbb',
style = 'attr1,attr2,...' | function(buffer) -> 'attr1,attr2,...',

-- Or, alternatively, the name of the highlight group
highlight = 'string' | function(buffer) -> string | nil,

-- If `true` the buffer will be deleted when this component is
-- left-clicked (usually used to implement close buttons, overrides `on_click`).
-- deprecated, it is recommended to use the Buffer:delete() method in an on_click event
Expand Down
3 changes: 3 additions & 0 deletions doc/cokeline.txt
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,9 @@ Every component passed to the `components` list has to be a table of the form:
bg = '#rrggbb' | function(buffer) -> '#rrggbb' | nil,
style = 'string' | nil,

-- Or, alternatively, the name of the highlight group
highlight = 'string' | function(buffer) -> string | nil,

-- If `true` the buffer will be deleted when this component is
-- left-clicked (useful to implement close buttons).
delete_buffer_on_left_click = boolean,
Expand Down
18 changes: 12 additions & 6 deletions lua/cokeline/components.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ local map = vim.tbl_map
---@field style string|fun(buffer:Buffer): string
---@field fg string|fun(buffer:Buffer): string
---@field bg string|fun(buffer:Buffer): string
---@field highlight string|fun(buffer:Buffer): string
---@field on_click ClickHandler | nil
---@field on_mouse_enter MouseEnterHandler | nil
---@field on_mouse_leave MouseLeaveHandler | nil
Expand All @@ -38,6 +39,7 @@ Component.new = function(c, i, default_hl)
fg = c.fg or default_hl.fg or "NONE",
bg = c.bg or default_hl.bg or "NONE",
style = c.style or default_hl.style or "NONE",
highlight = c.highlight,
delete_buffer_on_left_click = c.delete_buffer_on_left_click or false,
on_click = c.on_click,
on_mouse_enter = c.on_mouse_enter,
Expand Down Expand Up @@ -88,12 +90,16 @@ Component.render = function(self, context)
or evaluate(_G.cokeline.config.default_hl.bg)
or "NONE"

component.hlgroup = Hlgroup.new(
("Cokeline_%s_%s"):format(component.bufnr or context.kind, self.index),
style,
fg,
bg
)
if component.highlight then
component.hlgroup = Hlgroup.new_existing(evaluate(component.highlight))
else
component.hlgroup = Hlgroup.new(
("Cokeline_%s_%s"):format(component.bufnr or context.kind, self.index),
style,
fg,
bg
)
end

return component
end
Expand Down
11 changes: 11 additions & 0 deletions lua/cokeline/hlgroups.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@ Hlgroup.new = function(name, gui, guifg, guibg)
return hlgroup
end

---Using already existing highlight group, returns a new `Hlgroup` table.
---@param name string
---@return Hlgroup
Hlgroup.new_existing = function(name)
local hlgroup = {
name = name,
}
setmetatable(hlgroup, Hlgroup)
return hlgroup
end

---Embeds some text in a highlight group.
---@param self Hlgroup
---@param text string
Expand Down