From 1cdddc24565f97c2eb7b182e1e1eef6ae6fea91e Mon Sep 17 00:00:00 2001 From: Daemon Date: Sat, 30 Apr 2022 03:10:47 -0700 Subject: [PATCH] feat: add option to invert component icon alignment (#666) * feat: add option to invert component icon alignment Invert component status icon if variable is set `icon_right = true`. * refactor: use string for icon alignment Change conditional variable for icon left/right alignment from Boolean to `icon_alignment = 'right'`. * refactor: check icon table for alignment variable Determine component status output from icon_alignment variable. Provides alignment variables for generic component and filetype devicons. `component = { 'name', icon = {'*', align = 'right'} }` `component = { 'filetype', icon_alignment = 'right' }` * style: restore previous formatting on line 79 * refactor: set `self.option.icon` as table * fix: forgot non-color icon alignment * fix: remove redundant variable assignment * docs: add README examples for icon alignment Include configuration examples for icon alignment to README file. * test: add test for filetype components icon align Ensure testability for filetype icons being aligned to right side. --- README.md | 8 +++++++- lua/lualine/component.lua | 14 ++++++++++++-- lua/lualine/components/filetype.lua | 2 ++ tests/spec/component_spec.lua | 18 ++++++++++++++++++ 4 files changed, 39 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index ac3c896..4ec4564 100644 --- a/README.md +++ b/README.md @@ -362,6 +362,9 @@ sections = { -- 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 position can also be set to the right side from table. Example: + -- {'branch', icon = {'', align='right', color={fg='green'}}} icon = nil, separator = nil, -- Determines what separator to use for the component. @@ -568,7 +571,10 @@ sections = { { 'filetype', colored = true, -- Displays filetype icon in color if set to true - icon_only = false -- Display only an icon for filetype + icon_only = false, -- Display only an icon for filetype + icon = { align = 'right' }, -- Display filetype icon on the right hand side + -- icon = {'X', align='right'} + -- Icon string ^ in table is ignored in filetype component } } } diff --git a/lua/lualine/component.lua b/lua/lualine/component.lua index d0efee6..a427345 100644 --- a/lua/lualine/component.lua +++ b/lua/lualine/component.lua @@ -108,14 +108,22 @@ function M:apply_highlights(default_highlight) end end ----apply icon in front of component (prepemds component with icon) +---apply icon to component (appends/prepemds component with icon) function M:apply_icon() 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 + if self.options.icon_color_highlight and type(self.options.icon) == 'table' and self.options.icon.align == 'right' then + self.status = table.concat { + self.status, + ' ', + self:format_hl(self.options.icon_color_highlight), + icon, + self:get_default_hl(), + } + elseif self.options.icon_color_highlight then self.status = table.concat { self:format_hl(self.options.icon_color_highlight), icon, @@ -123,6 +131,8 @@ function M:apply_icon() ' ', self.status, } + elseif type(self.options.icon) == 'table' and self.options.icon.align == 'right' then + self.status = table.concat({ self.status, icon }, ' ') else self.status = table.concat({ icon, self.status }, ' ') end diff --git a/lua/lualine/components/filetype.lua b/lua/lualine/components/filetype.lua index 196e0ed..ff3963a 100644 --- a/lua/lualine/components/filetype.lua +++ b/lua/lualine/components/filetype.lua @@ -61,6 +61,8 @@ function M:apply_icon() if self.options.icon_only then self.status = icon + elseif type(self.options.icon) == 'table' and self.options.icon.align == 'right' then + self.status = self.status .. ' ' .. icon else self.status = icon .. ' ' .. self.status end diff --git a/tests/spec/component_spec.lua b/tests/spec/component_spec.lua index 8148d26..8255ddd 100644 --- a/tests/spec/component_spec.lua +++ b/tests/spec/component_spec.lua @@ -361,6 +361,24 @@ describe('Filetype component', function() assert_component('filetype', opts, '*') package.loaded['nvim-web-devicons'] = nil end) + + it('displays right aligned icon when icon.align is "right"', function() + package.loaded['nvim-web-devicons'] = { + get_icon = function() + return '*', 'test_highlight_group' + end, + } + + local opts = build_component_opts { + component_separators = { left = '', right = '' }, + padding = 0, + colored = false, + icon_only = false, + icon = { align = 'right' } + } + assert_component('filetype', opts, 'lua *') + package.loaded['nvim-web-devicons'] = nil + end) end) describe('Hostname component', function()