Skip to content

Commit

Permalink
fixing issues due to buffering
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephan Badragan committed May 8, 2024
1 parent b927bfa commit fea3267
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
18 changes: 17 additions & 1 deletion lua/grug-far/rg/fetchWithRg.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
local utils = require('grug-far/utils')
local uv = vim.loop

local function fetchWithRg(params)
Expand Down Expand Up @@ -40,6 +41,7 @@ local function fetchWithRg(params)
uv.kill(pid, 'sigkill')
end

local lastLine = ''
uv.read_start(stdout, function(err, data)
if isAborted then
return
Expand All @@ -51,7 +53,21 @@ local function fetchWithRg(params)
end

if data then
on_fetch_chunk(data)
-- large outputs can cause the last line to be truncated
-- save it and prepend to next chunk
local chunkData = lastLine .. data
local i = utils.strFindLast(chunkData, "\n")
if i then
chunkData = string.sub(chunkData, 1, i)
lastLine = string.sub(chunkData, i + 1, -1)
on_fetch_chunk(chunkData)
else
lastLine = chunkData
end
else
if #lastLine > 0 then
on_fetch_chunk(lastLine)
end
end
end)

Expand Down
17 changes: 17 additions & 0 deletions lua/grug-far/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,21 @@ function M.debounce(callback, timeout)
end
end

function M.strFindLast(str, substr)
local i = 0
local j = nil
while true do
local i2, j2 = string.find(str, substr, i + 1, true)
if i2 == nil then break end
i = i2
j = j2
end

if j == nil then
return nil, nil
end

return i, j
end

return M

0 comments on commit fea3267

Please sign in to comment.