Partial implementation of better formatting machinery

This commit is contained in:
Lucien Greathouse
2019-11-14 18:43:51 -08:00
parent 3f8b178f88
commit b7d026b98e
2 changed files with 74 additions and 5 deletions

View File

@@ -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 buffer = {}
local indentLevel = 0
local indentation = ""
function buffer:push(template, ...)
local value = string.format(template, ...)
function buffer:writeLine(template, ...)
return writeLineFmt(self, template, ...)
end
function buffer:write(template, ...)
return writeFmt(self, template, ...)
end
function buffer:writeRaw(value)
if #indentation > 0 then
value = indentation .. value:gsub("\n", "\n" .. indentation)
value = value:gsub("\n", "\n" .. indentation)
end
table.insert(self, value)
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()
indentLevel = indentLevel + 1
indentation = string.rep(" ", indentLevel)
@@ -24,7 +91,7 @@ local function debugOutputBuffer()
end
function buffer:finish()
return table.concat(self, "\n")
return table.concat(self, "")
end
return buffer
@@ -51,4 +118,6 @@ end
return {
debugOutputBuffer = debugOutputBuffer,
writeFmt = writeFmt,
writeLineFmt = writeLineFmt,
}