-
Notifications
You must be signed in to change notification settings - Fork 3
/
menu.lua
73 lines (56 loc) · 1.33 KB
/
menu.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
68
69
70
71
72
73
local w, h = term.getSize()
local selectedEntry = 1
local menus = {}
local currentMenu = "main"
local running = true
function render()
term.clear()
term.setCursorPos(1, 1)
local menu = menus[currentMenu] or menus["main"]
if selectedEntry > #menu.entries then
selectedEntry = 1
end
if currentMenu ~= "main" then
table.insert(menu.entries, {
label = "[BACK]",
callback = function()
currentMenu = menu.parent or "main"
render()
end
})
end
for i = 1, #menu.entries do
local caret = selectedEntry == i and ">> " or " "
term.setTextColor(selectedEntry == i and colors.magenta or colors.white)
print(caret .. menu.entries[i].label)
end
end
function onKeyPress(key)
local menu = menus[currentMenu] or menus["main"]
local switch = (({
[keys.enter] = menu.entries[selectedEntry].callback or function() end,
[keys.up] = function()
selectedEntry = selectedEntry - 1
end,
[keys.down] = function()
selectedEntry = selectedEntry + 1
end
})[key] or function() end)
switch()
if selectedEntry > #menu.entries or selectedEntry <= 0 then
selectedEntry = 1
end
end
function thread()
while running do
render()
local event, key = os.pullEvent "key"
onKeyPress(key)
end
end
return {
init = function(m) menus = m end,
exit = function() running = false end,
render = render,
thread = thread
}