Skip to content

Commit

Permalink
feat: add JSON output for query()
Browse files Browse the repository at this point in the history
  • Loading branch information
delphinus committed Sep 6, 2024
1 parent dbca328 commit fe21145
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
4 changes: 4 additions & 0 deletions lua/frecency/database.lua
Original file line number Diff line number Diff line change
Expand Up @@ -265,4 +265,8 @@ function Database:file_lock()
return self._file_lock
end

function Database:sync()
self.tbl:sync()
end

return Database
13 changes: 13 additions & 0 deletions lua/frecency/database/table.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local timer = require "frecency.timer"
local wait = require "frecency.wait"
local lazy_require = require "frecency.lazy_require"
local async = lazy_require "plenary.async" --[[@as FrecencyPlenaryAsync]]

Expand Down Expand Up @@ -57,4 +58,16 @@ function Table:wait_ready()
timer.track "wait_ready() finish"
end

function Table:sync()
timer.track "Table:sync() start"
if rawget(self, "is_ready") then
timer.track "Table:sync() unneeded"
return
end
wait(function()
self:wait_ready()
end)
timer.track "Table:sync() finish"
end

return Table
9 changes: 6 additions & 3 deletions lua/frecency/klass.lua
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ end

---@class FrecencyQueryOpts
---@field direction? "asc"|"desc" default: "desc"
---@field json? boolean default: false
---@field limit? integer default: 100
---@field order? FrecencyQueryOrder default: "score"
---@field record? boolean default: false
Expand All @@ -213,14 +214,16 @@ end

---@param opts? FrecencyQueryOpts
---@param epoch? integer
---@return FrecencyQueryEntry[]|string[]
---@return string|FrecencyQueryEntry[]|string[]
function Frecency:query(opts, epoch)
opts = vim.tbl_extend("force", {
direction = "desc",
json = false,
limit = 100,
order = "score",
record = false,
}, opts or {})
self.database:sync()
---@param entry FrecencyDatabaseEntry
local entries = vim.tbl_map(function(entry)
return {
Expand All @@ -235,9 +238,9 @@ function Frecency:query(opts, epoch)
return entry.path
end, entries)
if #results > opts.limit then
return vim.list_slice(results, 1, opts.limit)
results = vim.list_slice(results, 1, opts.limit)
end
return results
return opts.json and vim.json.encode(results) or results
end

---@private
Expand Down

0 comments on commit fe21145

Please sign in to comment.