Skip to content

Commit

Permalink
feat(ui.results): show the number of results
Browse files Browse the repository at this point in the history
  • Loading branch information
danielefongo committed Apr 11, 2024
1 parent 57d25b7 commit 52b131b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
10 changes: 7 additions & 3 deletions lua/microscope/ui/results.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ local function get_focused(self)

local cursor = self:get_cursor()[1]
if self.data and self.data[cursor] then
return self.data[cursor]
return cursor, self.data[cursor]
end
end

local function on_empty_results_retrieved(self)
self:set_title("", "center")
self:clear()
end

Expand All @@ -37,6 +38,8 @@ local function on_new_request(self, request)
self.selected_data = {}
self.results = {}
self.request = request

self:set_title("", "center")
end

local function on_results_retrieved(self, list)
Expand Down Expand Up @@ -109,7 +112,7 @@ end

function results:selected()
local selected = vim.tbl_values(self.selected_data)
local focused = get_focused(self)
local _, focused = get_focused(self)

if #selected == 0 and focused then
return { focused }
Expand Down Expand Up @@ -141,8 +144,9 @@ end
function results:set_cursor(cursor)
window.set_cursor(self, cursor)
self:parse()
local focused = get_focused(self)
local idx, focused = get_focused(self)
if focused then
self:set_title(idx .. " / " .. #self.results, "center")
events:fire(events.event.result_focused, focused, 100)
end
end
Expand Down
34 changes: 34 additions & 0 deletions lua/tests/ui/results_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ describe("results", function()
assert.are.same(results_window:read(), { "result1", "result2" })
end)

it("updates title", function()
my_events:fire(events.event.results_retrieved, { "result1", "result2" })
helpers.wait(10)

assert.are.same(results_window:get_title(), { title = "1 / 2", title_pos = "center" })
end)

it("overwrites buffer on new results", function()
my_events:fire(events.event.results_retrieved, { "result1", "result2" })
helpers.wait(10)
Expand Down Expand Up @@ -68,6 +75,13 @@ describe("results", function()

assert.are.same(results_window:read(), { "" })
end)

it("updates title", function()
my_events:fire(events.event.empty_results_retrieved)
helpers.wait(10)

assert.are.same(results_window:get_title(), { title = "", title_pos = "center" })
end)
end)

describe("new_request", function()
Expand All @@ -82,6 +96,14 @@ describe("results", function()
assert.are.same(results_window:raw_results(), {})
assert.are.same(results_window:selected(), {})
end)

it("updates title", function()
results_window:set_title("any", "center")
my_events:fire(events.event.new_request)
helpers.wait(10)

assert.are.same(results_window:get_title(), { title = "", title_pos = "center" })
end)
end)

describe("new_opts", function()
Expand Down Expand Up @@ -115,6 +137,18 @@ describe("results", function()

assert.are.same(results_window:get_cursor(), { 2, 0 })
end)

it("updates title", function()
my_events:fire(events.event.results_retrieved, { "result1", "result2" })
helpers.wait(10)

results_window:show(helpers.dummy_layout(), false)
vim.api.nvim_win_set_cursor(results_window.win, { 2, 0 })
my_events:fire_native(events.event.cursor_moved)
helpers.wait(20)

assert.are.same(results_window:get_title(), { title = "2 / 2", title_pos = "center" })
end)
end)
end)

Expand Down

0 comments on commit 52b131b

Please sign in to comment.