feat(ansi): better logging and option to disab;e

This commit is contained in:
Barrett Ruth 2025-09-20 14:37:51 -04:00
parent bd81743274
commit f60f6dd5bb
8 changed files with 159 additions and 10 deletions

View file

@ -192,6 +192,24 @@ function M.setup_highlight_groups()
BrightWhite = vim.g.terminal_color_15,
}
local missing_colors = {}
for color_name, terminal_color in pairs(color_map) do
if terminal_color == nil then
table.insert(missing_colors, color_name)
end
end
if #missing_colors > 0 then
vim.notify(
string.format(
'[cp.nvim] Terminal colors not configured: %s. ANSI colors will not display properly. '
.. 'Set vim.g.terminal_color_* variables or use a colorscheme that provides them.',
table.concat(missing_colors, ', ')
),
vim.log.levels.WARN
)
end
local combinations = {
{ bold = false, italic = false },
{ bold = true, italic = false },
@ -202,7 +220,7 @@ function M.setup_highlight_groups()
for _, combo in ipairs(combinations) do
for color_name, terminal_color in pairs(color_map) do
local parts = { 'CpAnsi' }
local opts = { fg = terminal_color }
local opts = { fg = terminal_color or 'NONE' }
if combo.bold then
table.insert(parts, 'Bold')

View file

@ -30,6 +30,7 @@
---@field setup_code? fun(ctx: ProblemContext)
---@class RunPanelConfig
---@field ansi boolean Enable ANSI color parsing and highlighting
---@field diff_mode "vim"|"git" Diff backend to use
---@field next_test_key string Key to navigate to next test case
---@field prev_test_key string Key to navigate to previous test case
@ -97,6 +98,7 @@ M.defaults = {
scrapers = constants.PLATFORMS,
filename = nil,
run_panel = {
ansi = true,
diff_mode = 'vim',
next_test_key = '<c-n>',
prev_test_key = '<c-p>',
@ -186,6 +188,11 @@ function M.setup(user_config)
})
vim.validate({
ansi = {
config.run_panel.ansi,
'boolean',
'ansi color parsing must be enabled xor disabled',
},
diff_mode = {
config.run_panel.diff_mode,
function(value)

View file

@ -537,8 +537,10 @@ local function toggle_run_panel(is_debug)
refresh_run_panel()
vim.schedule(function()
local ansi = require('cp.ansi')
ansi.setup_highlight_groups()
if config.run_panel.ansi then
local ansi = require('cp.ansi')
ansi.setup_highlight_groups()
end
if current_diff_layout then
update_diff_panes()
end

View file

@ -241,9 +241,13 @@ local function run_single_test_case(ctx, contest_config, cp_config, test_case)
local actual_highlights = {}
if actual_output ~= '' then
local parsed = ansi.parse_ansi_text(actual_output)
actual_output = table.concat(parsed.lines, '\n')
actual_highlights = parsed.highlights
if cp_config.run_panel.ansi then
local parsed = ansi.parse_ansi_text(actual_output)
actual_output = table.concat(parsed.lines, '\n')
actual_highlights = parsed.highlights
else
actual_output = actual_output:gsub('\027%[[%d;]*[a-zA-Z]', '')
end
end
local max_lines = cp_config.run_panel.max_output_lines
@ -362,11 +366,18 @@ end
function M.handle_compilation_failure(compilation_output)
local ansi = require('cp.ansi')
local config = require('cp.config').setup()
-- Always parse the compilation output - it contains everything now
local parsed = ansi.parse_ansi_text(compilation_output or '')
local clean_text = table.concat(parsed.lines, '\n')
local highlights = parsed.highlights
local clean_text
local highlights = {}
if config.run_panel.ansi then
local parsed = ansi.parse_ansi_text(compilation_output or '')
clean_text = table.concat(parsed.lines, '\n')
highlights = parsed.highlights
else
clean_text = (compilation_output or ''):gsub('\027%[[%d;]*[a-zA-Z]', '')
end
for _, test_case in ipairs(run_panel_state.test_cases) do
test_case.status = 'fail'