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:
parent
be83cb947e
commit
c9c5e3f9d1
|
@ -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,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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 not ok then status = EvalFuncComponent.vim_function(component) end
|
if self.options.type == nil then
|
||||||
|
ok, status = pcall(EvalFuncComponent.lua_eval, component)
|
||||||
|
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
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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
|
||||||
local stl_expr = component[1] -- Vim's %p %l statusline elements
|
end
|
||||||
component[1] = function() return stl_expr end
|
end,
|
||||||
loaded_component =
|
stl = function(component)
|
||||||
lualine_load({'lua', 'lualine', 'components', 'special', 'function_component'}, true):new(component)
|
local stl_expr = component[1] -- Vim's %p %l statusline elements
|
||||||
elseif component[1]:find('[gvtwb]?o?:') == 1 then
|
component[1] = function() return stl_expr end
|
||||||
loaded_component =
|
return
|
||||||
lualine_load({'lua', 'lualine', 'components', 'special', 'vim_var_component'}, true):new(component)
|
lualine_load({'lua', 'lualine', 'components', 'special', 'function_component'}, true):new(component)
|
||||||
else
|
end,
|
||||||
loaded_component =
|
var = function(component)
|
||||||
lualine_load({'lua', 'lualine', 'components', 'special', 'eval_func_component'}, true):new(component)
|
return
|
||||||
|
lualine_load({'lua', 'lualine', 'components', 'special', 'vim_var_component'}, true):new(component)
|
||||||
|
end,
|
||||||
|
['_'] = function(component)
|
||||||
|
return
|
||||||
|
lualine_load({'lua', 'lualine', 'components', 'special', 'eval_func_component'}, true):new(component)
|
||||||
|
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
|
||||||
|
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
|
||||||
return loaded_component
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue