Skip to content

Commit

Permalink
feat(surround): color in utils.surround can now be a function
Browse files Browse the repository at this point in the history
  • Loading branch information
rebelot committed Jan 16, 2022
1 parent 414ad38 commit 61507a7
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 18 deletions.
9 changes: 4 additions & 5 deletions cookbook.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,14 +225,13 @@ These functions are accessible via `require'heirline.conditions'` and
supplied one, updated with the fields in the optional `with` table.
- `surround(delimiters, color, component)`: returns a new component, which
contains a copy of the supplied one, surrounded by the left and right
delimiters supplied by the `delimiters` table. This action will override the
`bg` field of the component `hl`.
delimiters given by the `delimiters` table.
- `delimiters`: table of the form `{left_delimiter, right_delimiter}`.
Because they are actually just providers, delimiters could also be
functions!
- `color`: `nil` or `string` of color hex code or builtin color name. This
color will be the foreground color of the delimiters and the background
color of the component.
- `color`: `string|nil` or `function -> string|nil`. String should refer to
RGB hex code or builtin color name. This color will be the foreground color
of the delimiters and the background color of the component.
- `component`: the component to be surrounded.
- `insert(parent, ...)`: return a copy of `parent` component where each `child`
in `...` (variable arguments) is appended to its children (if any).
Expand Down
42 changes: 29 additions & 13 deletions lua/heirline/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ function M.get_highlight(hlname)
local t = {}
local hex = function(n)
if n then
return string.format("#%06x", n)
return string.format("#%06x", n)
end
end
t.fg = hex(hl.foreground)
Expand Down Expand Up @@ -39,26 +39,42 @@ end

function M.surround(delimiters, color, component)
component = M.clone(component)
component.hl = component.hl or {}
if type(component.hl) == "function" then
local old_hl_func = component.hl
component.hl = function(obj)
local hl = old_hl_func(obj)
hl.bg = color
return hl

local surround_color = function(self)
if type(color) == "function" then
return color(self)
else
return color
end
else
component.hl.bg = color
end

return {
{
provider = delimiters[1],
hl = { fg = color },
hl = function(self)
local s_color = surround_color(self)
if s_color then
return { fg = s_color }
end
end,
},
{
hl = function(self)
local s_color = surround_color(self)
if s_color then
return { bg = s_color }
end
end,
component,
},
component,
{
provider = delimiters[2],
hl = { fg = color },
hl = function(self)
local s_color = surround_color(self)
if s_color then
return { fg = s_color }
end
end,
},
}
end
Expand Down

0 comments on commit 61507a7

Please sign in to comment.