From ef4723533011ec52e9aab55327de70997ba2482c Mon Sep 17 00:00:00 2001 From: shadmansaleh Date: Tue, 4 May 2021 10:56:58 +0600 Subject: [PATCH] Add component evaluation for inactive sections --- lua/lualine/component.lua | 34 ++++++++++++++++++++++++++--- lua/lualine/components/filename.lua | 2 ++ lua/lualine/utils/section.lua | 1 + lua/lualine/utils/utils.lua | 10 +++++++++ 4 files changed, 44 insertions(+), 3 deletions(-) diff --git a/lua/lualine/component.lua b/lua/lualine/component.lua index a7877c1..dfe67d9 100644 --- a/lua/lualine/component.lua +++ b/lua/lualine/component.lua @@ -3,6 +3,14 @@ local highlight = require 'lualine.highlight' -- Used to provide a unique id for each component local component_no = 1 +-- Here we're manupulation the require() cache so when we +-- require('lualine.component.components') it will return this table +-- It's hacky but package.loaded is documented in lua docs so from +-- standereds point of view we're good ]. I think it's better than +-- modifiying global state +package.loaded['lualine.component.components'] = {} +local components = package.loaded['lualine.component.components'] + local Component = { -- Creates a new component new = function(self, options, child) @@ -17,6 +25,7 @@ local Component = { new_component.options.component_name = tostring(component_no) end new_component.component_no = component_no + components[component_no] = new_component new_component:set_separator() new_component:create_option_highlights() end @@ -106,6 +115,7 @@ local Component = { end, strip_separator = function(self, default_highlight) + if self.status:find('%%{') == 1 then default_highlight = '' end if not default_highlight then default_highlight = '' end if not self.applied_separator then self.applied_separator = '' end self.status = self.status:sub(1, (#self.status - @@ -123,7 +133,20 @@ local Component = { -- luacheck: pop -- Driver code of the class - draw = function(self, default_highlight) + draw = function(self, default_highlight, statusline_inactive) + -- Check if we are in in inactive state and need to enable inactive_eval + -- for this compoennt + if self.inactive_eval and not statusline_inactive and vim.g.statusline_winid ~= + vim.fn.win_getid() then + -- In that case we'll return a evaluator + self.status = '%' .. string.format( + '{v:lua.require\'lualine.utils.utils\'.lualine_eval(%s,\'\',v:true)}', + tostring(self.component_no)) + -- Need to apply the highlights early as %{} escapes % :( + -- I'm not sure if it's a good idea. But don't have an option. + self:apply_highlights(default_highlight) + return self.status + end self.status = '' if self.options.condition ~= nil and self.options.condition() ~= true then return self.status @@ -135,8 +158,13 @@ local Component = { self:apply_icon() self:apply_case() self:apply_padding() - self:apply_highlights(default_highlight) - self:apply_separator() + if not statusline_inactive then + -- incase of inactive eval highlight hasbeen pre applied + self:apply_highlights(default_highlight) + end + if not (statusline_inactive and self.last_conponent) then + self:apply_separator() + end end return self.status end diff --git a/lua/lualine/components/filename.lua b/lua/lualine/components/filename.lua index d43c60b..1230934 100644 --- a/lua/lualine/components/filename.lua +++ b/lua/lualine/components/filename.lua @@ -2,6 +2,8 @@ -- MIT license, see LICENSE for more details. local FileName = require('lualine.component'):new() +FileName.inactive_eval = true + local function count(base, pattern) return select(2, string.gsub(base, pattern, '')) end diff --git a/lua/lualine/utils/section.lua b/lua/lualine/utils/section.lua index f8a12f8..007bbda 100644 --- a/lua/lualine/utils/section.lua +++ b/lua/lualine/utils/section.lua @@ -25,6 +25,7 @@ function M.draw_section(section, highlight_name) last_component_found = true status[component_no] = section[component_no]:strip_separator( highlight_name) + section[component_no].last_conponent = true end -- Remove component separator when color option is used in next component if next_component_colored then diff --git a/lua/lualine/utils/utils.lua b/lua/lualine/utils/utils.lua index 49bf2d4..59da208 100644 --- a/lua/lualine/utils/utils.lua +++ b/lua/lualine/utils/utils.lua @@ -56,4 +56,14 @@ function M.list_shrink(list) return new_list end +-- Wvaluate a component +function M.lualine_eval(id, ...) + local ok, components = pcall(require, 'lualine.component.components') + if ok and components then + return components[id]:draw(...) + else + return '' + end +end + return M