lualine.nvim/lua/lualine/components/special/eval_func_component.lua

29 lines
789 B
Lua
Raw Normal View History

local EvalFuncComponent = require('lualine.component'):new()
EvalFuncComponent.update_status = function(self)
local component = self.options[1]
local ok, status = pcall(EvalFuncComponent.eval_lua, component)
2021-04-14 12:25:34 +00:00
if not ok then status = EvalFuncComponent.vim_function(component) end
return status
end
EvalFuncComponent.eval_lua = function(code)
2021-04-14 12:25:34 +00:00
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)
if ok then
return return_val
else
return ''
end
end
return EvalFuncComponent