Skip to content

Commit c9ff487

Browse files
committed
refactor(printer): annotations and replaced deprecated functions
Signed-off-by: Guennadi Maximov C <g.maxc.fox@protonmail.com>
1 parent b211d79 commit c9ff487

1 file changed

Lines changed: 60 additions & 39 deletions

File tree

lua/quicktest/colored_printer.lua

Lines changed: 60 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
local api = vim.api
22

3+
---@class ColoredPrinter
4+
---@field color_groups? table<string, string>
5+
---@field current_fg? string
6+
---@field current_bg? string
7+
---@field current_styles? string[]
8+
---@field bright_color_bold? boolean
39
local ColoredPrinter = {}
410
ColoredPrinter.__index = ColoredPrinter
511

@@ -14,6 +20,7 @@ function ColoredPrinter.new()
1420
return self
1521
end
1622

23+
---@param index integer
1724
function ColoredPrinter:get_256_color(index)
1825
-- Standard 16 colors (0-15)
1926
if index <= 15 then
@@ -52,13 +59,13 @@ function ColoredPrinter:get_256_color(index)
5259
return 55 + c * 40
5360
end
5461

55-
return string.format("#%02x%02x%02x", color_value(r), color_value(g), color_value(b))
62+
return ("#%02x%02x%02x"):format(color_value(r), color_value(g), color_value(b))
5663
end
5764

5865
-- Grayscale (232-255)
5966
if index <= 255 then
6067
local gray = 8 + (index - 232) * 10
61-
return string.format("#%02x%02x%02x", gray, gray, gray)
68+
return ("#%02x%02x%02x"):format(gray, gray, gray)
6269
end
6370

6471
return "#ffffff" -- fallback
@@ -86,7 +93,8 @@ function ColoredPrinter:setup_highlight_groups()
8693

8794
for code, color in pairs(basic_colors) do
8895
local group_name = "QuicktestAnsiColor_" .. code
89-
vim.cmd(string.format("highlight %s ctermfg=%s guifg=%s", group_name, color:lower(), color))
96+
api.nvim_set_hl(0, group_name, { ctermfg = color:lower(), fg = color })
97+
-- vim.cmd(("highlight %s ctermfg=%s guifg=%s"):format(group_name, color:lower(), color))
9098

9199
self.color_groups[code] = group_name
92100
end
@@ -113,46 +121,55 @@ function ColoredPrinter:setup_highlight_groups()
113121

114122
for code, color in pairs(bg_colors) do
115123
local group_name = "QuicktestAnsiBgColor_" .. code
116-
vim.cmd(string.format("highlight %s ctermbg=%s guibg=%s", group_name, color:lower(), color))
124+
api.nvim_set_hl(0, group_name, { ctermbg = color:lower(), bg = color })
125+
-- vim.cmd(("highlight %s ctermbg=%s guibg=%s"):format(group_name, color:lower(), color))
117126

118127
self.color_groups[code] = group_name
119128
end
120129

121130
-- Use Normal highlight group directly to ensure proper default colors
122131
vim.cmd("highlight default QuicktestAnsiColorDefault guifg=NONE guibg=NONE")
123-
vim.cmd("highlight default link QuicktestAnsiColorDefault Normal")
132+
api.nvim_set_hl(0, "QuicktestAnsiColorDefault", { link = "Normal" })
133+
-- vim.cmd("highlight default link QuicktestAnsiColorDefault Normal")
124134
self.color_groups["default"] = "QuicktestAnsiColorDefault"
125135
end
126136

137+
---@param fg string
138+
---@param bg string
139+
---@param styles string[]
140+
---@return string
127141
function ColoredPrinter:get_or_create_color_group(fg, bg, styles)
128142
-- If no colors or styles are set, use the default group
129-
if not fg and not bg and (#styles == 0) then
143+
if not (fg or bg) and vim.tbl_isempty(styles) then
130144
return self.color_groups["default"]
131145
end
132146

147+
---@param str string
133148
local function sanitize(str)
134-
if str then
135-
-- Replace # with "hex" and any non-alphanumeric characters with their hex code
136-
return str:gsub("#", ""):gsub("[^%w]", function(c)
137-
return string.format("%02x", string.byte(c))
138-
end)
149+
if not str then
150+
return ""
139151
end
140-
return ""
152+
153+
-- Replace # with "hex" and any non-alphanumeric characters with their hex code
154+
return str:gsub("#", ""):gsub("[^%w]", function(c) ---@param c string
155+
return ("%02x"):format(c:byte())
156+
end)
141157
end
142158

159+
---@type table<string, boolean>
143160
local unique_styles = {}
144161
for _, style in ipairs(styles) do
145162
unique_styles[style] = true
146163
end
147-
unique_styles = vim.tbl_keys(unique_styles)
164+
148165
-- sort and uniqize the styles
149-
styles = vim.tbl_filter(function(s)
166+
styles = vim.tbl_filter(function(s) ---@param s string
150167
return s ~= ""
151-
end, unique_styles)
168+
end, vim.tbl_keys(unique_styles))
152169
table.sort(styles)
153170

154171
local color_key = table.concat(
155-
vim.tbl_filter(function(s)
172+
vim.tbl_filter(function(s) ---@param s string
156173
return s ~= ""
157174
end, { sanitize(fg), sanitize(bg), table.concat(styles, "_") }),
158175
"_"
@@ -161,7 +178,7 @@ function ColoredPrinter:get_or_create_color_group(fg, bg, styles)
161178
if not self.color_groups[color_key] then
162179
local group_name = "QuicktestAnsiColor"
163180

164-
if #color_key > 0 then
181+
if color_key:len() > 0 then
165182
group_name = group_name .. "_" .. color_key
166183
end
167184

@@ -170,33 +187,33 @@ function ColoredPrinter:get_or_create_color_group(fg, bg, styles)
170187

171188
if fg then
172189
if fg:match("^#") then
173-
cmd = cmd .. string.format(" guifg=%s", fg)
190+
cmd = ("%s guifg=%s"):format(cmd, fg)
174191
elseif self.color_groups[fg] then
175192
local fg_color = vim.fn.synIDattr(vim.fn.synIDtrans(vim.fn.hlID(self.color_groups[fg])), "fg#")
176193
if fg_color and fg_color ~= "" then
177-
cmd = cmd .. " guifg=" .. fg_color
194+
cmd = ("%s guifg=%s"):format(cmd, fg_color)
178195
end
179196
end
180197
end
181198

182199
if bg then
183200
if bg:match("^#") then
184-
cmd = cmd .. string.format(" guibg=%s", bg)
201+
cmd = ("%s guibg=%s"):format(cmd, bg)
185202
elseif self.color_groups[bg] then
186203
local bg_color = vim.fn.synIDattr(vim.fn.synIDtrans(vim.fn.hlID(self.color_groups[bg])), "bg#")
187204
if bg_color and bg_color ~= "" then
188-
cmd = cmd .. " guibg=" .. bg_color
205+
cmd = ("%s guibg=%s"):format(cmd, bg_color)
189206
end
190207
end
191208
end
192209

193-
if #styles > 0 then
210+
if not vim.tbl_isempty(styles) then
194211
cmd = cmd .. " gui=" .. table.concat(styles, ",")
195212
end
196213

197214
if start_cmd ~= cmd then
198215
---@diagnostic disable-next-line: param-type-mismatch
199-
local success, err = pcall(vim.cmd, cmd)
216+
local success = pcall(vim.cmd, cmd)
200217
if not success then
201218
-- Fallback to basic highlight if command fails
202219
vim.cmd("highlight " .. group_name)
@@ -208,6 +225,7 @@ function ColoredPrinter:get_or_create_color_group(fg, bg, styles)
208225
return self.color_groups[color_key]
209226
end
210227

228+
---@param line string
211229
function ColoredPrinter:parse_colors(line)
212230
local result = {}
213231
local highlights = {}
@@ -222,7 +240,7 @@ function ColoredPrinter:parse_colors(line)
222240
color_start = #result
223241
end
224242

225-
while i <= #line do
243+
while i <= line:len() do
226244
if line:sub(i, i) == "\27" and line:sub(i + 1, i + 1) == "[" then
227245
local j = line:find("m", i + 2)
228246
if j then
@@ -321,17 +339,18 @@ function ColoredPrinter:parse_colors(line)
321339
local g = tonumber(codes[index + 3])
322340
local b = tonumber(codes[index + 4])
323341
if r and g and b then
342+
local hex = ("#%02x%02x%02x"):format(r, g, b)
324343
if code == 38 then
325-
self.current_fg = string.format("#%02x%02x%02x", r, g, b)
344+
self.current_fg = hex
326345
else
327-
self.current_bg = string.format("#%02x%02x%02x", r, g, b)
346+
self.current_bg = hex
328347
end
329348
index = index + 4
330349
end
331350
end
332351
elseif codes[index + 1] == "5" then
333352
if #codes >= index + 2 then
334-
local color_index = tonumber(codes[index + 2])
353+
local color_index = tonumber(codes[index + 2]) ---@cast color_index integer
335354
if color_index and color_index >= 0 and color_index <= 255 then
336355
local color_hex = self:get_256_color(color_index)
337356
if code == 38 then
@@ -369,19 +388,19 @@ local function get_highlight_def(group)
369388
return "highlight AnsiColor"
370389
end
371390

372-
local hl = vim.api.nvim_get_hl_by_name(group, true)
373-
local hl_string = string.format("highlight %s", group)
391+
local hl = api.nvim_get_hl(0, { name = group })
392+
local hl_string = ("highlight %s"):format(group)
374393

375-
if hl.foreground then
376-
hl_string = hl_string .. string.format(" guifg=#%06x", hl.foreground)
394+
if hl.fg then
395+
hl_string = ("%s guifg=#%06x"):format(hl_string, hl.fg)
377396
end
378397

379-
if hl.background then
380-
hl_string = hl_string .. string.format(" guibg=#%06x", hl.background)
398+
if hl.bg then
399+
hl_string = ("%s guibg=#%06x"):format(hl_string, hl.bg)
381400
end
382401

383-
if hl.special then
384-
hl_string = hl_string .. string.format(" guisp=#%06x", hl.special)
402+
if hl.sp then
403+
hl_string = ("%s guisp=#%06x"):format(hl_string, hl.sp)
385404
end
386405

387406
local gui_attrs = {}
@@ -391,13 +410,14 @@ local function get_highlight_def(group)
391410
end
392411
end
393412

394-
if #gui_attrs > 0 then
395-
hl_string = hl_string .. " gui=" .. table.concat(gui_attrs, ",")
413+
if not vim.tbl_isempty(gui_attrs) then
414+
hl_string = ("%s gui=%s"):format(hl_string, table.concat(gui_attrs, ","))
396415
end
397416

398417
return hl_string
399418
end
400419

420+
---@param buf integer
401421
function ColoredPrinter:set_next_lines(lines, buf, lines_count)
402422
local parsed_lines = {}
403423
local parsed_highlights = {}
@@ -414,7 +434,8 @@ function ColoredPrinter:set_next_lines(lines, buf, lines_count)
414434
for _, h in ipairs(hl) do
415435
-- print("[" .. h.start .. "," .. h.end_ .. ")", get_highlight_def(h.group))
416436

417-
api.nvim_buf_add_highlight(buf, -1, h.group, lines_count - 1 + i, h.start, h.end_)
437+
vim.hl.range(buf, -1, h.group, { lines_count -1 + i, h.start }, { lines_count -1 + i, h.end_ })
438+
-- api.nvim_buf_add_highlight(buf, -1, h.group, lines_count - 1 + i, h.start, h.end_)
418439
end
419440
end
420441

@@ -428,7 +449,7 @@ function ColoredPrinter:debug_print_colors(line)
428449
print("Original line:", line)
429450
print("Parsed line:", plain_line)
430451
for _, hl in ipairs(highlights) do
431-
print("[" .. hl.start .. "," .. hl.end_ .. ")", get_highlight_def(hl.group))
452+
print(("[%s,%s)"):format(hl.start, hl.end_), get_highlight_def(hl.group))
432453
end
433454
end
434455

0 commit comments

Comments
 (0)