2021-09-20 11:46:38 +00:00
|
|
|
-- Copyright (c) 2020-2021 shadmansaleh
|
|
|
|
-- MIT license, see LICENSE for more details.
|
2021-09-25 17:15:42 +00:00
|
|
|
local M = require('lualine.component'):extend()
|
2021-09-20 11:46:38 +00:00
|
|
|
|
2021-09-25 17:15:42 +00:00
|
|
|
function M:update_status()
|
2021-04-11 08:20:41 +00:00
|
|
|
local component = self.options[1]
|
2021-08-20 12:32:56 +00:00
|
|
|
local ok, status
|
|
|
|
if self.options.type == nil then
|
2021-09-25 17:15:42 +00:00
|
|
|
ok, status = pcall(M.lua_eval, component)
|
2021-09-03 18:28:20 +00:00
|
|
|
if not ok then
|
2021-09-25 17:15:42 +00:00
|
|
|
status = M.vim_function(component)
|
2021-09-03 18:28:20 +00:00
|
|
|
end
|
2021-08-20 12:32:56 +00:00
|
|
|
else
|
2021-10-11 07:47:37 +00:00
|
|
|
if self.options.type == 'lua_expr' then
|
2021-09-25 17:15:42 +00:00
|
|
|
ok, status = pcall(M.lua_eval, component)
|
2021-09-03 18:28:20 +00:00
|
|
|
if not ok then
|
|
|
|
status = nil
|
|
|
|
end
|
2021-10-11 07:47:37 +00:00
|
|
|
elseif self.options.type == 'vim_fun' then
|
2021-09-25 17:15:42 +00:00
|
|
|
status = M.vim_function(component)
|
2021-08-20 12:32:56 +00:00
|
|
|
end
|
|
|
|
end
|
2021-04-11 08:20:41 +00:00
|
|
|
return status
|
|
|
|
end
|
|
|
|
|
2021-09-25 17:15:42 +00:00
|
|
|
function M.lua_eval(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
|
|
|
|
|
2021-09-25 17:15:42 +00:00
|
|
|
function M.vim_function(name)
|
2021-04-11 08:20:41 +00:00
|
|
|
-- 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
|
|
|
|
|
2021-09-25 17:15:42 +00:00
|
|
|
return M
|