From owner-svn-src-head@freebsd.org Tue Feb 20 03:40:17 2018 Return-Path: Delivered-To: svn-src-head@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 383A1F21C18; Tue, 20 Feb 2018 03:40:17 +0000 (UTC) (envelope-from kevans@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id DD88A7E2BC; Tue, 20 Feb 2018 03:40:16 +0000 (UTC) (envelope-from kevans@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id D872123D98; Tue, 20 Feb 2018 03:40:16 +0000 (UTC) (envelope-from kevans@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id w1K3eGpr038756; Tue, 20 Feb 2018 03:40:16 GMT (envelope-from kevans@FreeBSD.org) Received: (from kevans@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id w1K3eGlc038755; Tue, 20 Feb 2018 03:40:16 GMT (envelope-from kevans@FreeBSD.org) Message-Id: <201802200340.w1K3eGlc038755@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: kevans set sender to kevans@FreeBSD.org using -f From: Kyle Evans Date: Tue, 20 Feb 2018 03:40:16 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r329619 - head/stand/lua X-SVN-Group: head X-SVN-Commit-Author: kevans X-SVN-Commit-Paths: head/stand/lua X-SVN-Commit-Revision: 329619 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.25 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 20 Feb 2018 03:40:17 -0000 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();