enhance: improve function component

- provide self as 1st argument
- handle error in function call
- convert the result to string by default
This commit is contained in:
shadmansaleh 2021-09-03 18:19:00 +06:00
parent c0f53e4acd
commit a643a24d94
1 changed files with 8 additions and 1 deletions

View File

@ -2,7 +2,14 @@ local FunctionComponent = require('lualine.component'):new()
FunctionComponent.update_status = function(self, is_focused)
-- 1st element in options table is the function provided by config
return self.options[1](is_focused)
local ok, retval
ok, retval = pcall(self.options[1], self, is_focused)
if not ok then return '' end
if type(retval) ~= 'string' then
ok, retval = pcall(tostring, retval)
if not ok then return '' end
end
return retval
end
return FunctionComponent