mirror of
https://github.com/1bardesign/batteries.git
synced 2024-11-25 23:24:35 +00:00
[modified] stringx.pretty handles embedded tables better (still likely needs a wrap width instead of an element count per line)
This commit is contained in:
parent
ab93bcef6b
commit
04563ed4b7
27
stringx.lua
27
stringx.lua
@ -88,8 +88,8 @@ function stringx.pretty(input, indent, after)
|
|||||||
if type(indent) == "number" then
|
if type(indent) == "number" then
|
||||||
indent = (" "):rep(indent)
|
indent = (" "):rep(indent)
|
||||||
end
|
end
|
||||||
newline = indent == "" and "" or "\n"
|
|
||||||
separator = indent == "" and ", " or ",\n"..indent
|
local newline = indent == "" and "" or "\n"
|
||||||
|
|
||||||
local function internal_value(v)
|
local function internal_value(v)
|
||||||
v = stringx.pretty(v, indent, after)
|
v = stringx.pretty(v, indent, after)
|
||||||
@ -127,23 +127,42 @@ function stringx.pretty(input, indent, after)
|
|||||||
if after and after > 1 then
|
if after and after > 1 then
|
||||||
local line_chunks = {}
|
local line_chunks = {}
|
||||||
while #chunks > 0 do
|
while #chunks > 0 do
|
||||||
|
local break_next = false
|
||||||
local line = {}
|
local line = {}
|
||||||
for i = 1, after do
|
for i = 1, after do
|
||||||
if #chunks == 0 then
|
if #chunks == 0 then
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
local v = table.remove(chunks, 1)
|
local v = chunks[1]
|
||||||
table.insert(line, v)
|
--tables split to own line
|
||||||
|
if v:find("{") then
|
||||||
|
--break line here
|
||||||
|
break_next = true
|
||||||
|
break
|
||||||
|
else
|
||||||
|
table.insert(line, table.remove(chunks, 1))
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
if #line > 0 then
|
||||||
table.insert(line_chunks, table.concat(line, ", "))
|
table.insert(line_chunks, table.concat(line, ", "))
|
||||||
end
|
end
|
||||||
|
if break_next then
|
||||||
|
table.insert(line_chunks, table.remove(chunks, 1))
|
||||||
|
break_next = false
|
||||||
|
end
|
||||||
|
end
|
||||||
chunks = line_chunks
|
chunks = line_chunks
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local multiline = #chunks > 1
|
||||||
|
local separator = (indent == "" or not multiline) and ", " or ",\n"..indent
|
||||||
|
|
||||||
|
if multiline then
|
||||||
return "{" .. newline ..
|
return "{" .. newline ..
|
||||||
indent .. table.concat(chunks, separator) .. newline ..
|
indent .. table.concat(chunks, separator) .. newline ..
|
||||||
"}"
|
"}"
|
||||||
end
|
end
|
||||||
|
return "{" .. table.concat(chunks, separator) .. "}"
|
||||||
|
end
|
||||||
|
|
||||||
return stringx
|
return stringx
|
||||||
|
Loading…
Reference in New Issue
Block a user