lualoader: Clean up menu handling a little bit

This is driven by an urge to separate out the bits that really only need to
happen when the menu system starts up. Key points:

- menu.process now does the bulk of menu handling. It retains autoboot
  handling for dubious reasons, and it no longer accepts a 'nil' menu to
  process as 'the default'. Its return value is insignificant.
- The MENU_SUBMENU handler now returns nothing. If menu.process has exited,
  then we continue processing menu items on the parent menu as expected.
- menu.run is now the entry point of the menu system. It checks whether the
  menu should be skipped, processes the default menu, then returns.
This commit is contained in:
kevans 2018-02-25 04:11:08 +00:00
parent 71999db645
commit c7da60c4f0

View File

@ -81,7 +81,7 @@ menu.handlers = {
end,
[core.MENU_SUBMENU] = function(_, entry)
-- recurse
return menu.run(entry.submenu)
menu.process(entry.submenu)
end,
[core.MENU_RETURN] = function(_, entry)
-- allow entry to have a function/side effect
@ -342,29 +342,24 @@ menu.welcome = {
menu.default = menu.welcome
function menu.run(m)
if menu.skip() then
core.autoboot()
return false
end
if m == nil then
m = menu.default
end
function menu.process(m)
assert(m ~= nil)
-- redraw screen
screen.clear()
screen.defcursor()
local alias_table = drawer.drawscreen(m)
-- Might return nil, that's ok
-- autoboot processing likely belongs better in menu.run, but we want
-- to draw the menu once before we do any autoboot prompting. We also
-- collect the alias table from the drawer, which generates the table
-- based on all of the 'alias' entries along with effective line numbers
-- that each entry is drawn at. This makes it cleaner to handle here,
-- for the time being.
local autoboot_key;
if m == menu.default then
autoboot_key = menu.autoboot()
end
local cont = true
while cont do
while true do
local key = autoboot_key or io.getchar()
autoboot_key = nil
@ -391,12 +386,11 @@ function menu.run(m)
-- Get menu handler
local handler = menu.handlers[sel_entry.entry_type]
if handler ~= nil then
-- The handler's return value indicates whether
-- we need to exit this menu. An omitted return
-- value means "continue" by default.
cont = handler(m, sel_entry)
if cont == nil then
cont = true
-- The handler's return value indicates if we
-- need to exit this menu. An omitted or true
-- return value means to continue.
if handler(m, sel_entry) == false then
return
end
end
-- if we got an alias key the screen is out of date:
@ -405,14 +399,18 @@ function menu.run(m)
alias_table = drawer.drawscreen(m)
end
end
end
if m == menu.default then
screen.defcursor()
print("Exiting menu!")
return false
function menu.run()
if menu.skip() then
core.autoboot()
return
end
return true
menu.process(menu.default)
screen.defcursor()
print("Exiting menu!")
end
function menu.skip()