From 625feb3fd14052a3f91f5ef9d4ff0915bb8ff803 Mon Sep 17 00:00:00 2001 From: Mikayla Fischler Date: Sat, 30 Sep 2023 11:46:47 -0400 Subject: [PATCH] #344 graphics assertion overhaul --- graphics/core.lua | 8 ++++++ graphics/element.lua | 28 +++++++++++++------ graphics/elements/controls/app.lua | 8 +++--- graphics/elements/controls/checkbox.lua | 4 +-- graphics/elements/controls/hazard_button.lua | 6 ++-- graphics/elements/controls/multi_button.lua | 10 +++---- graphics/elements/controls/push_button.lua | 6 ++-- graphics/elements/controls/radio_2d.lua | 12 ++++---- graphics/elements/controls/radio_button.lua | 14 ++++------ graphics/elements/controls/sidebar.lua | 8 +++--- .../elements/controls/spinbox_numeric.lua | 6 ++-- graphics/elements/controls/switch_button.lua | 8 +++--- graphics/elements/controls/tabbar.lua | 10 +++---- graphics/elements/indicators/alight.lua | 10 +++---- graphics/elements/indicators/coremap.lua | 4 +-- graphics/elements/indicators/data.lua | 8 +++--- graphics/elements/indicators/hbar.lua | 7 ++--- graphics/elements/indicators/icon.lua | 4 +-- graphics/elements/indicators/led.lua | 6 ++-- graphics/elements/indicators/ledpair.lua | 10 +++---- graphics/elements/indicators/ledrgb.lua | 4 +-- graphics/elements/indicators/light.lua | 6 ++-- graphics/elements/indicators/power.lua | 4 +-- graphics/elements/indicators/rad.lua | 8 +++--- graphics/elements/indicators/state.lua | 8 ++---- graphics/elements/indicators/trilight.lua | 10 +++---- graphics/elements/multipane.lua | 2 +- graphics/elements/pipenet.lua | 2 +- graphics/elements/rectangle.lua | 6 ++-- graphics/elements/textbox.lua | 2 +- graphics/elements/tiling.lua | 10 +++---- 31 files changed, 126 insertions(+), 113 deletions(-) diff --git a/graphics/core.lua b/graphics/core.lua index ee7d829..88e0f30 100644 --- a/graphics/core.lua +++ b/graphics/core.lua @@ -111,6 +111,14 @@ function core.pipe(x1, y1, x2, y2, color, thin, align_tr) } end +-- Assertion Handling + +-- extract the custom element assert message, dropping the path to the element file +function core.extract_assert_msg(msg) + local start = string.find(msg, "@") or 1 + return string.sub(msg, start) +end + -- Interactive Field Manager ---@param e graphics_base diff --git a/graphics/element.lua b/graphics/element.lua index 20bc0c0..7bc8de5 100644 --- a/graphics/element.lua +++ b/graphics/element.lua @@ -2,6 +2,8 @@ -- Generic Graphics Element -- +local util = require("scada-common.util") + local core = require("graphics.core") local events = core.events @@ -65,6 +67,16 @@ local element = {} ---@field key string data key ---@field func function callback +-- more detailed assert message for element verification +---@param condition any assert condition +---@param msg string assert message +---@param callstack_offset? integer shift value to change targets of debug.getinfo() +function element.assert(condition, msg, callstack_offset) + callstack_offset = callstack_offset or 0 + local caller = debug.getinfo(3 + callstack_offset) + assert(condition, util.c(caller.source, ":", caller.currentline, "{", debug.getinfo(2 + callstack_offset).name, "}: ", msg)) +end + -- a base graphics element, should not be created on its own ---@nodiscard ---@param args graphics_args arguments @@ -77,7 +89,7 @@ function element.new(args, child_offset_x, child_offset_y) elem_type = debug.getinfo(2).name, define_completed = false, p_window = nil, ---@type table - position = { x = 1, y = 1 }, ---@type coordinate_2d + position = events.new_coord_2d(1, 1), bounds = { x1 = 1, y1 = 1, x2 = 1, y2 = 1 }, ---@class element_bounds next_y = 1, -- next child y coordinate next_id = 0, -- next child ID @@ -99,11 +111,9 @@ function element.new(args, child_offset_x, child_offset_y) child_id_map = {} } - local name_brief = "graphics.element{" .. self.elem_type .. "}: " - -- element as string function self.mt.__tostring() - return "graphics.element{" .. self.elem_type .. "} @ " .. tostring(self) + return util.c("graphics.element{", self.elem_type, "} @ ", self) end ---@class graphics_element @@ -207,10 +217,10 @@ function element.new(args, child_offset_x, child_offset_y) end -- check frame - assert(f.x >= 1, name_brief .. "frame x not >= 1") - assert(f.y >= 1, name_brief .. "frame y not >= 1") - assert(f.w >= 1, name_brief .. "frame width not >= 1") - assert(f.h >= 1, name_brief .. "frame height not >= 1") + element.assert(f.x >= 1, "frame x not >= 1", 2) + element.assert(f.y >= 1, "frame y not >= 1", 2) + element.assert(f.w >= 1, "frame width not >= 1", 2) + element.assert(f.h >= 1, "frame height not >= 1", 2) -- create window protected.window = window.create(self.p_window, f.x, f.y, f.w, f.h, args.hidden ~= true) @@ -409,7 +419,7 @@ function element.new(args, child_offset_x, child_offset_y) end -- check window - assert(self.p_window, name_brief .. "no parent window provided") + element.assert(self.p_window, "no parent window provided", 1) -- prepare the template if args.parent == nil then diff --git a/graphics/elements/controls/app.lua b/graphics/elements/controls/app.lua index 10ef6b5..1433193 100644 --- a/graphics/elements/controls/app.lua +++ b/graphics/elements/controls/app.lua @@ -24,10 +24,10 @@ local MOUSE_CLICK = core.events.MOUSE_CLICK ---@param args app_button_args ---@return graphics_element element, element_id id local function app_button(args) - assert(type(args.text) == "string", "controls.app: text is a required field") - assert(type(args.title) == "string", "controls.app: title is a required field") - assert(type(args.callback) == "function", "controls.app: callback is a required field") - assert(type(args.app_fg_bg) == "table", "controls.app: app_fg_bg is a required field") + element.assert(type(args.text) == "string", "text is a required field") + element.assert(type(args.title) == "string", "title is a required field") + element.assert(type(args.callback) == "function", "callback is a required field") + element.assert(type(args.app_fg_bg) == "table", "app_fg_bg is a required field") args.height = 4 args.width = 5 diff --git a/graphics/elements/controls/checkbox.lua b/graphics/elements/controls/checkbox.lua index a31e636..7f0dc58 100644 --- a/graphics/elements/controls/checkbox.lua +++ b/graphics/elements/controls/checkbox.lua @@ -18,8 +18,8 @@ local element = require("graphics.element") ---@param args checkbox_args ---@return graphics_element element, element_id id local function checkbox(args) - assert(type(args.label) == "string", "controls.checkbox: label is a required field") - assert(type(args.box_fg_bg) == "table", "controls.checkbox: box_fg_bg is a required field") + element.assert(type(args.label) == "string", "label is a required field") + element.assert(type(args.box_fg_bg) == "table", "box_fg_bg is a required field") args.can_focus = true args.height = 1 diff --git a/graphics/elements/controls/hazard_button.lua b/graphics/elements/controls/hazard_button.lua index 9f402cf..9a0e621 100644 --- a/graphics/elements/controls/hazard_button.lua +++ b/graphics/elements/controls/hazard_button.lua @@ -21,9 +21,9 @@ local element = require("graphics.element") ---@param args hazard_button_args ---@return graphics_element element, element_id id local function hazard_button(args) - assert(type(args.text) == "string", "controls.hazard_button: text is a required field") - assert(type(args.accent) == "number", "controls.hazard_button: accent is a required field") - assert(type(args.callback) == "function", "controls.hazard_button: callback is a required field") + element.assert(type(args.text) == "string", "text is a required field") + element.assert(type(args.accent) == "number", "accent is a required field") + element.assert(type(args.callback) == "function", "callback is a required field") args.height = 3 args.width = string.len(args.text) + 4 diff --git a/graphics/elements/controls/multi_button.lua b/graphics/elements/controls/multi_button.lua index 4de45b0..d686b9d 100644 --- a/graphics/elements/controls/multi_button.lua +++ b/graphics/elements/controls/multi_button.lua @@ -29,11 +29,11 @@ local element = require("graphics.element") ---@param args multi_button_args ---@return graphics_element element, element_id id local function multi_button(args) - assert(type(args.options) == "table", "controls.multi_button: options is a required field") - assert(#args.options > 0, "controls.multi_button: at least one option is required") - assert(type(args.callback) == "function", "controls.multi_button: callback is a required field") - assert(type(args.default) == "nil" or (type(args.default) == "number" and args.default > 0), "controls.multi_button: default must be nil or a number > 0") - assert(type(args.min_width) == "nil" or (type(args.min_width) == "number" and args.min_width > 0), "controls.multi_button: min_width must be nil or a number > 0") + element.assert(type(args.options) == "table", "options is a required field") + element.assert(#args.options > 0, "at least one option is required") + element.assert(type(args.callback) == "function", "callback is a required field") + element.assert(type(args.default) == "nil" or (type(args.default) == "number" and args.default > 0), "default must be nil or a number > 0") + element.assert(type(args.min_width) == "nil" or (type(args.min_width) == "number" and args.min_width > 0), "min_width must be nil or a number > 0") -- single line args.height = 1 diff --git a/graphics/elements/controls/push_button.lua b/graphics/elements/controls/push_button.lua index 02bda8a..59e2b9e 100644 --- a/graphics/elements/controls/push_button.lua +++ b/graphics/elements/controls/push_button.lua @@ -26,9 +26,9 @@ local KEY_CLICK = core.events.KEY_CLICK ---@param args push_button_args ---@return graphics_element element, element_id id local function push_button(args) - assert(type(args.text) == "string", "controls.push_button: text is a required field") - assert(type(args.callback) == "function", "controls.push_button: callback is a required field") - assert(type(args.min_width) == "nil" or (type(args.min_width) == "number" and args.min_width > 0), "controls.push_button: min_width must be nil or a number > 0") + element.assert(type(args.text) == "string", "text is a required field") + element.assert(type(args.callback) == "function", "callback is a required field") + element.assert(type(args.min_width) == "nil" or (type(args.min_width) == "number" and args.min_width > 0), "min_width must be nil or a number > 0") local text_width = string.len(args.text) diff --git a/graphics/elements/controls/radio_2d.lua b/graphics/elements/controls/radio_2d.lua index e4386f1..d87d87e 100644 --- a/graphics/elements/controls/radio_2d.lua +++ b/graphics/elements/controls/radio_2d.lua @@ -27,12 +27,12 @@ local element = require("graphics.element") ---@param args radio_2d_args ---@return graphics_element element, element_id id local function radio_2d_button(args) - assert(type(args.options) == "table" and #args.options > 0, "controls.radio_2d: options should be a table with length >= 1") - assert(util.is_int(args.rows) and util.is_int(args.columns), "controls.radio_2d: rows/columns must be integers") - assert((args.rows * args.columns) >= #args.options, "controls.radio_2d: rows x columns size insufficient for provided number of options") - assert(type(args.radio_colors) == "table", "controls.radio_2d: radio_colors is a required field") - assert(type(args.select_color) == "number" or type(args.color_map) == "table", "controls.radio_2d: select_color or color_map is required") - assert(type(args.default) == "nil" or (type(args.default) == "number" and args.default > 0), "controls.radio_2d: default must be nil or a number > 0") + element.assert(type(args.options) == "table" and #args.options > 0, "options should be a table with length >= 1") + element.assert(util.is_int(args.rows) and util.is_int(args.columns), "rows/columns must be integers") + element.assert((args.rows * args.columns) >= #args.options, "rows x columns size insufficient for provided number of options") + element.assert(type(args.radio_colors) == "table", "radio_colors is a required field") + element.assert(type(args.select_color) == "number" or type(args.color_map) == "table", "select_color or color_map is required") + element.assert(type(args.default) == "nil" or (type(args.default) == "number" and args.default > 0), "default must be nil or a number > 0") local array = {} local col_widths = {} diff --git a/graphics/elements/controls/radio_button.lua b/graphics/elements/controls/radio_button.lua index 483f1f8..d54a1df 100644 --- a/graphics/elements/controls/radio_button.lua +++ b/graphics/elements/controls/radio_button.lua @@ -25,14 +25,12 @@ local KEY_CLICK = core.events.KEY_CLICK ---@param args radio_button_args ---@return graphics_element element, element_id id local function radio_button(args) - assert(type(args.options) == "table", "controls.radio_button: options is a required field") - assert(#args.options > 0, "controls.radio_button: at least one option is required") - assert(type(args.radio_colors) == "table", "controls.radio_button: radio_colors is a required field") - assert(type(args.select_color) == "number", "controls.radio_button: select_color is a required field") - assert(type(args.default) == "nil" or (type(args.default) == "number" and args.default > 0), - "controls.radio_button: default must be nil or a number > 0") - assert(type(args.min_width) == "nil" or (type(args.min_width) == "number" and args.min_width > 0), - "controls.radio_button: min_width must be nil or a number > 0") + element.assert(type(args.options) == "table", "options is a required field") + element.assert(#args.options > 0, "at least one option is required") + element.assert(type(args.radio_colors) == "table", "radio_colors is a required field") + element.assert(type(args.select_color) == "number", "select_color is a required field") + element.assert(type(args.default) == "nil" or (type(args.default) == "number" and args.default > 0), "default must be nil or a number > 0") + element.assert(type(args.min_width) == "nil" or (type(args.min_width) == "number" and args.min_width > 0), "min_width must be nil or a number > 0") -- determine widths local max_width = 1 diff --git a/graphics/elements/controls/sidebar.lua b/graphics/elements/controls/sidebar.lua index 3ac6bb1..fef8a8a 100644 --- a/graphics/elements/controls/sidebar.lua +++ b/graphics/elements/controls/sidebar.lua @@ -27,16 +27,16 @@ local MOUSE_CLICK = core.events.MOUSE_CLICK ---@param args sidebar_args ---@return graphics_element element, element_id id local function sidebar(args) - assert(type(args.tabs) == "table", "controls.sidebar: tabs is a required field") - assert(#args.tabs > 0, "controls.sidebar: at least one tab is required") - assert(type(args.callback) == "function", "controls.sidebar: callback is a required field") + element.assert(type(args.tabs) == "table", "tabs is a required field") + element.assert(#args.tabs > 0, "at least one tab is required") + element.assert(type(args.callback) == "function", "callback is a required field") args.width = 3 -- create new graphics element base object local e = element.new(args) - assert(e.frame.h >= (#args.tabs * 3), "graphics.elements.controls.sidebar: height insufficent to display all tabs") + element.assert(e.frame.h >= (#args.tabs * 3), "height insufficent to display all tabs") -- default to 1st tab e.value = 1 diff --git a/graphics/elements/controls/spinbox_numeric.lua b/graphics/elements/controls/spinbox_numeric.lua index 7615ecf..90edf18 100644 --- a/graphics/elements/controls/spinbox_numeric.lua +++ b/graphics/elements/controls/spinbox_numeric.lua @@ -29,8 +29,8 @@ local function spinbox(args) local wn_prec = args.whole_num_precision local fr_prec = args.fractional_precision - assert(util.is_int(wn_prec), "controls.spinbox_numeric: whole number precision must be an integer") - assert(util.is_int(fr_prec), "controls.spinbox_numeric: fractional precision must be an integer") + element.assert(util.is_int(wn_prec), "whole number precision must be an integer") + element.assert(util.is_int(fr_prec), "fractional precision must be an integer") local fmt, fmt_init ---@type string, string @@ -44,7 +44,7 @@ local function spinbox(args) local dec_point_x = args.whole_num_precision + 1 - assert(type(args.arrow_fg_bg) == "table", "controls.spinbox_numeric: arrow_fg_bg is a required field") + element.assert(type(args.arrow_fg_bg) == "table", "arrow_fg_bg is a required field") -- determine widths args.width = wn_prec + fr_prec + util.trinary(fr_prec > 0, 1, 0) diff --git a/graphics/elements/controls/switch_button.lua b/graphics/elements/controls/switch_button.lua index d8da813..f649b10 100644 --- a/graphics/elements/controls/switch_button.lua +++ b/graphics/elements/controls/switch_button.lua @@ -21,10 +21,10 @@ local element = require("graphics.element") ---@param args switch_button_args ---@return graphics_element element, element_id id local function switch_button(args) - assert(type(args.text) == "string", "controls.switch_button: text is a required field") - assert(type(args.callback) == "function", "controls.switch_button: callback is a required field") - assert(type(args.active_fg_bg) == "table", "controls.switch_button: active_fg_bg is a required field") - assert(type(args.min_width) == "nil" or (type(args.min_width) == "number" and args.min_width > 0), "controls.switch_button: min_width must be nil or a number > 0") + element.assert(type(args.text) == "string", "text is a required field") + element.assert(type(args.callback) == "function", "callback is a required field") + element.assert(type(args.active_fg_bg) == "table", "active_fg_bg is a required field") + element.assert(type(args.min_width) == "nil" or (type(args.min_width) == "number" and args.min_width > 0), "min_width must be nil or a number > 0") local text_width = string.len(args.text) diff --git a/graphics/elements/controls/tabbar.lua b/graphics/elements/controls/tabbar.lua index ad2c0d7..9b604e1 100644 --- a/graphics/elements/controls/tabbar.lua +++ b/graphics/elements/controls/tabbar.lua @@ -27,10 +27,10 @@ local element = require("graphics.element") ---@param args tabbar_args ---@return graphics_element element, element_id id local function tabbar(args) - assert(type(args.tabs) == "table", "controls.tabbar: tabs is a required field") - assert(#args.tabs > 0, "controls.tabbar: at least one tab is required") - assert(type(args.callback) == "function", "controls.tabbar: callback is a required field") - assert(type(args.min_width) == "nil" or (type(args.min_width) == "number" and args.min_width > 0), "controls.tabbar: min_width must be nil or a number > 0") + element.assert(type(args.tabs) == "table", "tabs is a required field") + element.assert(#args.tabs > 0, "at least one tab is required") + element.assert(type(args.callback) == "function", "callback is a required field") + element.assert(type(args.min_width) == "nil" or (type(args.min_width) == "number" and args.min_width > 0), "min_width must be nil or a number > 0") args.height = 1 @@ -48,7 +48,7 @@ local function tabbar(args) -- create new graphics element base object local e = element.new(args) - assert(e.frame.w >= (button_width * #args.tabs), "graphics.elements.controls.tabbar: width insufficent to display all tabs") + element.assert(e.frame.w >= (button_width * #args.tabs), "width insufficent to display all tabs") -- default to 1st tab e.value = 1 diff --git a/graphics/elements/indicators/alight.lua b/graphics/elements/indicators/alight.lua index 8ed5337..e05569c 100644 --- a/graphics/elements/indicators/alight.lua +++ b/graphics/elements/indicators/alight.lua @@ -25,13 +25,13 @@ local flasher = require("graphics.flasher") ---@param args alarm_indicator_light ---@return graphics_element element, element_id id local function alarm_indicator_light(args) - assert(type(args.label) == "string", "indicators.alight: label is a required field") - assert(type(args.c1) == "number", "indicators.alight: c1 is a required field") - assert(type(args.c2) == "number", "indicators.alight: c2 is a required field") - assert(type(args.c3) == "number", "indicators.alight: c3 is a required field") + element.assert(type(args.label) == "string", "label is a required field") + element.assert(type(args.c1) == "number", "c1 is a required field") + element.assert(type(args.c2) == "number", "c2 is a required field") + element.assert(type(args.c3) == "number", "c3 is a required field") if args.flash then - assert(util.is_int(args.period), "indicators.alight: period is a required field if flash is enabled") + element.assert(util.is_int(args.period), "period is a required field if flash is enabled") end -- single line diff --git a/graphics/elements/indicators/coremap.lua b/graphics/elements/indicators/coremap.lua index 12319f6..9084b99 100644 --- a/graphics/elements/indicators/coremap.lua +++ b/graphics/elements/indicators/coremap.lua @@ -18,8 +18,8 @@ local element = require("graphics.element") ---@param args core_map_args ---@return graphics_element element, element_id id local function core_map(args) - assert(util.is_int(args.reactor_l), "indicators.coremap: reactor_l is a required field") - assert(util.is_int(args.reactor_w), "indicators.coremap: reactor_w is a required field") + element.assert(util.is_int(args.reactor_l), "reactor_l is a required field") + element.assert(util.is_int(args.reactor_w), "reactor_w is a required field") -- require max dimensions args.width = 18 diff --git a/graphics/elements/indicators/data.lua b/graphics/elements/indicators/data.lua index 6097047..2304807 100644 --- a/graphics/elements/indicators/data.lua +++ b/graphics/elements/indicators/data.lua @@ -24,10 +24,10 @@ local element = require("graphics.element") ---@param args data_indicator_args ---@return graphics_element element, element_id id local function data(args) - assert(type(args.label) == "string", "indicators.data: label is a required field") - assert(type(args.format) == "string", "indicators.data: format is a required field") - assert(args.value ~= nil, "indicators.data: value is a required field") - assert(util.is_int(args.width), "indicators.data: width is a required field") + element.assert(type(args.label) == "string", "label is a required field") + element.assert(type(args.format) == "string", "format is a required field") + element.assert(args.value ~= nil, "value is a required field") + element.assert(util.is_int(args.width), "width is a required field") args.height = 1 diff --git a/graphics/elements/indicators/hbar.lua b/graphics/elements/indicators/hbar.lua index ef68dc6..eb4607a 100644 --- a/graphics/elements/indicators/hbar.lua +++ b/graphics/elements/indicators/hbar.lua @@ -22,9 +22,6 @@ local element = require("graphics.element") ---@param args hbar_args ---@return graphics_element element, element_id id local function hbar(args) - -- properties/state - local last_num_bars = -1 - -- create new graphics element base object local e = element.new(args) @@ -33,7 +30,9 @@ local function hbar(args) -- bar width is width - 5 characters for " 100%" if showing percent local bar_width = util.trinary(args.show_percent, e.frame.w - 5, e.frame.w) - assert(bar_width > 0, "indicators.hbar: too small for bar") + element.assert(bar_width > 0, "too small for bar") + + local last_num_bars = -1 -- determine bar colors local bar_bkg = e.fg_bg.blit_bkg diff --git a/graphics/elements/indicators/icon.lua b/graphics/elements/indicators/icon.lua index 70d7d2c..5c2fc4e 100644 --- a/graphics/elements/indicators/icon.lua +++ b/graphics/elements/indicators/icon.lua @@ -23,8 +23,8 @@ local element = require("graphics.element") ---@param args icon_indicator_args ---@return graphics_element element, element_id id local function icon(args) - assert(type(args.label) == "string", "indicators.icon: label is a required field") - assert(type(args.states) == "table", "indicators.icon: states is a required field") + element.assert(type(args.label) == "string", "label is a required field") + element.assert(type(args.states) == "table", "states is a required field") args.height = 1 args.width = math.max(args.min_label_width or 1, string.len(args.label)) + 4 diff --git a/graphics/elements/indicators/led.lua b/graphics/elements/indicators/led.lua index 72e1e93..011ee62 100644 --- a/graphics/elements/indicators/led.lua +++ b/graphics/elements/indicators/led.lua @@ -23,11 +23,11 @@ local flasher = require("graphics.flasher") ---@param args indicator_led_args ---@return graphics_element element, element_id id local function indicator_led(args) - assert(type(args.label) == "string", "indicators.led: label is a required field") - assert(type(args.colors) == "table", "indicators.led: colors is a required field") + element.assert(type(args.label) == "string", "label is a required field") + element.assert(type(args.colors) == "table", "colors is a required field") if args.flash then - assert(util.is_int(args.period), "indicators.led: period is a required field if flash is enabled") + element.assert(util.is_int(args.period), "period is a required field if flash is enabled") end args.height = 1 diff --git a/graphics/elements/indicators/ledpair.lua b/graphics/elements/indicators/ledpair.lua index 3ee99fb..1a81854 100644 --- a/graphics/elements/indicators/ledpair.lua +++ b/graphics/elements/indicators/ledpair.lua @@ -25,13 +25,13 @@ local flasher = require("graphics.flasher") ---@param args indicator_led_pair_args ---@return graphics_element element, element_id id local function indicator_led_pair(args) - assert(type(args.label) == "string", "indicators.ledpair: label is a required field") - assert(type(args.off) == "number", "indicators.ledpair: off is a required field") - assert(type(args.c1) == "number", "indicators.ledpair: c1 is a required field") - assert(type(args.c2) == "number", "indicators.ledpair: c2 is a required field") + element.assert(type(args.label) == "string", "label is a required field") + element.assert(type(args.off) == "number", "off is a required field") + element.assert(type(args.c1) == "number", "c1 is a required field") + element.assert(type(args.c2) == "number", "c2 is a required field") if args.flash then - assert(util.is_int(args.period), "indicators.ledpair: period is a required field if flash is enabled") + element.assert(util.is_int(args.period), "period is a required field if flash is enabled") end args.height = 1 diff --git a/graphics/elements/indicators/ledrgb.lua b/graphics/elements/indicators/ledrgb.lua index f2320c6..406e0cc 100644 --- a/graphics/elements/indicators/ledrgb.lua +++ b/graphics/elements/indicators/ledrgb.lua @@ -18,8 +18,8 @@ local element = require("graphics.element") ---@param args indicator_led_rgb_args ---@return graphics_element element, element_id id local function indicator_led_rgb(args) - assert(type(args.label) == "string", "indicators.ledrgb: label is a required field") - assert(type(args.colors) == "table", "indicators.ledrgb: colors is a required field") + element.assert(type(args.label) == "string", "label is a required field") + element.assert(type(args.colors) == "table", "colors is a required field") args.height = 1 args.width = math.max(args.min_label_width or 0, string.len(args.label)) + 2 diff --git a/graphics/elements/indicators/light.lua b/graphics/elements/indicators/light.lua index 742d6c1..290118c 100644 --- a/graphics/elements/indicators/light.lua +++ b/graphics/elements/indicators/light.lua @@ -23,11 +23,11 @@ local flasher = require("graphics.flasher") ---@param args indicator_light_args ---@return graphics_element element, element_id id local function indicator_light(args) - assert(type(args.label) == "string", "indicators.light: label is a required field") - assert(type(args.colors) == "table", "indicators.light: colors is a required field") + element.assert(type(args.label) == "string", "label is a required field") + element.assert(type(args.colors) == "table", "colors is a required field") if args.flash then - assert(util.is_int(args.period), "indicators.light: period is a required field if flash is enabled") + element.assert(util.is_int(args.period), "period is a required field if flash is enabled") end args.height = 1 diff --git a/graphics/elements/indicators/power.lua b/graphics/elements/indicators/power.lua index eaa1f68..96cbfd5 100644 --- a/graphics/elements/indicators/power.lua +++ b/graphics/elements/indicators/power.lua @@ -23,8 +23,8 @@ local element = require("graphics.element") ---@param args power_indicator_args ---@return graphics_element element, element_id id local function power(args) - assert(type(args.value) == "number", "indicators.power: value is a required number field") - assert(util.is_int(args.width), "indicators.power: width is a required field") + element.assert(type(args.value) == "number", "value is a required number field") + element.assert(util.is_int(args.width), "width is a required field") args.height = 1 diff --git a/graphics/elements/indicators/rad.lua b/graphics/elements/indicators/rad.lua index 2ae9b6f..5987625 100644 --- a/graphics/elements/indicators/rad.lua +++ b/graphics/elements/indicators/rad.lua @@ -24,10 +24,10 @@ local element = require("graphics.element") ---@param args rad_indicator_args ---@return graphics_element element, element_id id local function rad(args) - assert(type(args.value) ~= "number", "indicators.rad: value is a required number field") - assert(type(args.label) == "string", "indicators.rad: label is a required field") - assert(type(args.format) == "string", "indicators.rad: format is a required field") - assert(util.is_int(args.width), "indicators.rad: width is a required field") + element.assert(type(args.value) ~= "number", "value is a required number field") + element.assert(type(args.label) == "string", "label is a required field") + element.assert(type(args.format) == "string", "format is a required field") + element.assert(util.is_int(args.width), "width is a required field") args.height = 1 diff --git a/graphics/elements/indicators/state.lua b/graphics/elements/indicators/state.lua index 2096c6b..f2dc134 100644 --- a/graphics/elements/indicators/state.lua +++ b/graphics/elements/indicators/state.lua @@ -25,13 +25,11 @@ local element = require("graphics.element") ---@param args state_indicator_args ---@return graphics_element element, element_id id local function state_indicator(args) - assert(type(args.states) == "table", "indicators.state: states is a required field") + element.assert(type(args.states) == "table", "states is a required field") if util.is_int(args.height) then - assert(args.height % 2 == 1, "indicators.state: height should be an odd number") - else - args.height = 1 - end + element.assert(args.height % 2 == 1, "height should be an odd number") + else args.height = 1 end args.width = args.min_width or 1 diff --git a/graphics/elements/indicators/trilight.lua b/graphics/elements/indicators/trilight.lua index f90b21b..f5d441c 100644 --- a/graphics/elements/indicators/trilight.lua +++ b/graphics/elements/indicators/trilight.lua @@ -25,13 +25,13 @@ local flasher = require("graphics.flasher") ---@param args tristate_indicator_light_args ---@return graphics_element element, element_id id local function tristate_indicator_light(args) - assert(type(args.label) == "string", "indicators.trilight: label is a required field") - assert(type(args.c1) == "number", "indicators.trilight: c1 is a required field") - assert(type(args.c2) == "number", "indicators.trilight: c2 is a required field") - assert(type(args.c3) == "number", "indicators.trilight: c3 is a required field") + element.assert(type(args.label) == "string", "label is a required field") + element.assert(type(args.c1) == "number", "c1 is a required field") + element.assert(type(args.c2) == "number", "c2 is a required field") + element.assert(type(args.c3) == "number", "c3 is a required field") if args.flash then - assert(util.is_int(args.period), "indicators.trilight: period is a required field if flash is enabled") + element.assert(util.is_int(args.period), "period is a required field if flash is enabled") end args.height = 1 diff --git a/graphics/elements/multipane.lua b/graphics/elements/multipane.lua index 5cfd217..a283ed8 100644 --- a/graphics/elements/multipane.lua +++ b/graphics/elements/multipane.lua @@ -19,7 +19,7 @@ local element = require("graphics.element") ---@param args multipane_args ---@return graphics_element element, element_id id local function multipane(args) - assert(type(args.panes) == "table", "multipane: panes is a required field") + element.assert(type(args.panes) == "table", "panes is a required field") -- create new graphics element base object local e = element.new(args) diff --git a/graphics/elements/pipenet.lua b/graphics/elements/pipenet.lua index ce15c75..625a70d 100644 --- a/graphics/elements/pipenet.lua +++ b/graphics/elements/pipenet.lua @@ -24,7 +24,7 @@ local element = require("graphics.element") ---@param args pipenet_args ---@return graphics_element element, element_id id local function pipenet(args) - assert(type(args.pipes) == "table", "pipenet: pipes is a required field") + element.assert(type(args.pipes) == "table", "pipes is a required field") args.width = 0 args.height = 0 diff --git a/graphics/elements/rectangle.lua b/graphics/elements/rectangle.lua index dfd07c9..252790b 100644 --- a/graphics/elements/rectangle.lua +++ b/graphics/elements/rectangle.lua @@ -22,7 +22,7 @@ local element = require("graphics.element") ---@param args rectangle_args ---@return graphics_element element, element_id id local function rectangle(args) - assert(args.border ~= nil or args.thin ~= true, "rectangle: thin requires border to be provided") + element.assert(args.border ~= nil or args.thin ~= true, "thin requires border to be provided") -- if thin, then width will always need to be 1 if args.thin == true then @@ -65,8 +65,8 @@ local function rectangle(args) local inner_width = e.frame.w - width_x2 -- check dimensions - assert(width_x2 <= e.frame.w, "rectangle: border too thick for width") - assert(width_x2 <= e.frame.h, "rectangle: border too thick for height") + element.assert(width_x2 <= e.frame.w, "border too thick for width") + element.assert(width_x2 <= e.frame.h, "border too thick for height") -- form the basic line strings and top/bottom blit strings local spaces = util.spaces(e.frame.w) diff --git a/graphics/elements/textbox.lua b/graphics/elements/textbox.lua index cfd5585..a6f7207 100644 --- a/graphics/elements/textbox.lua +++ b/graphics/elements/textbox.lua @@ -24,7 +24,7 @@ local TEXT_ALIGN = core.TEXT_ALIGN ---@param args textbox_args ---@return graphics_element element, element_id id local function textbox(args) - assert(type(args.text) == "string", "textbox: text is a required field") + element.assert(type(args.text) == "string", "text is a required field") -- create new graphics element base object local e = element.new(args) diff --git a/graphics/elements/tiling.lua b/graphics/elements/tiling.lua index f40fef5..02e2605 100644 --- a/graphics/elements/tiling.lua +++ b/graphics/elements/tiling.lua @@ -22,7 +22,7 @@ local element = require("graphics.element") ---@param args tiling_args ---@return graphics_element element, element_id id local function tiling(args) - assert(type(args.fill_c) == "table", "tiling: fill_c is a required field") + element.assert(type(args.fill_c) == "table", "fill_c is a required field") -- create new graphics element base object local e = element.new(args) @@ -47,10 +47,10 @@ local function tiling(args) end -- check dimensions - assert(inner_width > 0, "tiling: inner_width <= 0") - assert(inner_height > 0, "tiling: inner_height <= 0") - assert(start_x <= inner_width, "tiling: start_x > inner_width") - assert(start_y <= inner_height, "tiling: start_y > inner_height") + element.assert(inner_width > 0, "inner_width <= 0") + element.assert(inner_height > 0, "inner_height <= 0") + element.assert(start_x <= inner_width, "start_x > inner_width") + element.assert(start_y <= inner_height, "start_y > inner_height") -- draw tiling box function e.redraw()