diff --git a/lua/lualine.lua b/lua/lualine.lua index dfa4d16..2e2d1bc 100644 --- a/lua/lualine.lua +++ b/lua/lualine.lua @@ -35,8 +35,32 @@ M.inactive_sections = { lualine_z = { } } -M.extensions = { -} +M.extensions = {} + +local function apply_configuration(config_table) + if not config_table then return end + local function parse_sections(section_group_name) + if not config_table[section_group_name] then return end + for section_name, section in pairs(config_table[section_group_name]) do + M[section_group_name][section_name] = config_table[section_group_name][section_name] + if type(section) == 'table' then + for _, component in pairs(section) do + if type(component) == 'table' and type(component[2]) == 'table' then + local options = component[2] + component[2] = nil + for key, val in pairs(options) do + component[key] = val + end + end + end + end + end + end + parse_sections('options') + parse_sections('sections') + parse_sections('inactive_sections') + if config_table.extensions then M.extensions = config_table.extensions end +end local function check_single_separator() local compoennt_separator = M.options.component_separators @@ -126,36 +150,31 @@ local function component_loader(component) end end - -local function load_components() - local function load_sections(sections) - for section_name, section in pairs(sections) do - for index, component in pairs(section) do - if type(component) == 'string' or type(component) == 'function' then - component = {component} - end - component.self = {} - component.self.section = section_name - component_loader(component) - section[index] = component +local function load_sections(sections) + for section_name, section in pairs(sections) do + for index, component in pairs(section) do + if type(component) == 'string' or type(component) == 'function' then + component = {component} end + component.self = {} + component.self.section = section_name + component_loader(component) + section[index] = component end end +end + +local function load_components() load_sections(M.sections) load_sections(M.inactive_sections) end local function load_extensions() - for _, extension in pairs(M.extensions) do - if type(extension) == 'string' then - require('lualine.extensions.' .. extension).load_extension() - end - if type(extension) == 'table' then - extension.load_extension() - end - if type(extension) == 'function' then - extension() - end + for index, extension in pairs(M.extensions) do + local local_extension = require('lualine.extensions.' .. extension) + load_sections(local_extension.sections) + load_sections(local_extension.inactive_sections) + M.extensions[index] = local_extension end end @@ -181,7 +200,27 @@ local function lualine_set_theme() theme_set = M.options.theme end -local function statusline(sections, is_focused) +local function statusline(is_focused) + local sections = nil + for _, extension in ipairs(M.extensions) do + for _, filetype in ipairs(extension.filetypes) do + if vim.bo.filetype == filetype then + if is_focused then + sections = extension.sections + else + sections = extension.inactive_sections + end + break + end + end + end + if sections == nil then + if is_focused then + sections = M.sections + else + sections = M.inactive_sections + end + end if M.options.theme ~= theme_set then _G.lualine_set_theme() end @@ -252,9 +291,9 @@ end local function status_dispatch() if vim.g.statusline_winid == vim.fn.win_getid() then - return statusline(M.sections, true) + return statusline(true) else - return statusline(M.inactive_sections, false) + return statusline(false) end end