feat(health): summary in section heading #33388

Problem:
As checkhealth grows, it is increasingly hard to quickly glance through
the information.

Solution:
Show a summary of ok, warn, and error outputs per section.
This commit is contained in:
Yochem van Rosmalen
2025-04-09 13:13:20 +02:00
committed by GitHub
parent ff2cbe8fac
commit 5a94edad70
3 changed files with 44 additions and 22 deletions

View File

@@ -106,6 +106,7 @@
local M = {}
local s_output = {} ---@type string[]
local check_summary = { warn = 0, error = 0 }
-- From a path return a list [{name}, {func}, {type}] representing a healthcheck
local function filepath_to_healthcheck(path)
@@ -286,6 +287,7 @@ end
function M.warn(msg, ...)
local input = format_report_message('⚠️ WARNING', msg, ...)
collect_output(input)
check_summary['warn'] = check_summary['warn'] + 1
end
--- Reports an error.
@@ -295,6 +297,7 @@ end
function M.error(msg, ...)
local input = format_report_message('❌ ERROR', msg, ...)
collect_output(input)
check_summary['error'] = check_summary['error'] + 1
end
local path2name = function(path)
@@ -341,6 +344,23 @@ M._complete = function()
return rv
end
--- Gets the results heading for the current report section.
---
---@return string
local function get_summary()
local s = ''
local errors = check_summary['error']
local warns = check_summary['warn']
s = s .. (warns > 0 and (' %2d ⚠️'):format(warns) or '')
s = s .. (errors > 0 and (' %2d ❌'):format(errors) or '')
if errors == 0 and warns == 0 then
s = s .. ''
end
return s
end
--- Runs the specified healthchecks.
--- Runs all discovered healthchecks if plugin_names is empty.
---
@@ -397,9 +417,9 @@ function M._check(mods, plugin_names)
local func = value[1]
local type = value[2]
s_output = {}
check_summary = { warn = 0, error = 0 }
if func == '' then
s_output = {}
M.error('No healthcheck found for "' .. name .. '" plugin.')
end
if type == 'v' then
@@ -420,10 +440,12 @@ function M._check(mods, plugin_names)
M.error('The healthcheck report for "' .. name .. '" plugin is empty.')
end
local report = get_summary()
local replen = vim.fn.strwidth(report)
local header = {
string.rep('=', 78),
-- Example: `foo.health: [ …] require("foo.health").check()`
('%s: %s%s'):format(name, (' '):rep(76 - name:len() - func:len()), func),
-- Example: `foo.health: [ …] 1 ⚠️ 5 ❌`
('%s: %s%s'):format(name, (' '):rep(76 - name:len() - replen), report),
'',
}