Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephan Badragan authored and MagicDuck committed May 18, 2024
1 parent 2d37299 commit 0578379
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 25 deletions.
9 changes: 7 additions & 2 deletions lua/grug-far/actions/search.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ local fetchResults = require('grug-far/rg/fetchResults')
local renderResultsHeader = require('grug-far/render/resultsHeader')
local resultsList = require('grug-far/render/resultsList')

-- TODO (sbadragan): problem when you search if you go: -> ge -> g , it gets stuck
-- TODO (sbadragan): show progress when searching, helps with big searches
local function search(params)
local buf = params.buf
local context = params.context
Expand All @@ -20,6 +22,7 @@ local function search(params)
state.actionMessage = nil
renderResultsHeader(buf, context)
resultsList.clear(buf, context)
P('starting search')
end)

state.abortSearch = fetchResults({
Expand All @@ -37,12 +40,14 @@ local function search(params)
resultsList.appendResultsChunk(buf, context, data)
end),
on_finish = vim.schedule_wrap(function(status, errorMessage)
state.abortSearch = nil

P('finish search with status ' .. (status or 'nil'))
state.status = status
if status == 'error' then
state.stats = nil
resultsList.setError(buf, context, errorMessage)
elseif status == nil then
state.stats = nil
state.actionMessage = nil
end

renderResultsHeader(buf, context)
Expand Down
52 changes: 29 additions & 23 deletions lua/grug-far/rg/fetchWithRg.lua
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
local utils = require('grug-far/utils')
local uv = vim.loop

local function closeHandle(handle)
if handle and not handle:is_closing() then
handle:close()
end
end

local function fetchWithRg(params)
local on_fetch_chunk = params.on_fetch_chunk
local on_finish = params.on_finish
local args = params.args
local isAborted = false
local isFinished = false
local errorMessage = ''

if not args then
on_finish(nil)
on_finish(nil, nil)
return
end

Expand All @@ -23,43 +29,43 @@ local function fetchWithRg(params)
args = args
}, function(
code
--signal
-- signal
)
if not stdout:is_closing() then
stdout:close()
end
if not stderr:is_closing() then
stderr:close()
end
if handle and not handle:is_closing() then
handle:close()
if isFinished then
return
end

isFinished = true
closeHandle(stdout)
closeHandle(stderr)
closeHandle(handle)

if code > 0 and #errorMessage == 0 then
errorMessage = 'no matches'
end
local isSuccess = code == 0 and #errorMessage == 0

on_finish(isSuccess and 'success' or 'error', errorMessage);
end)

-- TODO (sbadragan): problem here in that we don't seem to be immediately aborting searches
local on_abort = function()
isAborted = true
if not stdout:is_closing() then
stdout:close()
end
if not stderr:is_closing() then
stderr:close()
end
if handle and not handle:is_closing() then
handle:close()
if isFinished then
return
end

isFinished = true
closeHandle(stdout)
closeHandle(stderr)
closeHandle(handle)
P('terminating search')
uv.kill(pid, uv.constants.SIGTERM)

on_finish(nil, nil);
end

local lastLine = ''
uv.read_start(stdout, function(err, data)
if isAborted then
if isFinished then
return
end

Expand Down Expand Up @@ -88,7 +94,7 @@ local function fetchWithRg(params)
end)

uv.read_start(stderr, function(err, data)
if isAborted then
if isFinished then
return
end

Expand Down

0 comments on commit 0578379

Please sign in to comment.