Color highlighting internally for custom component #586
-
I'm trying to understand how to highlight pieces of a custom component but not fully sure how to handle this properly. I was looking at how this is done for the local status_symbols = {
unclean = '✘',
clean = '✔',
}
local colors = {
clean = '%#lualine_b_diagnostics_info_normal#',
unclean = '%#lualine_b_diagnostics_error_normal#',
}
summary = {
clean = 38
unclean = 16,
}
local format_summary = function(summary)
local pieces = {}
for _, status in ipairs({'clean', 'unclean'}) do
local num = summary[status]
if num > 0 then
table.insert(pieces, string.format('%s%s%s', colors[status], status_symbols[status], num))
end
end
return table.concat(pieces, ' ')
end where I put the It does sort of work although there is a weird edge. And also I guess this is not the intended way since I'm pretty much just taking the highlighting from Any advice how to do this properly would be highly appreciated :) Thanks in advance! Edit: I also did try to use |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 7 replies
-
Just for context, this is the WIP plugin I'm working on which I trying to do this :) |
Beta Was this translation helpful? Give feedback.
-
You will have to use You can follow the structure on diagnostics/diff components . For your case you'll need something like this local M = require('lualine.component'):extend()
local highlight = require 'lualine.highlight'
function M:init(options)
M.super.init(self, options)
self.status_symbols = {
unclean = '✘',
clean = '✔',
}
self.colors = {
clean = highlight.create_component_highlight_group(
{fg = '#48b0bd'}, 'summery_clean', self.options),
unclean = highlight.create_component_highlight_group(
{fg = '#e55561'}, 'summery_unclean', self.options)
}
end
function M:update_status()
local summary = { -- fetch actual summery
clean = 38,
unclean = 16,
}
local pieces = {}
for _, status in ipairs({'clean', 'unclean'}) do
local num = summary[status]
if num > 0 then
table.insert(pieces, string.format('%s%s%s',
highlight.component_format_highlight(self.colors[status]),
self.status_symbols[status], num))
end
end
return table.concat(pieces, ' ')
end
return M |
Beta Was this translation helpful? Give feedback.
-
Also: if you don't want to create a new highlight group but instead use existing highlight groups, a lua function (or expression) can simply return a string with function custom_summary_component()
local summary = { -- fetch actual summary
clean = 38,
unclean = 16,
}
local pieces = {}
for _, status in ipairs({'clean', 'unclean'}) do
local num = summary[status]
if num > 0 then
table.insert(pieces, string.format('%s%s%s',
('%#MyHighlightGroup_%s#'):format(status), -- <=================== see this line
self.status_symbols[status], num))
end
end
return table.concat(pieces, ' ')
end |
Beta Was this translation helpful? Give feedback.
You will have to use
create_component_highlight_group
andcomponent_format_highlight
pair. Then lualine will set correct background color based on section and mode .You can follow the structure on diagnostics/diff components . For your case you'll need something like this