-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathemitter.lua
More file actions
50 lines (40 loc) · 880 Bytes
/
emitter.lua
File metadata and controls
50 lines (40 loc) · 880 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
46
47
48
49
50
local Emitter = {}
Emitter.__index = Emitter
function Emitter.create()
return setmetatable({depth=0}, Emitter)
end
function Emitter:add(s)
table.insert(self, s)
end
function Emitter:add_ln(s)
if s then
table.insert(self, s)
end
table.insert(self, '\n')
end
function Emitter:generate()
for i,chunk in ipairs(self) do
if type(chunk) == 'function' then
self[i] = chunk()
end
end
return table.concat(self)
end
function Emitter:add_indent(s)
table.insert(self, string.rep(' ', self.depth))
if s then
table.insert(self, s)
end
end
function Emitter:add_indent_ln(s)
self:add_indent(s)
self:add_ln()
end
function Emitter:inc_indent()
self.depth = self.depth + 1
end
function Emitter:dec_indent()
self.depth = self.depth - 1
end
setmetatable(Emitter, {__call = function(_, ...) return Emitter.create(...) end})
return Emitter