-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuilder.lua
More file actions
45 lines (35 loc) · 808 Bytes
/
builder.lua
File metadata and controls
45 lines (35 loc) · 808 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
local Builder = {}
Builder.__index = Builder
function Builder.new(pretty)
local self = setmetatable({}, Builder)
self.strs = {}
self.pretty = pretty
self.indents = 0
return self
end
function Builder:push(str)
if not str then return end
self.strs[#self.strs+1] = str
end
local indent_cache = {}
function Builder:indent()
if not self.pretty then return end
self.indents = self.indents + 1
end
function Builder:unindent()
if not self.pretty then return end
self.indents = self.indents - 1
end
function Builder:newline()
if not self.pretty then return end
local str = indent_cache[self.indents]
if not str then
str = '\n' .. (' '):rep(self.indents)
indent_cache[self.indents] = str
end
self:push(str)
end
function Builder:build()
return table.concat(self.strs)
end
return Builder