local F = far.Flags
----[[*D*]]local TabUtils=require"Bax.TabUtils"
----[[*D*]]local TabToStr=TabUtils.TableToString
--[[*D*]]local le=require"le"
------------------------------------------------------
local function ListSubdirs(Root)
local FromRoot = Root:match('^[a-zA-Z]:%\\$')
local lenRoot = FromRoot and 4 or ( Root:len() + (Root:sub(-1)=='\\' and 1 or 2) )
local PanItems = {}
local function AddOneDir(FF,f)
FF.FileName = f:sub(lenRoot)
table.insert(PanItems, FF)
end
far.RecursiveSearch(Root, "*.*>>d", AddOneDir, bor(F.FRS_RECUR, F.FRS_NONE))
table.sort(PanItems, function(a1,a2) return a1.FileName:gsub('\\','\1') < a2.FileName:gsub('\\','\1') end)
return PanItems
end -- ListSubdirs
------------------------------------------------------
local function ListArchive(Root)
local PanItems = {}
local hf = io.popen('7z.exe l "'..Root..'"')
if hf then
local function StrDateTimeToFileTime(s)
-- wDay=8;wMonth=4,wYear=2020,wHour=0,wMinute=36;wSecond=16
local ftime={wYear=tonumber(s:sub(1,4));
wMonth=tonumber(s:sub(6,7));
wDay=tonumber(s:sub(9,10));
wHour=tonumber(s:sub(12,13));
wMinute=tonumber(s:sub(15,16));
wSecond=tonumber(s:sub(18,19))
}
return win.SystemTimeToFileTime(ftime)
end
local bNeed = false
for l in hf:lines() do
l = win.OemToUtf8(l)
if l == '------------------- ----- ------------ ------------ ------------------------' then
if bNeed then break else bNeed = true end
elseif bNeed then
if l:sub(21,25):match"D" then
local FTime=StrDateTimeToFileTime(l)
PanItems[#PanItems+1] = {FileName=l:sub(54); FileAttributes='dc'; LastAccessTime=FTime; LastWriteTime=FTime}
end
end -- bNeed
end -- for l
hf:close()
end -- if hf
return PanItems
--far.Message('Не готово','Tree',nil,'w')
end
------------------------------------------------------
local function Tree(PanItems)
local aTree, i_Tree = {SubTree={}}, {} -- Древовидная структура и индекс для ускорения поиска
-----------------------------------------------------
local function AddDir(i)
local Source = PanItems[i]
local fName = Source.FileName .. '\\' --fName .. '\\'
local FullName=''
local Parent = aTree
for w in fName:gmatch("([^%\\]*)%\\") do -- Последний каталог войдёт
FullName = FullName .. ((FullName:sub(-1)=='\\' or FullName == '') and '' or '\\') .. w
if i_Tree[FullName] then
Parent = i_Tree[FullName]
else
local NewItem = {FileName=FullName; SubTree={}; Source=Source}
table.insert(Parent.SubTree, NewItem)
i_Tree[FullName] = NewItem
end
end
end -- AddDir
-----------------------------------------------------
for i=1, #PanItems do
AddDir(i)
end
-- Собственно составление дерева
----[[*D*]]local StrTree={} -- Для вывода на экран
local function Tree1Dir(OneDir, Last, sPref)
local ThisPref, NextPref
if not Last then
ThisPref, NextPref = sPref .. '├───', sPref .. '│ '
else
ThisPref, NextPref = sPref .. '└───', sPref .. ' '
end
OneDir.ShowDir = ThisPref .. mf.fsplit(OneDir.FileName, 12)
OneDir.Source.ShowDir = OneDir.ShowDir
if OneDir.SubTree then
for i=1, #OneDir.SubTree do
Tree1Dir(OneDir.SubTree[i], i==#OneDir.SubTree, NextPref)
end
end
end
for i,v in ipairs(aTree.SubTree) do
Tree1Dir(v, i==#aTree.SubTree, '')
end
return aTree
end -- function Tree
------------------------------------------------------
local Root = far.InputBox(nil, 'Дерево', 'Введите имя корня', nil,
----[[C:\Users\PC\AppData\Roaming\Far Manager\Profile\Macros\Bax.Test\Tree\200309\Test.7z]],
[[C:\Users\PC\AppData\Roaming\Far Manager\Profile\Macros\Bax.Test\Tree]],
----[[C:\Users\PC\AppData\Roaming\Far Manager\Profile\Macros\Bax.Test\Tree\200309\Test.Тест\Test\Test2\Test2_1]],
nil,nil,bor(F.FIB_EDITPATH, F.FIB_EXPANDENV) )
if not Root then return end
if not win.GetFileAttr(Root) then return end
local PanItems
if win.GetFileAttr(Root):find'd' then
PanItems = ListSubdirs(Root)
else
PanItems = ListArchive(Root)
end
local aTree = Tree(PanItems)
--*D* {
le(aTree, '123: aTree')
do
local hf=io.open('deb.tree.lua', 'w')
if hf then
local qqq=mf.serialize(aTree)
hf:write(qqq)
hf:close()
end
end
local aDeb={Root}
local function DrawSubTree(Node)
table.insert(aDeb, Node.ShowDir)
for _,v in ipairs(Node.SubTree) do
DrawSubTree(v)
end
end -- function DrawSubTree
for _,v in ipairs(aTree.SubTree) do
DrawSubTree(v)
end
far.Show(table.concat(aDeb, '\n'))
--*D* }