lualine.nvim/lua/lualine/components/special/eval_func_component.lua
shadmansaleh c9c5e3f9d1 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.
2021-08-22 13:54:05 +06:00

35 lines
1.1 KiB
Lua

local EvalFuncComponent = require('lualine.component'):new()
EvalFuncComponent.update_status = function(self)
local component = self.options[1]
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
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
end
EvalFuncComponent.lua_eval = function(code)
local result = loadstring('return ' .. code)()
assert(result, 'String expected got nil')
return tostring(result)
end
EvalFuncComponent.vim_function = function(name)
-- vim function component
local ok, return_val = pcall(vim.fn[name])
if not ok then return '' end -- function call failed
ok, return_val = pcall(tostring, return_val)
return ok and return_val or ''
end
return EvalFuncComponent