2021-04-11 08:20:41 +00:00
|
|
|
local EvalFuncComponent = require('lualine.component'):new()
|
|
|
|
|
|
|
|
EvalFuncComponent.update_status = function(self)
|
|
|
|
local component = self.options[1]
|
2021-08-20 12:32:56 +00:00
|
|
|
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
|
2021-04-11 08:20:41 +00:00
|
|
|
return status
|
|
|
|
end
|
|
|
|
|
2021-08-20 12:32:56 +00:00
|
|
|
EvalFuncComponent.lua_eval = function(code)
|
2021-04-14 12:25:34 +00:00
|
|
|
local result = loadstring('return ' .. code)()
|
|
|
|
assert(result, 'String expected got nil')
|
|
|
|
return tostring(result)
|
2021-04-11 08:20:41 +00:00
|
|
|
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)
|
2021-08-20 12:32:56 +00:00
|
|
|
return ok and return_val or ''
|
2021-04-11 08:20:41 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return EvalFuncComponent
|