From 42d3fcdbc7a5231082570c5caeb974ad4304c316 Mon Sep 17 00:00:00 2001 From: Stephan Badragan Date: Mon, 6 May 2024 00:37:21 -0700 Subject: [PATCH] simplifying state --- lua/grug-far/render/resultsHeader.lua | 15 +++---- lua/grug-far/render/resultsList.lua | 63 +++++++++++---------------- 2 files changed, 32 insertions(+), 46 deletions(-) diff --git a/lua/grug-far/render/resultsHeader.lua b/lua/grug-far/render/resultsHeader.lua index bab51ada..02ef8f88 100644 --- a/lua/grug-far/render/resultsHeader.lua +++ b/lua/grug-far/render/resultsHeader.lua @@ -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 @@ -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, diff --git a/lua/grug-far/render/resultsList.lua b/lua/grug-far/render/resultsList.lua index a48b9598..c1ef707a 100644 --- a/lua/grug-far/render/resultsList.lua +++ b/lua/grug-far/render/resultsList.lua @@ -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 })