fix: lualine crashing because of keyboard interrupt. (#534)

* fix: lualine crashing because of keyboard interrupt.

If keyboard interrupt occurs while lualine is evaluating statusline
lualine crashes completely. Since keyboard interrupt isn't something
we can handle try to mitigate the issue by retrying to evaluate the
statusline on error.Now lualine will retry 3 times before giving
up on an runtime error.

* Make last retry unprotected call
This commit is contained in:
Shadman 2022-01-31 17:34:53 +00:00 committed by GitHub
parent 9208bae98f
commit e6e75f5e92
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 2 deletions

View File

@ -141,7 +141,7 @@ end
--- component objects --- component objects
---@param is_focused boolean : whether being evsluated for focused window or not ---@param is_focused boolean : whether being evsluated for focused window or not
---@return string statusline string ---@return string statusline string
local function statusline(sections, is_focused) local statusline = modules.utils.retry_call_wrap(function(sections, is_focused)
-- The sequence sections should maintain [SECTION_SEQUENCE] -- The sequence sections should maintain [SECTION_SEQUENCE]
local section_sequence = { 'a', 'b', 'c', 'x', 'y', 'z' } local section_sequence = { 'a', 'b', 'c', 'x', 'y', 'z' }
local status = {} local status = {}
@ -173,7 +173,7 @@ local function statusline(sections, is_focused)
table.insert(status, modules.highlight.format_highlight('lualine_c') .. '%=') table.insert(status, modules.highlight.format_highlight('lualine_c') .. '%=')
end end
return apply_transitional_separators(table.concat(status)) return apply_transitional_separators(table.concat(status))
end end)
--- check if any extension matches the filetype and return proper sections --- check if any extension matches the filetype and return proper sections
---@param current_ft string : filetype name of current file ---@param current_ft string : filetype name of current file

View File

@ -143,4 +143,31 @@ function M.is_component(comp)
return mt and mt.__is_lualine_component == true return mt and mt.__is_lualine_component == true
end end
--- Call function with args and return it's result.
--- If error occurs during fn retry times times.
---@param fn function Function to call.
---@param args table List of arguments used for calling function.
---@param times number Number of times to retry on error.
---@return any Result of fn.
function M.retry_call(fn, args, times)
times = times or 3
for _=0,times-1 do
local result = {pcall(fn, unpack(args))}
if result[1] == true then
return unpack(result, 2)
end
end
return fn(unpack(args))
end
--- Wrap a function in retry_call
---@param fn function Function to call.
---@param times number Number of times to retry on error.
---@return function retry call wraped function
function M.retry_call_wrap(fn, times)
return function(...)
return M.retry_call(fn, {...}, times)
end
end
return M return M