From a643a24d9437e3499e3d813c1d63e5e6acff9e7d Mon Sep 17 00:00:00 2001
From: shadmansaleh <13149513+shadmansaleh@users.noreply.github.com>
Date: Fri, 3 Sep 2021 18:19:00 +0600
Subject: [PATCH] enhance: improve function component

- provide self as 1st argument
- handle error in function call
- convert the result to string by default
---
 lua/lualine/components/special/function_component.lua | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/lua/lualine/components/special/function_component.lua b/lua/lualine/components/special/function_component.lua
index 132bd27..23deea9 100644
--- a/lua/lualine/components/special/function_component.lua
+++ b/lua/lualine/components/special/function_component.lua
@@ -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