Skip to content

Commit e3f488c

Browse files
authored
Merge pull request #795 from NeogitOrg/fix-ansi-parsing
2 parents feb1a23 + 54ee9b3 commit e3f488c

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

lua/neogit/lib/ansi.lua

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@ local colors = {
1212
["37"] = "White", ["1;37"] = "BoldWhite",
1313
}
1414

15+
local mark = "%"
16+
1517
---Parses a string with ansi-escape codes (colors) into a table
1618
---@param str string
1719
function M.parse(str, opts)
1820
local colored = {}
19-
local idx = 1
2021

2122
local parsed, _ = str:gsub("(\27%[[;%d]*m.-\27%[m)", function(match)
2223
local color, text = match:match("\27%[([;%d]*)m(.-)\27%[m")
@@ -25,21 +26,21 @@ function M.parse(str, opts)
2526
color = "35"
2627
end
2728

28-
colored[tostring(idx)] = { text = text, color = colors[color] }
29-
idx = idx + 1
30-
31-
return idx - 1
29+
table.insert(colored, { text = text, color = colors[color] })
30+
return mark
3231
end)
3332

3433
local out = {}
3534
for g in parsed:gmatch(".") do
36-
if g:match("%d") then
37-
table.insert(out, colored[g])
35+
if g == mark then
36+
table.insert(out, table.remove(colored, 1))
3837
else
3938
table.insert(out, { text = g, color = "Gray" })
4039
end
4140
end
4241

42+
assert(vim.tbl_isempty(colored), "ANSI Parser didn't consume all graph parts")
43+
4344
return out
4445
end
4546

0 commit comments

Comments
 (0)