Repository: nvim-lualine/lualine.nvim
Pull request #666 (2022-04-26 12:31:03)

feat: add option to invert component icon alignment
I was about to open an issue for this but figured it seemed easy enough.
Here is the issue template I was going to provide.

Set: `icon_right = true`

### Requested feature

Allow component icons to be displayed on both the left and right side.

![gimp-2 10_fO1cGKEU7J](https://user-images.githubusercontent.com/36192863/165288945-3527d022-4222-4f62-8e7c-112ab3d5c08c.gif)

### Motivation

Seems fairly simple and I like the look.

daephx: Another thing I was looking at was was allow this to be set in `icon = table`.
But I'm not sure on the best way to handle this.

https://github.com/nvim-lualine/lualine.nvim/blob/4ba702e5d13da9c2041679b84467345834adaabc/lua/lualine/component.lua#L115-L117

Where you could get away with putting:

```diff
    if type(icon) == 'table' then
+     self.options.icon_right = icon.icon_right
      icon = icon[1]
    end
```
Or some variation on the name `icon = { '', align_right = true`?
shadmansaleh: > So while the icon table options can be applied, the filetype component would need an option at a higher level from what I understand.

Why ?
I think we can make 
```lua
{'filetype', icon={align='right'}}
```
work . For filetype component icon gets ignored but it's still available in `self.options` table . We can just checif self.options.icon is a table and has align 'right' in it.

Patch:
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.lua b/lua/lualine.lua
index dc849b9..717d14c 100644
--- a/lua/lualine.lua
+++ b/lua/lualine.lua
@@ -269,10 +269,14 @@ local function set_statusline()
   if next(config.sections) ~= nil or next(config.inactive_sections) ~= nil then
     vim.cmd('autocmd lualine VimResized * redrawstatus')
     vim.go.statusline = "%{%v:lua.require'lualine'.statusline()%}"
-    vim.go.laststatus = config.options.globalstatus and 3 or 2
+    if config.options.globalstatus then
+      vim.go.laststatus = 3
+    end
   elseif vim.go.statusline == "%{%v:lua.require'lualine'.statusline()%}" then
     vim.go.statusline = ''
-    vim.go.laststatus = 2
+    if config.options.globalstatus then
+      vim.go.laststatus = 2
+    end
   end
 end
 
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


Test patch:
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()