| 1 |
-- Copyright (c) 2020-2021 shadmansaleh |
| 2 |
-- MIT license, see LICENSE for more details. |
| 3 |
local M = require('lualine.component'):extend() |
| 4 |
|
| 5 |
function M:update_status() |
| 6 |
local component = self.options[1] |
| 7 |
local ok, status |
| 8 |
if self.options.type == nil then |
| 9 |
ok, status = pcall(M.lua_eval, component) |
| 10 |
if not ok then |
| 11 |
status = M.vim_function(component) |
| 12 |
end |
| 13 |
else |
| 14 |
if self.options.type == 'lua_expr' then |
| 15 |
ok, status = pcall(M.lua_eval, component) |
| 16 |
if not ok then |
| 17 |
status = nil |
| 18 |
end |
| 19 |
elseif self.options.type == 'vim_fun' then |
| 20 |
status = M.vim_function(component) |
| 21 |
end |
| 22 |
end |
| 23 |
return status |
| 24 |
end |
| 25 |
|
| 26 |
---evaluate the lua code and return it's result as string |
| 27 |
---@param code string |
| 28 |
---@return string |
| 29 |
function M.lua_eval(code) |
| 30 |
local result = loadstring('return ' .. code)() |
| 31 |
assert(result, 'String expected got nil') |
| 32 |
return tostring(result) |
| 33 |
end |
| 34 |
|
| 35 |
---call vim function (name) and return it's result as string |
| 36 |
---@param name string |
| 37 |
---@return string |
| 38 |
function M.vim_function(name) |
| 39 |
-- vim function component |
| 40 |
local ok, return_val = pcall(vim.api.nvim_call_function, name, {}) |
| 41 |
if not ok then |
| 42 |
return '' |
| 43 |
end -- function call failed |
| 44 |
ok, return_val = pcall(tostring, return_val) |
| 45 |
return ok and return_val or '' |
| 46 |
end |
| 47 |
|
| 48 |
return M |