#204 replaced util.strwrap implementation with cc.strings.wrap

This commit is contained in:
Mikayla Fischler 2023-04-11 23:53:42 -04:00
parent fc9d86f23e
commit 3ae39b2455
4 changed files with 7 additions and 46 deletions

View File

@ -19,7 +19,7 @@ local iocontrol = require("coordinator.iocontrol")
local renderer = require("coordinator.renderer")
local sounder = require("coordinator.sounder")
local COORDINATOR_VERSION = "v0.12.5"
local COORDINATOR_VERSION = "v0.12.6"
local print = util.print
local println = util.println

File diff suppressed because one or more lines are too long

View File

@ -18,7 +18,7 @@ local plc = require("reactor-plc.plc")
local renderer = require("reactor-plc.renderer")
local threads = require("reactor-plc.threads")
local R_PLC_VERSION = "v1.1.4"
local R_PLC_VERSION = "v1.1.5"
local print = util.print
local println = util.println

View File

@ -2,6 +2,8 @@
-- Utility Functions
--
local cc_strings = require("cc.strings")
---@class util
local util = {}
@ -104,53 +106,12 @@ function util.pad(str, n)
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
---@nodiscard
---@param str string
---@param limit integer line limit
---@return table lines
function util.strwrap(str, limit)
local lines = {}
local ln_start = 1
local first_break = str:find("([%-%s]+)")
if first_break ~= nil then
lines[1] = string.sub(str, 1, first_break - 1)
else
lines[1] = str
end
---@diagnostic disable-next-line: discard-returns
str:gsub("(%s+)()(%S+)()",
function(space, start, word, stop)
-- support splitting SINGLE DASH words
word:gsub("(%S+)(%-)()(%S+)()",
function (pre, dash, d_start, post, d_stop)
if (stop + d_stop) - ln_start <= limit then
-- do nothing, it will entirely fit
elseif ((start + d_start) + 1) - ln_start <= limit then
-- we can fit including the dash
lines[#lines] = lines[#lines] .. space .. pre .. dash
-- drop the space and replace the word with the post
space = ""
word = post
-- force a wrap
stop = limit + 1 + ln_start
-- change start position for new line start
start = start + d_start - 1
end
end)
-- can we append this or do we have to start a new line?
if stop - ln_start > limit then
-- starting new line
ln_start = start
lines[#lines + 1] = word
else lines[#lines] = lines[#lines] .. space .. word end
end)
return lines
end
function util.strwrap(str, limit) return cc_strings.wrap(str, limit) end
-- concatenation with built-in to string
---@nodiscard