mirror of
https://github.com/sxyazi/yazi.git
synced 2024-12-19 06:41:35 +03:00
58 lines
1.1 KiB
Lua
58 lines
1.1 KiB
Lua
local M = {}
|
|
|
|
function M:preload()
|
|
local urls = {}
|
|
for _, file in ipairs(self.files) do
|
|
urls[#urls + 1] = tostring(file.url)
|
|
end
|
|
|
|
local args
|
|
if ya.target_family() == "windows" then
|
|
args = { "-b", "--mime-type" }
|
|
else
|
|
args = { "-b", "-L", "--mime-type" }
|
|
end
|
|
|
|
local child, code = Command("file"):args(args):args(urls):stdout(Command.PIPED):spawn()
|
|
if not child then
|
|
ya.err("spawn `file` command returns " .. tostring(code))
|
|
return 0
|
|
end
|
|
|
|
local mimes, last = {}, ya.time()
|
|
local flush = function(force)
|
|
if not force and ya.time() - last < 0.1 then
|
|
return
|
|
end
|
|
if next(mimes) then
|
|
ya.manager_emit("update_mimetype", {}, mimes)
|
|
mimes, last = {}, ya.time()
|
|
end
|
|
end
|
|
|
|
local i, j = 1, 0
|
|
repeat
|
|
local next, event = child:read_line_with { timeout = 100 }
|
|
if event == 3 then
|
|
flush(true)
|
|
goto continue
|
|
elseif event ~= 0 then
|
|
break
|
|
end
|
|
|
|
next = next:gsub("[\r\n]+$", "")
|
|
if ya.mime_valid(next) then
|
|
j, mimes[urls[i]] = j + 1, next
|
|
flush(false)
|
|
end
|
|
|
|
i = i + 1
|
|
::continue::
|
|
until i > #urls
|
|
|
|
flush(true)
|
|
return j == #urls and 3 or 2
|
|
end
|
|
|
|
return M
|