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.
This commit is contained in:
Daemon 2022-04-30 03:10:47 -07:00 committed by GitHub
parent dfac736a01
commit 1cdddc2456
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 39 additions and 3 deletions

View File

@ -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
}
}
}

View File

@ -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

View File

@ -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

View File

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