From 32c85cd214c387fd4883ccb18257e0204184fb85 Mon Sep 17 00:00:00 2001 From: shadmansaleh <13149513+shadmansaleh@users.noreply.github.com> Date: Wed, 2 Mar 2022 20:02:58 +0600 Subject: [PATCH] feat: add custom color support for icons --- README.md | 7 ++++++- doc/lualine.txt | 7 ++++++- lua/lualine/component.lua | 27 +++++++++++++++++++++++++-- 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index dcf22ae..157ae40 100644 --- a/README.md +++ b/README.md @@ -352,7 +352,12 @@ sections = { { 'mode', icons_enabled = true, -- Enables the display of icons alongside the component. - icon = nil, -- Defines the icon to be displayed in front of the component. + -- Defines the icon to be displayed in front of the component. + -- Can be string|table + -- As table it must contain the icon as first entry and can use + -- color option to custom color the icon. Example: + -- {'branch', icon = ''} / {'branch', icon = {'', color={fg='green'}}} + icon = nil, separator = nil, -- Determines what separator to use for the component. -- Note: diff --git a/doc/lualine.txt b/doc/lualine.txt index 024b8a0..7468d1a 100644 --- a/doc/lualine.txt +++ b/doc/lualine.txt @@ -365,7 +365,12 @@ General component options These are options that control behavior { 'mode', icons_enabled = true, -- Enables the display of icons alongside the component. - icon = nil, -- Defines the icon to be displayed in front of the component. + -- Defines the icon to be displayed in front of the component. + -- Can be string|table + -- As table it must contain the icon as first entry and can use + -- color option to custom color the icon. Example: + -- {'branch', icon = ''} / {'branch', icon = {'', color={fg='green'}}} + icon = nil, separator = nil, -- Determines what separator to use for the component. -- Note: diff --git a/lua/lualine/component.lua b/lua/lualine/component.lua index fdf6212..4f72dba 100644 --- a/lua/lualine/component.lua +++ b/lua/lualine/component.lua @@ -58,6 +58,15 @@ function M:create_option_highlights() false ) end + -- setup icon highlight + if type(self.options.icon) == 'table' and self.options.icon.color then + self.options.icon_color_highlight = highlight.create_component_highlight_group( + self.options.icon.color, + self.options.component_name .. 'icon', + self.options, + false + ) + end end ---adds spaces to left and right of a component @@ -111,8 +120,22 @@ end ---apply icon in front of component (prepemds component with icon) function M:apply_icon() - if self.options.icons_enabled and self.options.icon then - self.status = self.options.icon .. ' ' .. self.status + local icon = self.options.icon + if self.options.icons_enabled and icon then + if type(icon) == 'table' then + icon = icon[1] + end + if self.options.icon_color_highlight then + self.status = table.concat { + highlight.component_format_highlight(self.options.icon_color_highlight), + icon, + self:get_default_hl(), + ' ', + self.status, + } + else + self.status = table.concat({ icon, self.status }, ' ') + end end end