feat: add support for inline custom components

This commit is contained in:
shadmansaleh 2021-11-25 12:57:00 +06:00
parent 17668aa19d
commit c030a380ef
4 changed files with 60 additions and 8 deletions

View File

@ -18,6 +18,8 @@ function M:__tostring()
return str return str
end end
M.__is_lualine_component = true
---initialize new component ---initialize new component
---@param options table options for component ---@param options table options for component
function M:init(options) function M:init(options)

View File

@ -1,5 +1,7 @@
-- Copyright (c) 2020-2021 hoob3rt -- Copyright (c) 2020-2021 hoob3rt
-- MIT license, see LICENSE for more details. -- MIT license, see LICENSE for more details.
local utils = require 'lualine.utils.utils'
local config = { local config = {
options = { options = {
icons_enabled = true, icons_enabled = true,
@ -46,7 +48,7 @@ end
---@return table copy of config ---@return table copy of config
local function apply_configuration(config_table) local function apply_configuration(config_table)
if not config_table then if not config_table then
return vim.deepcopy(config) return utils.deepcopy(config)
end end
local function parse_sections(section_group_name) local function parse_sections(section_group_name)
if config_table[section_group_name] == nil then if config_table[section_group_name] == nil then
@ -57,7 +59,7 @@ local function apply_configuration(config_table)
return return
end end
for section_name, section in pairs(config_table[section_group_name]) do for section_name, section in pairs(config_table[section_group_name]) do
config[section_group_name][section_name] = vim.deepcopy(section) config[section_group_name][section_name] = utils.deepcopy(section)
end end
end end
parse_sections 'options' parse_sections 'options'
@ -65,17 +67,17 @@ local function apply_configuration(config_table)
parse_sections 'inactive_sections' parse_sections 'inactive_sections'
parse_sections 'tabline' parse_sections 'tabline'
if config_table.extensions then if config_table.extensions then
config.extensions = vim.deepcopy(config_table.extensions) config.extensions = utils.deepcopy(config_table.extensions)
end end
config.options.section_separators = fix_separators(config.options.section_separators) config.options.section_separators = fix_separators(config.options.section_separators)
config.options.component_separators = fix_separators(config.options.component_separators) config.options.component_separators = fix_separators(config.options.component_separators)
return vim.deepcopy(config) return utils.deepcopy(config)
end end
--- returns current active config --- returns current active config
---@return table a copy of config ---@return table a copy of config
local function get_current_config() local function get_current_config()
return vim.deepcopy(config) return utils.deepcopy(config)
end end
return { return {

View File

@ -4,6 +4,7 @@
local lualine_require = require 'lualine_require' local lualine_require = require 'lualine_require'
local require = lualine_require.require local require = lualine_require.require
local modules = lualine_require.lazy_require { local modules = lualine_require.lazy_require {
utils = 'lualine.utils.utils',
notice = 'lualine.utils.notices', notice = 'lualine.utils.notices',
} }
local is_valid_filename = lualine_require.is_valid_filename local is_valid_filename = lualine_require.is_valid_filename
@ -11,6 +12,10 @@ local sep = lualine_require.sep
--- function that loads specific type of component --- function that loads specific type of component
local component_types = { local component_types = {
-- loads custion component
custom = function(component)
return component[1](component)
end,
--- loads lua functions as component --- loads lua functions as component
lua_fun = function(component) lua_fun = function(component)
return require 'lualine.components.special.function_component'(component) return require 'lualine.components.special.function_component'(component)
@ -53,8 +58,7 @@ local component_types = {
local function component_loader(component) local function component_loader(component)
if type(component[1]) == 'function' then if type(component[1]) == 'function' then
return component_types.lua_fun(component) return component_types.lua_fun(component)
end elseif type(component[1]) == 'string' then
if type(component[1]) == 'string' then
-- load the component -- load the component
if component.type ~= nil then if component.type ~= nil then
if component_types[component.type] and component.type ~= 'lua_fun' then if component_types[component.type] and component.type ~= 'lua_fun' then
@ -81,6 +85,8 @@ component type '%s' isn't recognised. Check if spelling is correct.]],
else else
return component_types['_'](component) return component_types['_'](component)
end end
elseif type(component[1]) == 'table' then
return component_types.custom(component)
end end
end end
@ -168,7 +174,7 @@ end
local function load_sections(sections, options) local function load_sections(sections, options)
for section_name, section in pairs(sections) do for section_name, section in pairs(sections) do
for index, component in pairs(section) do for index, component in pairs(section) do
if type(component) == 'string' or type(component) == 'function' then if (type(component) == 'string' or type(component) == 'function') or modules.utils.is_component(component) then
component = { component } component = { component }
end end
if is_valid_component_type(index, component) then if is_valid_component_type(index, component) then

View File

@ -101,4 +101,46 @@ function M.charAt(str, pos)
return string.char(str:byte(pos)) return string.char(str:byte(pos))
end end
-- deepcopy adapted from penlight
-- https://github.com/lunarmodules/Penlight/blob/0653cdb05591454a9804a7fee8c873b8f06b0b8f/lua/pl/tablex.lua#L98-L122
local function cycle_aware_copy(t, cache)
if type(t) ~= 'table' then
return t
end
if cache[t] then
return cache[t]
end
local res = {}
cache[t] = res
local mt = getmetatable(t)
for k, v in pairs(t) do
k = cycle_aware_copy(k, cache)
v = cycle_aware_copy(v, cache)
res[k] = v
end
setmetatable(res, mt)
return res
end
--- make a deep copy of a table, recursively copying all the keys and fields.
-- This supports cycles in tables; cycles will be reproduced in the copy.
-- This will also set the copied table's metatable to that of the original.
-- @within Copying
-- @tab t A table
-- @return new table
function M.deepcopy(t)
return cycle_aware_copy(t, {})
end
--- Check if comp is a lualine component
--- @param comp any
--- @return boolean
function M.is_component(comp)
if type(comp) ~= 'table' then
return false
end
local mt = getmetatable(comp)
return mt and mt.__is_lualine_component == true
end
return M return M