-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtreeview.lua
More file actions
68 lines (50 loc) · 1.65 KB
/
treeview.lua
File metadata and controls
68 lines (50 loc) · 1.65 KB
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
68
local ennui = require("ennui")
local TreeView = ennui.Widgets.Treeview
local TreeViewNode = ennui.Widgets.Treeviewnode
local Text = ennui.Widgets.Text
local Window = ennui.Widgets.Window
local ScrollArea = ennui.Widgets.Scrollarea
local host = ennui.Widgets.Host()
:setSize(love.graphics.getDimensions())
local window = Window("TreeView Example")
:setSize(350, 600)
:setPosition(100, 100)
local treeView = TreeView()
:setSize(ennui.Size.fill(), ennui.Size.auto())
:setRowHeight(26)
:setIndentSize(20)
local scrollArea = ScrollArea()
:setSize(ennui.Size.fill(), ennui.Size.fill())
local function sortFiles(a, b)
local aInfo = love.filesystem.getInfo(a)
local bInfo = love.filesystem.getInfo(b)
if not aInfo or not bInfo then
return a < b
end
if aInfo.type == "directory" and bInfo.type ~= "directory" then
return true
elseif bInfo.type == "directory" and aInfo.type ~= "directory" then
return false
end
end
local function recursivelyGetFileList(path, node)
local files = love.filesystem.getDirectoryItems(path)
table.sort(files, sortFiles)
for _, file in ipairs(files) do
local fullPath = path .. "/" .. file
local info = love.filesystem.getInfo(fullPath)
local childNode = TreeViewNode(file)
node:addChild(childNode)
if info.type == "directory" then
recursivelyGetFileList(fullPath, childNode)
end
end
return node
end
local root = TreeViewNode("ennui")
recursivelyGetFileList("ennui", root)
treeView:addChild(root)
scrollArea:addChild(treeView)
window:setContent(scrollArea)
host:addChild(window)
return host