improve startup, dont rely on debug (cbor)

This commit is contained in:
kepler155c@gmail.com 2019-10-28 20:01:57 -06:00
parent 6e6d4b81cd
commit 774d3ed415
3 changed files with 103 additions and 71 deletions

View File

@ -1,14 +1,48 @@
--[[
.startup.boot
delay
description: delays amount before starting the default selection
default: 1.5
preload
description : runs before menu is displayed, can be used for password
locking, drive encryption, etc.
example : { [1] = '/path/somefile.lua', [2] = 'path2/another.lua' }
menu
description: array of menu entries (see .startup.boot for examples)
]]
local colors = _G.colors local colors = _G.colors
local os = _G.os local os = _G.os
local settings = _G.settings local settings = _G.settings
local term = _G.term local term = _G.term
local bootOptions = { local function loadBootOptions()
if not fs.exists('.startup.boot') then
local f = fs.open('.startup.boot', 'w')
f.write(textutils.serialize({
delay = 1.5,
preload = { },
menu = {
{ prompt = os.version() }, { prompt = os.version() },
{ prompt = 'Opus' , args = { '/sys/boot/opus.boot' } }, { prompt = 'Opus' , args = { '/sys/boot/opus.boot' } },
{ prompt = 'Opus Shell' , args = { '/sys/boot/opus.boot', 'sys/apps/shell.lua' } }, { prompt = 'Opus Shell' , args = { '/sys/boot/opus.boot', 'sys/apps/shell.lua' } },
{ prompt = 'Opus Kiosk' , args = { '/sys/boot/kiosk.boot' } }, { prompt = 'Opus Kiosk' , args = { '/sys/boot/kiosk.boot' } },
} },
}))
f.close()
end
local f = fs.open('.startup.boot', 'r')
local options = textutils.unserialize(f.readAll())
f.close()
return options
end
local bootOptions = loadBootOptions()
local bootOption = 2 local bootOption = 2
if settings then if settings then
settings.load('.settings') settings.load('.settings')
@ -17,69 +51,63 @@ end
local function startupMenu() local function startupMenu()
local x, y = term.getSize() local x, y = term.getSize()
local align, selected = 0, 1 local align, selected = 0, bootOption
local function redraw() local function redraw()
local title = "Boot Options:" local title = "Boot Options:"
term.clear() term.clear()
term.setTextColor(colors.white) term.setTextColor(colors.white)
term.setCursorPos((x/2)-(#title/2), (y/2)-(#bootOptions/2)-1) term.setCursorPos((x/2)-(#title/2), (y/2)-(#bootOptions.menu/2)-1)
term.write(title) term.write(title)
for i = 1, #bootOptions do for i, item in pairs(bootOptions.menu) do
local txt = i..". "..bootOptions[i].prompt local txt = i .. ". " .. item.prompt
term.setCursorPos((x/2)-(align/2), (y/2)-(#bootOptions/2)+i) term.setCursorPos((x/2)-(align/2), (y/2)-(#bootOptions.menu/2)+i)
term.write(txt) term.write(txt)
end end
end end
for i = 1, #bootOptions do for _, item in pairs(bootOptions.menu) do
if (bootOptions[i].prompt):len() > align then if #item.prompt > align then
align = (bootOptions[i].prompt):len() align = #item.prompt
end end
end end
redraw() redraw()
repeat while true do
term.setCursorPos((x/2)-(align/2)-2, (y/2)-(#bootOptions/2)+selected) term.setCursorPos((x/2)-(align/2)-2, (y/2)-(#bootOptions.menu/2)+selected)
if term.isColor() then term.setTextColor(term.isColor() and colors.yellow or colors.lightGray)
term.setTextColor(colors.yellow)
else
term.setTextColor(colors.lightGray)
end
term.write(">") term.write(">")
local k = ({os.pullEvent()}) local event, key = os.pullEvent()
if k[1] == "mouse_scroll" then if event == "mouse_scroll" then
if k[2] == 1 then key = key == 1 and keys.down or keys.up
k = keys.down elseif event == 'key_up' then
else key = nil -- only process key events
k = keys.up
end end
elseif k[1] == "key" then
k = k[2] if key == keys.enter or key == keys.right then
else
k = nil
end
if k then
if k == keys.enter or k == keys.right then
return selected return selected
elseif k == keys.down then elseif key == keys.down then
if selected == #bootOptions then if selected == #bootOptions.menu then
selected = 0 selected = 0
end end
selected = selected+1 selected = selected + 1
elseif k == keys.up then elseif key == keys.up then
if selected == 1 then if selected == 1 then
selected = #bootOptions+1 selected = #bootOptions.menu + 1
end end
selected = selected-1 selected = selected - 1
elseif k >= keys.one and k <= #bootOptions+1 and k < keys.zero then elseif event == 'char' then
selected = k-1 key = tonumber(key) or 0
return selected if bootOptions.menu[key] then
return key
end end
end
local cx, cy = term.getCursorPos() local cx, cy = term.getCursorPos()
term.setCursorPos(cx-1, cy) term.setCursorPos(cx-1, cy)
term.write(" ") term.write(" ")
end end
until true == false
end end
local function splash() local function splash()
@ -120,13 +148,17 @@ end
term.clear() term.clear()
splash() splash()
local timerId = os.startTimer(1.5) for _, v in pairs(bootOptions.preload) do
os.run(_ENV, v)
end
local timerId = os.startTimer(bootOptions.delay)
while true do while true do
local e, id = os.pullEvent() local e, id = os.pullEvent()
if e == 'timer' and id == timerId then if e == 'timer' and id == timerId then
break break
end end
if e == 'char' then if e == 'char' or e == 'key' then
bootOption = startupMenu() bootOption = startupMenu()
if settings then if settings then
settings.set('opus.boot_option', bootOption) settings.set('opus.boot_option', bootOption)
@ -138,9 +170,9 @@ end
term.clear() term.clear()
term.setCursorPos(1, 1) term.setCursorPos(1, 1)
if bootOptions[bootOption].args then if bootOptions.menu[bootOption].args then
os.run(_ENV, table.unpack(bootOptions[bootOption].args)) os.run(_ENV, table.unpack(bootOptions.menu[bootOption].args))
else else
print(bootOptions[bootOption].prompt) print(bootOptions.menu[bootOption].prompt)
end end

View File

@ -14,7 +14,7 @@ end
local setmetatable = setmetatable; local setmetatable = setmetatable;
local getmetatable = getmetatable; local getmetatable = getmetatable;
local dbg_getmetatable = debug.getmetatable; local dbg_getmetatable = debug and debug.getmetatable;
local assert = assert; local assert = assert;
local error = error; local error = error;
local type = type; local type = type;