feat: Add type option to hint what type of component is the string

String values for component are way too overloaded . This means lualine
has to make lots of checks to understand how to load a string value
as a component . With type option users can specify exactly what they
want.
This commit is contained in:
shadmansaleh 2021-08-20 18:32:56 +06:00 committed by Shadman
parent be83cb947e
commit c9c5e3f9d1
5 changed files with 84 additions and 30 deletions

View File

@ -284,7 +284,14 @@ sections = {
-- color = {fg = '#rrggbb', bg= '#rrggbb', gui='style'} -- color = {fg = '#rrggbb', bg= '#rrggbb', gui='style'}
-- or highlight group -- or highlight group
-- color = "WarningMsg" -- color = "WarningMsg"
color = nil color = nil,
-- Type option specifies what type a component is.
-- When type is omitted lualine will guess it.
-- Available types [format: type_name(example)]
-- mod(branch/filename), stl(%f/%m), var(g:coc_status/bo:modifiable),
-- luae(lua expressions), vimf(viml function name)
-- luae is short for lua-expression and vimf is short fror vim-function
type = nil,
} }
} }
} }

View File

@ -220,7 +220,15 @@ Local options~
-- color = {fg = '#rrggbb', bg= '#rrggbb', gui='style'} -- color = {fg = '#rrggbb', bg= '#rrggbb', gui='style'}
-- or highlight group -- or highlight group
-- color = "WarningMsg" -- color = "WarningMsg"
color = nil color = nil,
-- Type option specifies what type a component is.
-- When type is omitted lualine will guess it.
-- Available types [format: type_name(example)]
-- mod(branch/filename), stl(%f/%m), var(g:coc_status/bo:modifiable),
-- luae(lua expressions), vimf(viml function name)
-- luae is short for lua-expression and vimf is short fror vim-function
-- vim_function(viml function name)
type = nil,
} }
} }
} }

View File

@ -2,12 +2,22 @@ local EvalFuncComponent = require('lualine.component'):new()
EvalFuncComponent.update_status = function(self) EvalFuncComponent.update_status = function(self)
local component = self.options[1] local component = self.options[1]
local ok, status = pcall(EvalFuncComponent.eval_lua, component) local ok, status
if self.options.type == nil then
ok, status = pcall(EvalFuncComponent.lua_eval, component)
if not ok then status = EvalFuncComponent.vim_function(component) end if not ok then status = EvalFuncComponent.vim_function(component) end
else
if self.options.type == 'luae' then
ok, status = pcall(EvalFuncComponent.lua_eval, component)
if not ok then status = nil end
elseif self.options.type == 'vimf' then
status = EvalFuncComponent.vim_function(component)
end
end
return status return status
end end
EvalFuncComponent.eval_lua = function(code) EvalFuncComponent.lua_eval = function(code)
local result = loadstring('return ' .. code)() local result = loadstring('return ' .. code)()
assert(result, 'String expected got nil') assert(result, 'String expected got nil')
return tostring(result) return tostring(result)
@ -18,11 +28,7 @@ EvalFuncComponent.vim_function = function(name)
local ok, return_val = pcall(vim.fn[name]) local ok, return_val = pcall(vim.fn[name])
if not ok then return '' end -- function call failed if not ok then return '' end -- function call failed
ok, return_val = pcall(tostring, return_val) ok, return_val = pcall(tostring, return_val)
if ok then return ok and return_val or ''
return return_val
else
return ''
end
end end
return EvalFuncComponent return EvalFuncComponent

View File

@ -8,12 +8,12 @@ VarComponent.update_status = function(self)
-- filters var portion from g:var -- filters var portion from g:var
local var_name = component:sub(#scope + 2, #component) local var_name = component:sub(#scope + 2, #component)
-- Displays nothing when veriable aren't present -- Displays nothing when veriable aren't present
if not (scope and var_name) then return '' end
local return_val = vim[scope][var_name] local return_val = vim[scope][var_name]
if return_val == nil then return '' end if return_val == nil then return '' end
local ok local ok
ok, return_val = pcall(tostring, return_val) ok, return_val = pcall(tostring, return_val)
if ok then return return_val end return ok and return_val or ''
return ''
end end
return VarComponent return VarComponent

View File

@ -60,31 +60,64 @@ local function lualine_load(patern, use_cache)
return retval return retval
end end
local function component_loader(component) local component_types = {
if type(component[1]) == 'function' then luaf = function(component)
return return
lualine_load({'lua', 'lualine', 'components', 'special', 'function_component'}, true):new(component) lualine_load({'lua', 'lualine', 'components', 'special', 'function_component'}, true):new(component)
end end,
if type(component[1]) == 'string' then mod = function(component)
-- load the component
local ok, loaded_component = pcall(lualine_load, local ok, loaded_component = pcall(lualine_load,
{'lua', 'lualine', 'components', component[1]}, true) {'lua', 'lualine', 'components', component[1]}, true)
if ok then if ok then
component.component_name = component[1] component.component_name = component[1]
loaded_component = loaded_component:new(component) loaded_component = loaded_component:new(component)
elseif string.char(component[1]:byte(1)) == '%' then return loaded_component
end
end,
stl = function(component)
local stl_expr = component[1] -- Vim's %p %l statusline elements local stl_expr = component[1] -- Vim's %p %l statusline elements
component[1] = function() return stl_expr end component[1] = function() return stl_expr end
loaded_component = return
lualine_load({'lua', 'lualine', 'components', 'special', 'function_component'}, true):new(component) lualine_load({'lua', 'lualine', 'components', 'special', 'function_component'}, true):new(component)
elseif component[1]:find('[gvtwb]?o?:') == 1 then end,
loaded_component = var = function(component)
return
lualine_load({'lua', 'lualine', 'components', 'special', 'vim_var_component'}, true):new(component) lualine_load({'lua', 'lualine', 'components', 'special', 'vim_var_component'}, true):new(component)
else end,
loaded_component = ['_'] = function(component)
return
lualine_load({'lua', 'lualine', 'components', 'special', 'eval_func_component'}, true):new(component) lualine_load({'lua', 'lualine', 'components', 'special', 'eval_func_component'}, true):new(component)
end end
}
local function component_loader(component)
if type(component[1]) == 'function' then
return component_types.luaf(component)
end
if type(component[1]) == 'string' then
-- load the component
if component.type ~= nil then
if component_types[component.type] and component.type ~= 'luaf' then
return component_types[component.type](component)
elseif component.type == 'vimf' or component.type == 'luae' then
return component_types['_'](component)
else
notice.add_notice(string.format([[
### component.type
component type '%s' isn't recognised. Check if spelling is correct.]], component.type))
end
end
local loaded_component = component_types.mod(component)
if loaded_component then
return loaded_component return loaded_component
elseif string.char(component[1]:byte(1)) == '%' then
return component_types.stl(component)
elseif component[1]:find('[gvtwb]?o?:') == 1 then
return component_types.var(component)
else
return component_types['_'](component)
end
end end
end end