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:
parent
db50ad5b0c
commit
8313797190
@ -30,6 +30,7 @@ local core = require("core");
|
|||||||
|
|
||||||
local color = {};
|
local color = {};
|
||||||
|
|
||||||
|
-- Module exports
|
||||||
color.BLACK = 0;
|
color.BLACK = 0;
|
||||||
color.RED = 1;
|
color.RED = 1;
|
||||||
color.GREEN = 2;
|
color.GREEN = 2;
|
||||||
|
@ -28,52 +28,9 @@
|
|||||||
--
|
--
|
||||||
|
|
||||||
local config = {};
|
local config = {};
|
||||||
-- Which variables we changed
|
|
||||||
config.env_changed = {};
|
|
||||||
-- Values to restore env to (nil to unset)
|
|
||||||
config.env_restore = {};
|
|
||||||
|
|
||||||
local modules = {};
|
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 = {
|
local pattern_table = {
|
||||||
[1] = {
|
[1] = {
|
||||||
str = "^%s*(#.*)",
|
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)
|
function config.isValidComment(c)
|
||||||
if (c ~= nil) then
|
if (c ~= nil) then
|
||||||
local s = c:match("^%s*#.*");
|
local s = c:match("^%s*#.*");
|
||||||
@ -433,5 +436,4 @@ function config.loadelf()
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
return config;
|
return config;
|
||||||
|
@ -30,6 +30,7 @@ local config = require('config');
|
|||||||
|
|
||||||
local core = {};
|
local core = {};
|
||||||
|
|
||||||
|
-- Module exports
|
||||||
-- Commonly appearing constants
|
-- Commonly appearing constants
|
||||||
core.KEY_BACKSPACE = 8;
|
core.KEY_BACKSPACE = 8;
|
||||||
core.KEY_ENTER = 13;
|
core.KEY_ENTER = 13;
|
||||||
|
@ -59,29 +59,6 @@ local shift_brand_text = function(shift)
|
|||||||
drawer.box_pos_dim.y = drawer.box_pos_dim.y + shift.y;
|
drawer.box_pos_dim.y = drawer.box_pos_dim.y + shift.y;
|
||||||
end
|
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 = {
|
fbsd_logo = {
|
||||||
" ______ ____ _____ _____ ",
|
" ______ ____ _____ _____ ",
|
||||||
" | ____| | _ \\ / ____| __ \\ ",
|
" | ____| | _ \\ / ____| __ \\ ",
|
||||||
@ -190,6 +167,30 @@ orb = {
|
|||||||
|
|
||||||
none = {""};
|
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 = {
|
drawer.branddefs = {
|
||||||
-- Indexed by valid values for loader_brand in loader.conf(5). Valid
|
-- Indexed by valid values for loader_brand in loader.conf(5). Valid
|
||||||
-- keys are: graphic (table depicting graphic)
|
-- keys are: graphic (table depicting graphic)
|
||||||
|
@ -51,7 +51,7 @@ local OnOff = function(str, b)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- Module exports
|
||||||
menu.handlers = {
|
menu.handlers = {
|
||||||
-- Menu handlers take the current menu and selected entry as parameters,
|
-- Menu handlers take the current menu and selected entry as parameters,
|
||||||
-- and should return a boolean indicating whether execution should
|
-- and should return a boolean indicating whether execution should
|
||||||
|
@ -32,6 +32,7 @@ local screen = require("screen");
|
|||||||
|
|
||||||
local password = {};
|
local password = {};
|
||||||
|
|
||||||
|
-- Module exports
|
||||||
function password.read()
|
function password.read()
|
||||||
local str = "";
|
local str = "";
|
||||||
local n = 0;
|
local n = 0;
|
||||||
|
@ -42,6 +42,7 @@ local intstring = function(num)
|
|||||||
return str;
|
return str;
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- Module exports
|
||||||
function screen.clear()
|
function screen.clear()
|
||||||
if (core.isSerialBoot()) then
|
if (core.isSerialBoot()) then
|
||||||
return;
|
return;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user