AgentE382

Symmetryc's Buffer API, "."-style, redirect.(Bug Untested)

Dec 18th, 2013
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- Buffer API, By Symmetryc
  2. -- Edited by AgentE382 to change ":" OO-syntax to "." OO-syntax. (First step to redirect target based on this.)
  3. -- Edited by AgentE382 to add preliminary redirect-target capability.
  4. -- Edited by AgentE382 to add full redirect-target functionality.
  5. -- Edited by AgentE382 to fix full redirect-target functionality.
  6. return {
  7.     new = function()
  8.         local redirectStack = {}
  9.  
  10.         local out = term.native
  11.  
  12.         local self = {
  13.             act = {pos = {}};
  14.             pos = {};
  15.             back = colors.white;
  16.             text = colors.lightGray;
  17.             blink = true
  18.         }
  19.  
  20.         function self.write(_str)
  21.             local act = self.act
  22.             local selfpos = self.pos
  23.             local append = true
  24.             if selfpos[1] ~= act.pos[1] or selfpos[2] ~= act.pos[2] then
  25.                 act[#act + 1] = {out.setCursorPos, selfpos[1], selfpos[2]}
  26.                 append = false
  27.             end
  28.             if self.back ~= act.back then
  29.                 act[#act + 1] = {out.setBackgroundColor, self.back}
  30.                 act.back = self.back
  31.                 append = false
  32.             end
  33.             if self.text ~= act.text then
  34.                 act[#act + 1] = {out.setTextColor, self.text}
  35.                 act.text = self.text
  36.                 append = false
  37.             end
  38.             if self.blink ~= act.blink then
  39.                 act[#act + 1] = {out.setCursorBlink, self.blink}
  40.                 act.blink = self.blink
  41.                 append = false
  42.             end
  43.             for line, nl in _str:gmatch("([^\n]*)(\n?)") do
  44.                 if append then
  45.                     act[#act][2] = act[#act][2]..line
  46.                     append = false
  47.                 else
  48.                     act[#act + 1] = {out.write, line}
  49.                 end
  50.                 selfpos[1] = selfpos[1] + #line
  51.                 if nl == "\n" then
  52.                     selfpos[1] = 1
  53.                     selfpos[2] = selfpos[2] + 1
  54.                     act[#act + 1] = {out.setCursorPos, 1, selfpos[2]}
  55.                 end
  56.             end
  57.             act.pos = {selfpos[1], selfpos[2]}
  58.             return self
  59.         end;
  60.         function self.draw(self)
  61.             for i, v in ipairs(self.act) do
  62.                 if v[3] then
  63.                     v[1](v[2], v[3])
  64.                 else
  65.                     v[1](v[2])
  66.                 end
  67.             end
  68.             self.act = {}
  69.             return self
  70.         end;
  71.  
  72.         function self.clear()
  73.             local a = self.act
  74.             if a[#a][1] ~= out.clear then
  75.                 a[#a + 1] = {out.clear}
  76.             end
  77.             return self
  78.         end
  79.  
  80.         function self.clearLine()
  81.             local a = self.act
  82.             if a[#a][1] ~= out.clearLine then
  83.                 a[#a + 1] = {out.clearLine}
  84.             end
  85.             return self
  86.         end
  87.  
  88.         function self.getCursorPos()
  89.             local p = self.pos
  90.             return p[1], p[2]
  91.         end
  92.  
  93.         function self.setCursorPos(x, y)
  94.             local p = self.pos
  95.             p[1], p[2] = x, y
  96.             return self
  97.         end
  98.  
  99.         function self.setCursorBlink(state)
  100.             self.blink = state
  101.             return self
  102.         end
  103.  
  104.         function self.isColor()
  105.             return out.isColor and out.isColor()
  106.         end
  107.  
  108.         function self.getSize()
  109.             return out.getSize()
  110.         end
  111.  
  112.         function self.redirect(target)
  113.             redirectStack[#redirectStack + 1] = out
  114.             out = target
  115.             return self
  116.         end
  117.  
  118.         function self.restore()
  119.             out = redirectStack[#redirectStack] or out
  120.             redirectStack[#redirectStack] = nil
  121.             return self
  122.         end
  123.  
  124.         function self.scroll(n)
  125.             local a = self.act
  126.             if a[#a][1] ~= out.scroll then
  127.                 a[#a + 1] = {out.scroll, n}
  128.             else
  129.                 local s = a[#a]
  130.                 s[2] = s[2] + n
  131.             end
  132.             return self
  133.         end
  134.  
  135.         function self.setTextColor(color)
  136.             self.text = color
  137.             return self
  138.         end
  139.  
  140.         function self.setBackgroundColor(color)
  141.             self.back = color
  142.             return self
  143.         end
  144.  
  145.         return self
  146.     end;
  147. }
Advertisement
Add Comment
Please, Sign In to add comment