Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Buffer API, By Symmetryc
- -- Edited by AgentE382 to change ":" OO-syntax to "." OO-syntax. (First step to redirect target based on this.)
- -- Edited by AgentE382 to add preliminary redirect-target capability.
- -- Edited by AgentE382 to add full redirect-target functionality.
- -- Edited by AgentE382 to fix full redirect-target functionality.
- return {
- new = function()
- local redirectStack = {}
- local out = term.native
- local self = {
- act = {pos = {}};
- pos = {};
- back = colors.white;
- text = colors.lightGray;
- blink = true
- }
- function self.write(_str)
- local act = self.act
- local selfpos = self.pos
- local append = true
- if selfpos[1] ~= act.pos[1] or selfpos[2] ~= act.pos[2] then
- act[#act + 1] = {out.setCursorPos, selfpos[1], selfpos[2]}
- append = false
- end
- if self.back ~= act.back then
- act[#act + 1] = {out.setBackgroundColor, self.back}
- act.back = self.back
- append = false
- end
- if self.text ~= act.text then
- act[#act + 1] = {out.setTextColor, self.text}
- act.text = self.text
- append = false
- end
- if self.blink ~= act.blink then
- act[#act + 1] = {out.setCursorBlink, self.blink}
- act.blink = self.blink
- append = false
- end
- for line, nl in _str:gmatch("([^\n]*)(\n?)") do
- if append then
- act[#act][2] = act[#act][2]..line
- append = false
- else
- act[#act + 1] = {out.write, line}
- end
- selfpos[1] = selfpos[1] + #line
- if nl == "\n" then
- selfpos[1] = 1
- selfpos[2] = selfpos[2] + 1
- act[#act + 1] = {out.setCursorPos, 1, selfpos[2]}
- end
- end
- act.pos = {selfpos[1], selfpos[2]}
- return self
- end;
- function self.draw(self)
- for i, v in ipairs(self.act) do
- if v[3] then
- v[1](v[2], v[3])
- else
- v[1](v[2])
- end
- end
- self.act = {}
- return self
- end;
- function self.clear()
- local a = self.act
- if a[#a][1] ~= out.clear then
- a[#a + 1] = {out.clear}
- end
- return self
- end
- function self.clearLine()
- local a = self.act
- if a[#a][1] ~= out.clearLine then
- a[#a + 1] = {out.clearLine}
- end
- return self
- end
- function self.getCursorPos()
- local p = self.pos
- return p[1], p[2]
- end
- function self.setCursorPos(x, y)
- local p = self.pos
- p[1], p[2] = x, y
- return self
- end
- function self.setCursorBlink(state)
- self.blink = state
- return self
- end
- function self.isColor()
- return out.isColor and out.isColor()
- end
- function self.getSize()
- return out.getSize()
- end
- function self.redirect(target)
- redirectStack[#redirectStack + 1] = out
- out = target
- return self
- end
- function self.restore()
- out = redirectStack[#redirectStack] or out
- redirectStack[#redirectStack] = nil
- return self
- end
- function self.scroll(n)
- local a = self.act
- if a[#a][1] ~= out.scroll then
- a[#a + 1] = {out.scroll, n}
- else
- local s = a[#a]
- s[2] = s[2] + n
- end
- return self
- end
- function self.setTextColor(color)
- self.text = color
- return self
- end
- function self.setBackgroundColor(color)
- self.back = color
- return self
- end
- return self
- end;
- }
Advertisement
Add Comment
Please, Sign In to add comment