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.
This commit is contained in:
Kyle Evans 2018-02-20 14:45:58 +00:00
parent e15abd1f16
commit b57465454b
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=329641
7 changed files with 75 additions and 68 deletions

View File

@ -30,6 +30,7 @@ local core = require("core");
local color = {};
-- Module exports
color.BLACK = 0;
color.RED = 1;
color.GREEN = 2;

View File

@ -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;

View File

@ -30,6 +30,7 @@ local config = require('config');
local core = {};
-- Module exports
-- Commonly appearing constants
core.KEY_BACKSPACE = 8;
core.KEY_ENTER = 13;

View File

@ -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)

View File

@ -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

View File

@ -32,6 +32,7 @@ local screen = require("screen");
local password = {};
-- Module exports
function password.read()
local str = "";
local n = 0;

View File

@ -42,6 +42,7 @@ local intstring = function(num)
return str;
end
-- Module exports
function screen.clear()
if (core.isSerialBoot()) then
return;