Skip to content

Bugfix: Parse ansi colors correctly when there are more than 10 graph lines #795

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 29, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions lua/neogit/lib/ansi.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ local colors = {
["37"] = "White", ["1;37"] = "BoldWhite",
}

local mark = "%"

---Parses a string with ansi-escape codes (colors) into a table
---@param str string
function M.parse(str, opts)
local colored = {}
local idx = 1

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

colored[tostring(idx)] = { text = text, color = colors[color] }
idx = idx + 1

return idx - 1
table.insert(colored, { text = text, color = colors[color] })
return mark
end)

local out = {}
for g in parsed:gmatch(".") do
if g:match("%d") then
table.insert(out, colored[g])
if g == mark then
table.insert(out, table.remove(colored, 1))
else
table.insert(out, { text = g, color = "Gray" })
end
end

assert(vim.tbl_isempty(colored), "ANSI Parser didn't consume all graph parts")

return out
end

Expand Down