From b57465454b35f6e24a6df02a93a27d0136c016d9 Mon Sep 17 00:00:00 2001 From: Kyle Evans Date: Tue, 20 Feb 2018 14:45:58 +0000 Subject: [PATCH] stand/lua: Consistently organize modules We follow pretty closely the following structure of a module: 1. Copyright notice 2. Module requires 3. Module local declarations 4. Module local definitions 5. Module exports 6. return Re-organize the one-offs (config/drawer) and denote the start of module exports with a comment. --- stand/lua/color.lua | 1 + stand/lua/config.lua | 90 +++++++++++++++++++++--------------------- stand/lua/core.lua | 1 + stand/lua/drawer.lua | 47 +++++++++++----------- stand/lua/menu.lua | 2 +- stand/lua/password.lua | 1 + stand/lua/screen.lua | 1 + 7 files changed, 75 insertions(+), 68 deletions(-) diff --git a/stand/lua/color.lua b/stand/lua/color.lua index 189247ed27cf..4b676c37778c 100644 --- a/stand/lua/color.lua +++ b/stand/lua/color.lua @@ -30,6 +30,7 @@ local core = require("core"); local color = {}; +-- Module exports color.BLACK = 0; color.RED = 1; color.GREEN = 2; diff --git a/stand/lua/config.lua b/stand/lua/config.lua index e0d1c0bdd6c2..d5eb6b61f706 100644 --- a/stand/lua/config.lua +++ b/stand/lua/config.lua @@ -28,52 +28,9 @@ -- local config = {}; --- Which variables we changed -config.env_changed = {}; --- Values to restore env to (nil to unset) -config.env_restore = {}; local modules = {}; -function config.restoreEnv() - for k, v in pairs(config.env_changed) do - local restore_value = config.env_restore[k]; - if (restore_value ~= nil) then - loader.setenv(k, restore_value); - else - loader.unsetenv(k); - end - end - - config.env_changed = {}; - config.env_restore = {}; -end - -function config.setenv(k, v) - -- Do we need to track this change? - if (config.env_changed[k] == nil) then - config.env_changed[k] = true; - config.env_restore[k] = loader.getenv(k); - end - - return loader.setenv(k, v); -end - -function config.setKey(k, n, v) - if (modules[k] == nil) then - modules[k] = {}; - end - modules[k][n] = v; -end - -function config.lsModules() - print("== Listing modules"); - for k, v in pairs(modules) do - print(k, v.load); - end - print("== List of modules ended"); -end - local pattern_table = { [1] = { str = "^%s*(#.*)", @@ -162,6 +119,52 @@ local pattern_table = { } }; +-- Module exports +-- Which variables we changed +config.env_changed = {}; +-- Values to restore env to (nil to unset) +config.env_restore = {}; + +function config.restoreEnv() + for k, v in pairs(config.env_changed) do + local restore_value = config.env_restore[k]; + if (restore_value ~= nil) then + loader.setenv(k, restore_value); + else + loader.unsetenv(k); + end + end + + config.env_changed = {}; + config.env_restore = {}; +end + +function config.setenv(k, v) + -- Do we need to track this change? + if (config.env_changed[k] == nil) then + config.env_changed[k] = true; + config.env_restore[k] = loader.getenv(k); + end + + return loader.setenv(k, v); +end + +function config.setKey(k, n, v) + if (modules[k] == nil) then + modules[k] = {}; + end + modules[k][n] = v; +end + +function config.lsModules() + print("== Listing modules"); + for k, v in pairs(modules) do + print(k, v.load); + end + print("== List of modules ended"); +end + + function config.isValidComment(c) if (c ~= nil) then local s = c:match("^%s*#.*"); @@ -433,5 +436,4 @@ function config.loadelf() end end - return config; diff --git a/stand/lua/core.lua b/stand/lua/core.lua index ee84e9628a2d..043b08be3b68 100644 --- a/stand/lua/core.lua +++ b/stand/lua/core.lua @@ -30,6 +30,7 @@ local config = require('config'); local core = {}; +-- Module exports -- Commonly appearing constants core.KEY_BACKSPACE = 8; core.KEY_ENTER = 13; diff --git a/stand/lua/drawer.lua b/stand/lua/drawer.lua index 5d56d9ef0b45..6e8108144b48 100644 --- a/stand/lua/drawer.lua +++ b/stand/lua/drawer.lua @@ -59,29 +59,6 @@ local shift_brand_text = function(shift) drawer.box_pos_dim.y = drawer.box_pos_dim.y + shift.y; end -drawer.menu_name_handlers = { - -- Menu name handlers should take the menu being drawn and entry being - -- drawn as parameters, and return the name of the item. - -- This is designed so that everything, including menu separators, may - -- have their names derived differently. The default action for entry - -- types not specified here is to call and use entry.name(). - [core.MENU_CAROUSEL_ENTRY] = function(drawing_menu, entry) - local carid = entry.carousel_id; - local caridx = menu.getCarouselIndex(carid); - local choices = entry.items(); - - if (#choices < caridx) then - caridx = 1; - end - return entry.name(caridx, choices[caridx], choices); - end, -}; - -drawer.brand_position = {x = 2, y = 1}; -drawer.logo_position = {x = 46, y = 1}; -drawer.menu_position = {x = 6, y = 11}; -drawer.box_pos_dim = {x = 3, y = 10, w = 41, h = 11}; - fbsd_logo = { " ______ ____ _____ _____ ", " | ____| | _ \\ / ____| __ \\ ", @@ -190,6 +167,30 @@ orb = { none = {""}; +-- Module exports +drawer.menu_name_handlers = { + -- Menu name handlers should take the menu being drawn and entry being + -- drawn as parameters, and return the name of the item. + -- This is designed so that everything, including menu separators, may + -- have their names derived differently. The default action for entry + -- types not specified here is to call and use entry.name(). + [core.MENU_CAROUSEL_ENTRY] = function(drawing_menu, entry) + local carid = entry.carousel_id; + local caridx = menu.getCarouselIndex(carid); + local choices = entry.items(); + + if (#choices < caridx) then + caridx = 1; + end + return entry.name(caridx, choices[caridx], choices); + end, +}; + +drawer.brand_position = {x = 2, y = 1}; +drawer.logo_position = {x = 46, y = 1}; +drawer.menu_position = {x = 6, y = 11}; +drawer.box_pos_dim = {x = 3, y = 10, w = 41, h = 11}; + drawer.branddefs = { -- Indexed by valid values for loader_brand in loader.conf(5). Valid -- keys are: graphic (table depicting graphic) diff --git a/stand/lua/menu.lua b/stand/lua/menu.lua index cb318ffe166b..6aa1bb9fe071 100644 --- a/stand/lua/menu.lua +++ b/stand/lua/menu.lua @@ -51,7 +51,7 @@ local OnOff = function(str, b) end end - +-- Module exports menu.handlers = { -- Menu handlers take the current menu and selected entry as parameters, -- and should return a boolean indicating whether execution should diff --git a/stand/lua/password.lua b/stand/lua/password.lua index cb392b6f8216..a32af6998336 100644 --- a/stand/lua/password.lua +++ b/stand/lua/password.lua @@ -32,6 +32,7 @@ local screen = require("screen"); local password = {}; +-- Module exports function password.read() local str = ""; local n = 0; diff --git a/stand/lua/screen.lua b/stand/lua/screen.lua index d8939a0bdc63..d32bd98a6f1e 100644 --- a/stand/lua/screen.lua +++ b/stand/lua/screen.lua @@ -42,6 +42,7 @@ local intstring = function(num) return str; end +-- Module exports function screen.clear() if (core.isSerialBoot()) then return;