mirror of
https://github.com/kristoferssolo/telescope-frecency.nvim.git
synced 2025-10-21 20:10:38 +00:00
* fix: fix prop attributes and names
* feat: load DB as lazily as possible
* fix: move util function to test module
* feat: use one coroutine to access DB
* test: fix to wait the table to be ready
* fix: avoid race conditions
Before this, it can run require("frecency").new() duplicatedly to wait
until frecency:setup() finishes.
64 lines
1.9 KiB
Lua
64 lines
1.9 KiB
Lua
local uv = vim.uv or vim.loop
|
|
local async = require "plenary.async" --[[@as PlenaryAsync]]
|
|
local Path = require "plenary.path"
|
|
local Job = require "plenary.job"
|
|
local wait = require "frecency.tests.wait"
|
|
|
|
---@return PlenaryPath
|
|
---@return fun(): nil close swwp all entries
|
|
local function tmpdir()
|
|
local dir = Path:new(Path:new(assert(uv.fs_mkdtemp "tests_XXXXXX")):absolute())
|
|
return dir, function()
|
|
dir:rm { recursive = true }
|
|
end
|
|
end
|
|
|
|
---@param entries string[]
|
|
---@return PlenaryPath dir the top dir of tree
|
|
---@return fun(): nil close sweep all entries
|
|
local function make_tree(entries)
|
|
local dir, close = tmpdir()
|
|
for _, entry in ipairs(entries) do
|
|
---@diagnostic disable-next-line: undefined-field
|
|
dir:joinpath(entry):touch { parents = true }
|
|
end
|
|
return dir, close
|
|
end
|
|
|
|
local AsyncJob = async.wrap(function(cmd, callback)
|
|
return Job:new({
|
|
command = cmd[1],
|
|
args = { select(2, unpack(cmd)) },
|
|
on_exit = function(self, code, _)
|
|
local stdout = code == 0 and table.concat(self:result(), "\n") or nil
|
|
callback(stdout, code)
|
|
end,
|
|
}):start()
|
|
end, 2)
|
|
|
|
-- NOTE: vim.fn.strptime cannot be used in Lua loop
|
|
local function time_piece(iso8601)
|
|
local epoch
|
|
wait(function()
|
|
local stdout, code =
|
|
AsyncJob { "perl", "-MTime::Piece", "-e", "print Time::Piece->strptime('" .. iso8601 .. "', '%FT%T%z')->epoch" }
|
|
epoch = code == 0 and tonumber(stdout) or nil
|
|
end)
|
|
return epoch
|
|
end
|
|
|
|
---@param source table<string,{ count: integer, timestamps: string[] }>
|
|
local function v1_table(source)
|
|
local records = {}
|
|
for path, record in pairs(source) do
|
|
local timestamps = {}
|
|
for _, iso8601 in ipairs(record.timestamps) do
|
|
table.insert(timestamps, time_piece(iso8601))
|
|
end
|
|
records[path] = { count = record.count, timestamps = timestamps }
|
|
end
|
|
return { version = "v1", records = records }
|
|
end
|
|
|
|
return { make_tree = make_tree, tmpdir = tmpdir, v1_table = v1_table, time_piece = time_piece }
|