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()