mirror of
https://github.com/kristoferssolo/telescope-frecency.nvim.git
synced 2025-10-21 20:10:38 +00:00
* refactor: unite logic for finder & async_finder * chore: fix types * chore: add sleep to show results at first * refactor: fix to find results separatedly * test: remove unnecessary ones and fix others * test: add matrix for 0.9.x & Windows * test: use forked plenary.log for Windows * test: fix to use strptime in Windows * test: run again if segmentation fault in Windows * test: loosen timeout for Perl * test: use the latest plenary.nvim again * chore: fix types * chore: change variable name * feat: watch changes of DB to reload * chore: add comments to steps * test: copy whole modules for testing in Windows * fix: make valid paths for Windows * test: add tests for Native * test: use robust way to calculate time vim.fn.strptime cannot be used in Lua loop * chore: fix comments * refactor: simplify the code * test: loosen condition to detect failures * test: disable some logging Many loggings make the test fail. * test: run tests sequentially in Windows * test: loosen timeout not to fail on Windows
59 lines
1.8 KiB
Lua
59 lines
1.8 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"
|
|
|
|
---@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 stdout, code =
|
|
AsyncJob { "perl", "-MTime::Piece", "-e", "print Time::Piece->strptime('" .. iso8601 .. "', '%FT%T%z')->epoch" }
|
|
return code == 0 and tonumber(stdout) or nil
|
|
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 }
|