Skip to content

Commit

Permalink
simplifying state
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephan Badragan committed May 6, 2024
1 parent 7cb0834 commit 42d3fcd
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 46 deletions.
15 changes: 6 additions & 9 deletions lua/grug-far/render/resultsHeader.lua
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
local opts = require('grug-far/opts')

local function getStatusText(context)
local s = context.state.status
if s.status == 'error' then
local status = context.state.status
if status == 'error' then
return opts.getIcon('resultsStatusError', context)
elseif s.status == 'success' then
elseif status == 'success' then
return opts.getIcon('resultsStatusSuccess', context)
elseif s.status == 'progress' then
elseif status == 'progress' then
local progress_icons = opts.getIcon('resultsStatusProgressSeq', context)
if progress_icons then
return progress_icons[(s.count % #progress_icons) + 1]
local progressCount = context.state.progressCount or 0
return progress_icons[(progressCount % #progress_icons) + 1]
else
return ''
end
Expand All @@ -27,10 +28,6 @@ end
local function renderResultsHeader(buf, context)
local headerRow = context.state.headerRow

if not context.state.status then
context.state.status = { status = nil }
end

context.extmarkIds.results_header = vim.api.nvim_buf_set_extmark(buf, context.namespace, headerRow, 0, {
id = context.extmarkIds.results_header,
end_row = headerRow,
Expand Down
63 changes: 26 additions & 37 deletions lua/grug-far/render/resultsList.lua
Original file line number Diff line number Diff line change
Expand Up @@ -60,56 +60,45 @@ local function bufAppendErrorChunk(buf, context, error)
end

local function renderResultsList(buf, context, inputs)
local headerRow = context.state.headerRow

-- TODO (sbadragan): this should somehow be outside, possibly in another file
-- so that it can be called by actions
-- TODO (sbadragan): possibly we should split off stats
-- so maybe functions like updateStatus, updateStats, updateCurrentAction
local function updateStatus(newStatus, stats)
context.state.status = newStatus
if newStatus.status == 'progress' then
if newStatus.count == 0 or not context.state.stats then
context.state.stats = { matches = 0, files = 0 }
end
if stats then
context.state.stats = {
matches = context.state.stats.matches + stats.matches,
files = context.state.stats.files + stats.files
}
end
elseif newStatus.status ~= 'success' then
context.state.stats = nil
end

renderResultsHeader(buf, context)
end

context.state.asyncFetchResultList = context.state.asyncFetchResultList or
local state = context.state
state.asyncFetchResultList = state.asyncFetchResultList or
utils.debounce(asyncFetchResultList, context.options.debounceMs)
context.state.asyncFetchResultList({
state.asyncFetchResultList({
inputs = inputs,
on_start = function()
updateStatus({ status = 'progress', count = 0 })
state.status = 'progress'
state.progressCount = 0
state.stats = { matches = 0, files = 0 }
renderResultsHeader(buf, context)

-- remove all lines after heading and add one blank line
local headerRow = state.headerRow
vim.api.nvim_buf_set_lines(buf, headerRow, -1, false, { "" })
context.state.lastErrorLine = headerRow + 1
context.state.stats = nil
state.lastErrorLine = headerRow + 1
end,
on_fetch_chunk = function(data)
local status = context.state.status
updateStatus({
status = 'progress',
count = status.count and status.count + 1 or 2
}, data.stats)
state.status = 'progress'
state.progressCount = state.progressCount + 1
state.stats = {
matches = state.stats.matches + data.stats.matches,
files = state.stats.files + data.stats.files
}
renderResultsHeader(buf, context)

bufAppendResultsChunk(buf, context, data)
end,
on_error = function(error)
updateStatus({ status = 'error' })
state.status = 'error'
state.progressCount = nil
state.stats = nil
renderResultsHeader(buf, context)

bufAppendErrorChunk(buf, context, error)
end,
on_finish = function(status)
updateStatus({ status = status })
state.status = status
state.progressCount = nil
renderResultsHeader(buf, context)
end,
context = context
})
Expand Down

0 comments on commit 42d3fcd

Please sign in to comment.