mirror of
https://github.com/rojo-rbx/rojo.git
synced 2026-04-27 16:16:31 +00:00
Partial implementation of better formatting machinery
This commit is contained in:
@@ -1,18 +1,85 @@
|
|||||||
|
local function writeFmt(buffer, template, ...)
|
||||||
|
local currentArg = 0
|
||||||
|
local i = 1
|
||||||
|
local len = #template
|
||||||
|
|
||||||
|
while i < len do
|
||||||
|
local openBrace = template:find("{", i)
|
||||||
|
|
||||||
|
if openBrace == nil then
|
||||||
|
buffer:writeRaw(template:sub(i))
|
||||||
|
break
|
||||||
|
else
|
||||||
|
if openBrace - i > 0 then
|
||||||
|
buffer:writeRaw(template:sub(i, openBrace - 1))
|
||||||
|
end
|
||||||
|
|
||||||
|
local charAfterBrace = template:sub(openBrace + 1, openBrace + 1)
|
||||||
|
if charAfterBrace == "{" then
|
||||||
|
buffer:writeRaw(template:sub(i, openBrace))
|
||||||
|
i = openBrace + 2
|
||||||
|
else
|
||||||
|
local closeBrace = template:find("}", openBrace + 1)
|
||||||
|
assert(closeBrace ~= nil, "Unclosed formatting specifier. Use '{{' to write an open brace.")
|
||||||
|
|
||||||
|
local formatSpecifier = template:sub(openBrace + 1, closeBrace - 1)
|
||||||
|
|
||||||
|
currentArg = currentArg + 1
|
||||||
|
|
||||||
|
if formatSpecifier == "" then
|
||||||
|
local arg = select(currentArg, ...)
|
||||||
|
buffer:writeRaw(tostring(arg))
|
||||||
|
else
|
||||||
|
error("unsupported format specifier " .. formatSpecifier, 2)
|
||||||
|
end
|
||||||
|
|
||||||
|
i = closeBrace + 1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local function writeLineFmt(buffer, template, ...)
|
||||||
|
writeFmt(buffer, template, ...)
|
||||||
|
table.insert(buffer, "\n")
|
||||||
|
end
|
||||||
|
|
||||||
local function debugOutputBuffer()
|
local function debugOutputBuffer()
|
||||||
local buffer = {}
|
local buffer = {}
|
||||||
local indentLevel = 0
|
local indentLevel = 0
|
||||||
local indentation = ""
|
local indentation = ""
|
||||||
|
|
||||||
function buffer:push(template, ...)
|
function buffer:writeLine(template, ...)
|
||||||
local value = string.format(template, ...)
|
return writeLineFmt(self, template, ...)
|
||||||
|
end
|
||||||
|
|
||||||
|
function buffer:write(template, ...)
|
||||||
|
return writeFmt(self, template, ...)
|
||||||
|
end
|
||||||
|
|
||||||
|
function buffer:writeRaw(value)
|
||||||
if #indentation > 0 then
|
if #indentation > 0 then
|
||||||
value = indentation .. value:gsub("\n", "\n" .. indentation)
|
value = value:gsub("\n", "\n" .. indentation)
|
||||||
end
|
end
|
||||||
|
|
||||||
table.insert(self, value)
|
table.insert(self, value)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function buffer:writeLineRaw(piece)
|
||||||
|
if #indentation > 0 then
|
||||||
|
self:writeRaw(indentation)
|
||||||
|
end
|
||||||
|
|
||||||
|
self:writeRaw(piece)
|
||||||
|
table.insert(self, "\n")
|
||||||
|
end
|
||||||
|
|
||||||
|
function buffer:push(template, ...)
|
||||||
|
local value = string.format(template, ...)
|
||||||
|
|
||||||
|
self:writeLineRaw(value)
|
||||||
|
end
|
||||||
|
|
||||||
function buffer:indent()
|
function buffer:indent()
|
||||||
indentLevel = indentLevel + 1
|
indentLevel = indentLevel + 1
|
||||||
indentation = string.rep(" ", indentLevel)
|
indentation = string.rep(" ", indentLevel)
|
||||||
@@ -24,7 +91,7 @@ local function debugOutputBuffer()
|
|||||||
end
|
end
|
||||||
|
|
||||||
function buffer:finish()
|
function buffer:finish()
|
||||||
return table.concat(self, "\n")
|
return table.concat(self, "")
|
||||||
end
|
end
|
||||||
|
|
||||||
return buffer
|
return buffer
|
||||||
@@ -51,4 +118,6 @@ end
|
|||||||
|
|
||||||
return {
|
return {
|
||||||
debugOutputBuffer = debugOutputBuffer,
|
debugOutputBuffer = debugOutputBuffer,
|
||||||
|
writeFmt = writeFmt,
|
||||||
|
writeLineFmt = writeLineFmt,
|
||||||
}
|
}
|
||||||
@@ -20,7 +20,7 @@ local function DEBUG_showPatch(patch)
|
|||||||
output:indent()
|
output:indent()
|
||||||
|
|
||||||
for removed in ipairs(patch.removed) do
|
for removed in ipairs(patch.removed) do
|
||||||
output:push("Remove ID %s", removed)
|
output:writeLine("Remove ID {}", removed)
|
||||||
end
|
end
|
||||||
|
|
||||||
for id, added in pairs(patch.added) do
|
for id, added in pairs(patch.added) do
|
||||||
|
|||||||
Reference in New Issue
Block a user