util pad function

This commit is contained in:
Mikayla Fischler 2022-08-16 11:22:06 -04:00
parent 3c2f631451
commit 7f011369c4
3 changed files with 15 additions and 10 deletions

View File

@ -45,11 +45,7 @@ local function state_indicator(args)
args.width = string.len(state_def.text) args.width = string.len(state_def.text)
end end
local len = string.len(state_def.text) local text = util.pad(state_def.text, args.width)
local lpad = math.floor((args.width - len) / 2)
local rpad = args.width - lpad
local text = util.spaces(lpad) .. state_def.text .. util.spaces(rpad)
table.insert(state_blit_cmds, { table.insert(state_blit_cmds, {
text = text, text = text,

View File

@ -231,17 +231,14 @@ function log.dmesg_working(msg, tag, tag_color)
end end
local function done(ok) 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) out.setCursorPos(ts_coord.x1, ts_coord.y)
if ok or ok == nil then if ok or ok == nil then
out.setTextColor(colors.green) out.setTextColor(colors.green)
out.write(util.spaces(lpad) .. "DONE" .. util.spaces(rpad)) out.write(util.pad("DONE", width))
else else
out.setTextColor(colors.red) out.setTextColor(colors.red)
out.write(util.spaces(lpad) .. "FAIL" .. util.spaces(rpad)) out.write(util.pad("FAIL", width))
end end
out.setTextColor(initial_color) out.setTextColor(initial_color)

View File

@ -75,6 +75,18 @@ function util.spaces(n)
return util.strrep(" ", n) return util.strrep(" ", n)
end 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 -- wrap a string into a table of lines, supporting single dash splits
---@param str string ---@param str string
---@param limit integer line limit ---@param limit integer line limit