-
Notifications
You must be signed in to change notification settings - Fork 24
/
folderbytextwdx.lua
67 lines (62 loc) · 1.98 KB
/
folderbytextwdx.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
-- folderbytextwdx.lua (cross-platform)
-- 2024.07.30
--[[
Search for folders by contents of text files.
Without a recursive search, i.e. for file search tool only.
Symbolic links to directories will be ignored.
List of extensions: see the "aExt" table.
List of supported encodings: see LazUtf8.ConvertEncoding
https://doublecmd.github.io/doc/en/lua.html#libraryutf8
]]
local aExt = {"txt", "cue"}
local sRet = ""
function ContentGetSupportedField(FieldIndex)
if aExt[FieldIndex + 1] ~= nil then
return "Folder with " .. aExt[FieldIndex + 1], "", 9
end
return "", "", 0
end
function ContentGetValue(FileName, FieldIndex, UnitIndex, flags)
if aExt[FieldIndex + 1] == nil then return nil end
if flags == 1 then return nil end; -- CONTENT_DELAYIFSLOW, https://doublecmd.github.io/doc/en/lua.html#preface
if UnitIndex ~= 0 then return nil end
local k = SysUtils.FileGetAttr(FileName)
if k < 0 then return nil end
if (math.floor(k / 0x00000400) % 2 ~= 0) then return nil end
if (math.floor(k / 0x00000010) % 2 ~= 0) then
sRet = ""
local r = nil
local h, d = SysUtils.FindFirst(FileName .. SysUtils.PathDelim .. "*." .. aExt[FieldIndex + 1])
if h ~= nil then
repeat
if (math.floor(d.Attr / 0x00000010) % 2 == 0) then
if d.Size > 0 then GetText(FileName .. SysUtils.PathDelim .. d.Name, d.Size) end
end
r, d = SysUtils.FindNext(h)
until r == nil
SysUtils.FindClose(h)
end
if sRet == "" then return nil else return sRet end
end
return nil
end
function GetText(p, n)
local h = io.open(p, "rb")
if h == nil then return nil end
local cf = h:read("*a")
h:close()
local t, e
if n > 4096 then
e = LazUtf8.DetectEncoding(string.sub(cf, 1, 4096))
else
e = LazUtf8.DetectEncoding(cf)
end
if e == "utf8" then
sRet = sRet .. "\n" .. cf
elseif e == "utf8bom" then
sRet = sRet .. "\n" .. string.sub(cf, 4, -1)
else
t = LazUtf8.ConvertEncoding(cf, e, "utf8")
sRet = sRet .. "\n" .. t
end
end