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)
|
2021-09-03 18:28:20 +00:00
|
|
|
if not ok then
|
|
|
|
status = EvalFuncComponent.vim_function(component)
|
|
|
|
end
|
2021-08-20 12:32:56 +00:00
|
|
|
else
|
|
|
|
if self.options.type == 'luae' then
|
|
|
|
ok, status = pcall(EvalFuncComponent.lua_eval, component)
|
2021-09-03 18:28:20 +00:00
|
|
|
if not ok then
|
|
|
|
status = nil
|
|
|
|
end
|
2021-08-20 12:32:56 +00:00
|
|
|
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
|
2021-08-27 18:13:50 +00:00
|
|
|
local ok, return_val = pcall(vim.api.nvim_call_function, name, {})
|
2021-09-03 18:28:20 +00:00
|
|
|
if not ok then
|
|
|
|
return ''
|
|
|
|
end -- function call failed
|
2021-04-11 08:20:41 +00:00
|
|
|
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
|