I found myself in a need of a function earlier that will basically accept an array and integer and return a nicely formatted string. While I am sure there may be alternative and better methods of accomplishing this, it is my hope that some people can find this useful.
The function will turn:
t = {
"Go",
"placidly",
"amid",
"the",
"noise",
"and",
"haste",
"and",
"remember",
"what",
"peace",
"there",
"may",
"be",
"in",
"silence"
}
Into:
Code: Select all
Go placidly amid
the noise and
haste and remember
what peace there
may be in
silence
Further, any integer passed as a second argument will set the padding to that value.
function nconc(t, delim, col)
local delim = delim or 25
local col = col or "grey"
local i = 0
for _, v in ipairs(t) do
i = i + 1
local d = ""
d = d .. v
if (i - 1)%3 == 0 then
echo("\n")
end
local o = v
if o:len() < delim and (i - 1)%3 ~= 2 then
local pad = delim - o:len()
o = o .. string.rep(" ", pad)
elseif o:len() > delim then
o = o:cut(delim)
end
cecho("<"..col..">" .. o)
end
end