feat:Add component evaluation for inactive sections

This commit is contained in:
shadmansaleh 2021-07-25 12:52:52 +06:00
commit c57cb43c4d
4 changed files with 36 additions and 2 deletions

View File

@ -5,6 +5,14 @@ local highlight = require 'lualine.highlight'
-- Used to provide a unique id for each component -- Used to provide a unique id for each component
local component_no = 1 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 = { local Component = {
-- Creates a new component -- Creates a new component
new = function(self, options, child) new = function(self, options, child)
@ -19,6 +27,7 @@ local Component = {
new_component.options.component_name = tostring(component_no) new_component.options.component_name = tostring(component_no)
end end
new_component.component_no = component_no new_component.component_no = component_no
components[component_no] = new_component
new_component:set_separator() new_component:set_separator()
new_component:create_option_highlights() new_component:create_option_highlights()
end end
@ -148,7 +157,17 @@ local Component = {
-- luacheck: pop -- luacheck: pop
-- Driver code of the class -- 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))
return self.status
end
self.status = '' self.status = ''
if self.options.condition ~= nil and self.options.condition() ~= true then if self.options.condition ~= nil and self.options.condition() ~= true then
return self.status return self.status
@ -162,8 +181,10 @@ local Component = {
self:apply_padding() self:apply_padding()
self:apply_section_separators() self:apply_section_separators()
self:apply_highlights(default_highlight) self:apply_highlights(default_highlight)
if not (statusline_inactive and self.last_component) then
self:apply_separator() self:apply_separator()
end end
end
return self.status return self.status
end end
} }

View File

@ -2,6 +2,8 @@
-- MIT license, see LICENSE for more details. -- MIT license, see LICENSE for more details.
local FileName = require('lualine.component'):new() local FileName = require('lualine.component'):new()
FileName.inactive_eval = true
local function count(base, pattern) local function count(base, pattern)
return select(2, string.gsub(base, pattern, '')) return select(2, string.gsub(base, pattern, ''))
end end

View File

@ -38,6 +38,7 @@ function M.draw_section(section, section_name, is_focused)
.options.section_separators[1]) .options.section_separators[1])
end end
end end
section[component_no].last_component = true
end end
-- Remove component separator when color option is used in next component -- Remove component separator when color option is used in next component
if strip_next_component then if strip_next_component then

View File

@ -56,4 +56,14 @@ function M.list_shrink(list)
return new_list return new_list
end 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 return M