diff --git a/graphics/elements/indicators/state.lua b/graphics/elements/indicators/state.lua index a3d5854..5dd39ee 100644 --- a/graphics/elements/indicators/state.lua +++ b/graphics/elements/indicators/state.lua @@ -45,11 +45,7 @@ local function state_indicator(args) args.width = string.len(state_def.text) end - local len = string.len(state_def.text) - local lpad = math.floor((args.width - len) / 2) - local rpad = args.width - lpad - - local text = util.spaces(lpad) .. state_def.text .. util.spaces(rpad) + local text = util.pad(state_def.text, args.width) table.insert(state_blit_cmds, { text = text, diff --git a/scada-common/log.lua b/scada-common/log.lua index 08aa8c8..dadba40 100644 --- a/scada-common/log.lua +++ b/scada-common/log.lua @@ -231,17 +231,14 @@ function log.dmesg_working(msg, tag, tag_color) end local function done(ok) - local lpad = math.max(math.floor((width - 4) / 2), 0) - local rpad = (width - 4) - lpad - out.setCursorPos(ts_coord.x1, ts_coord.y) if ok or ok == nil then out.setTextColor(colors.green) - out.write(util.spaces(lpad) .. "DONE" .. util.spaces(rpad)) + out.write(util.pad("DONE", width)) else out.setTextColor(colors.red) - out.write(util.spaces(lpad) .. "FAIL" .. util.spaces(rpad)) + out.write(util.pad("FAIL", width)) end out.setTextColor(initial_color) diff --git a/scada-common/util.lua b/scada-common/util.lua index ccf9ff4..68957f9 100644 --- a/scada-common/util.lua +++ b/scada-common/util.lua @@ -75,6 +75,18 @@ function util.spaces(n) return util.strrep(" ", n) end +-- pad text to a minimum width +---@param str string text +---@param n integer minimum width +---@return string +function util.pad(str, n) + local len = string.len(str) + local lpad = math.floor((n - len) / 2) + local rpad = (n - len) - lpad + + return util.spaces(lpad) .. str .. util.spaces(rpad) +end + -- wrap a string into a table of lines, supporting single dash splits ---@param str string ---@param limit integer line limit