Skip to content

Commit

Permalink
fix(previewer): cache fzf port & fix "Too many opened files" issue (#604
Browse files Browse the repository at this point in the history
)
  • Loading branch information
linrongbin16 authored Feb 14, 2024
1 parent 551823a commit 13ff739
Show file tree
Hide file tree
Showing 6 changed files with 708 additions and 36 deletions.
49 changes: 47 additions & 2 deletions lua/fzfx/commons/fileios.lua
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,51 @@ M.FileLineReader = FileLineReader

-- FileLineReader }

-- CachedFileReader {

--- @class commons.CachedFileReader
--- @field filename string
--- @field cache string?
local CachedFileReader = {}

--- @param filename string
--- @return commons.CachedFileReader
function CachedFileReader:open(filename)
local o = {
filename = filename,
cache = nil,
}
setmetatable(o, self)
self.__index = self
return o
end

--- @param opts {trim:boolean?}?
--- @return string?
function CachedFileReader:read(opts)
opts = opts or {}
opts.trim = type(opts.trim) == "boolean" and opts.trim or false

if self.cache == nil then
self.cache = M.readfile(self.filename)
end
if self.cache == nil then
return self.cache
end
return opts.trim and vim.trim(self.cache) or self.cache
end

--- @return string?
function CachedFileReader:reset()
local saved = self.cache
self.cache = nil
return saved
end

M.CachedFileReader = CachedFileReader

-- CachedFileReader }

--- @param filename string
--- @param opts {trim:boolean?}?
--- @return string?
Expand Down Expand Up @@ -227,8 +272,8 @@ end
--- @param filename string
--- @return string[]|nil
M.readlines = function(filename)
local reader = M.FileLineReader:open(filename) --[[@as commons.FileLineReader]]
if not reader then
local ok, reader = pcall(M.FileLineReader.open, M.FileLineReader, filename) --[[@as commons.FileLineReader]]
if not ok or reader == nil then
return nil
end
local results = {}
Expand Down
11 changes: 2 additions & 9 deletions lua/fzfx/commons/numbers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,8 @@ end
--- @return number
M.random = function(m, n)
local rand_result, rand_err = require("fzfx.commons.uv").random(4)
if rand_result == nil then
if m == nil and n == nil then
return math.random()
elseif m ~= nil and n == nil then
return math.random(m)
else
return math.random(m --[[@as integer]], n --[[@as integer]])
end
end
assert(rand_result ~= nil, rand_err)

local bytes = {
string.byte(rand_result --[[@as string]], 1, -1),
}
Expand Down
Loading

0 comments on commit 13ff739

Please sign in to comment.