Author: kevans
Date: Tue Feb 20 03:40:16 2018
New Revision: 329619
URL: https://svnweb.freebsd.org/changeset/base/329619

Log:
  stand/lua: Extract menu handlers out into menu.handlers table
  
  This is a bit cleaner than our former method of an if ... else chain of
  handlers. Store handlers in the menu.handlers table so that they may be
  added to or removed dynamically.
  
  All handlers take the current menu and selected entry as parameters, and
  their return value indicates whether the menu processor should continue or
  not. An omitted return value or 'true' will indicate that we should
  continue, while returning 'false' will indicate that we should exit the
  current menu.
  
  The omitted return value behavior is due to continuing the loop being the
  more common situation.

Modified:
  head/stand/lua/menu.lua

Modified: head/stand/lua/menu.lua
==============================================================================
--- head/stand/lua/menu.lua     Tue Feb 20 02:32:22 2018        (r329618)
+++ head/stand/lua/menu.lua     Tue Feb 20 03:40:16 2018        (r329619)
@@ -42,6 +42,40 @@ local run;
 local autoboot;
 local carousel_choices = {};
 
+menu.handlers = {
+       -- Menu handlers take the current menu and selected entry as parameters,
+       -- and should return a boolean indicating whether execution should
+       -- continue or not. The return value may be omitted if this entry should
+       -- have no bearing on whether we continue or not, indicating that we
+       -- should just continue after execution.
+       [core.MENU_ENTRY] = function(current_menu, entry)
+               -- run function
+               entry.func();
+       end,
+       [core.MENU_CAROUSEL_ENTRY] = function(current_menu, entry)
+               -- carousel (rotating) functionality
+               local carid = entry.carousel_id;
+               local caridx = menu.getCarouselIndex(carid);
+               local choices = entry.items();
+
+               if (#choices > 0) then
+                       caridx = (caridx % #choices) + 1;
+                       menu.setCarouselIndex(carid, caridx);
+                       entry.func(caridx, choices[caridx], choices);
+               end
+       end,
+       [core.MENU_SUBMENU] = function(current_menu, entry)
+               -- recurse
+               return menu.run(entry.submenu());
+       end,
+       [core.MENU_RETURN] = function(current_menu, entry)
+               -- allow entry to have a function/side effect
+               if (entry.func ~= nil) then
+                       entry.func();
+               end
+               return false;
+       end,
+};
 -- loader menu tree is rooted at menu.welcome
 
 menu.boot_options = {
@@ -338,31 +372,16 @@ function menu.run(m)
 
                -- if we have an alias do the assigned action:
                if (sel_entry ~= nil) then
-                       if (sel_entry.entry_type == core.MENU_ENTRY) then
-                               -- run function
-                               sel_entry.func();
-                       elseif (sel_entry.entry_type == 
core.MENU_CAROUSEL_ENTRY) then
-                               -- carousel (rotating) functionality
-                               local carid = sel_entry.carousel_id;
-                               local caridx = menu.getCarouselIndex(carid);
-                               local choices = sel_entry.items();
-
-                               if (#choices > 0) then
-                                       caridx = (caridx % #choices) + 1;
-                                       menu.setCarouselIndex(carid, caridx);
-                                       sel_entry.func(caridx, choices[caridx],
-                                           choices);
+                       -- 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;
                                end
-                       elseif (sel_entry.entry_type == core.MENU_SUBMENU) then
-                               -- recurse
-                               cont = menu.run(sel_entry.submenu());
-                       elseif (sel_entry.entry_type == core.MENU_RETURN) then
-                               -- allow entry to have a function/side effect
-                               if (sel_entry.func ~= nil) then
-                                       sel_entry.func();
-                               end
-                               -- break recurse
-                               cont = false;
                        end
                        -- if we got an alias key the screen is out of date:
                        screen.clear();
_______________________________________________
svn-src-head@freebsd.org mailing list
https://lists.freebsd.org/mailman/listinfo/svn-src-head
To unsubscribe, send any mail to "svn-src-head-unsubscr...@freebsd.org"

Reply via email to