AgentE382

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

Dec 18th, 2013
213
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. return {
  6.     new = function()
  7.         local self = {
  8.             act = {pos = {}};
  9.             pos = {};
  10.             back = colors.white;
  11.             text = colors.lightGray;
  12.             blink = true
  13.         }
  14.  
  15.         function self.write(_str)
  16.             local act = self.act
  17.             local selfpos = self.pos
  18.             local append = true
  19.             if selfpos[1] ~= act.pos[1] or selfpos[2] ~= act.pos[2] then
  20.                 act[#act + 1] = {term.setCursorPos, selfpos[1], selfpos[2]}
  21.                 append = false
  22.             end
  23.             if self.back ~= act.back then
  24.                 act[#act + 1] = {term.setBackgroundColor, self.back}
  25.                 act.back = self.back
  26.                 append = false
  27.             end
  28.             if self.text ~= act.text then
  29.                 act[#act + 1] = {term.setTextColor, self.text}
  30.                 act.text = self.text
  31.                 append = false
  32.             end
  33.             if self.blink ~= act.blink then
  34.                 act[#act + 1] = {term.setCursorBlink, self.blink}
  35.                 act.blink = self.blink
  36.                 append = false
  37.             end
  38.             for line, nl in _str:gmatch("([^\n]*)(\n?)") do
  39.                 if append then
  40.                     act[#act][2] = act[#act][2]..line
  41.                     append = false
  42.                 else
  43.                     act[#act + 1] = {term.write, line}
  44.                 end
  45.                 selfpos[1] = selfpos[1] + #line
  46.                 if nl == "\n" then
  47.                     selfpos[1] = 1
  48.                     selfpos[2] = selfpos[2] + 1
  49.                     act[#act + 1] = {term.setCursorPos, 1, selfpos[2]}
  50.                 end
  51.             end
  52.             act.pos = {selfpos[1], selfpos[2]}
  53.             return self
  54.         end;
  55.         function self.draw(self)
  56.             for i, v in ipairs(self.act) do
  57.                 if v[3] then
  58.                     v[1](v[2], v[3])
  59.                 else
  60.                     v[1](v[2])
  61.                 end
  62.             end
  63.             self.act = {}
  64.             return self
  65.         end;
  66.  
  67.         function self.clear()
  68.             local a = self.act
  69.             for i = #a, 1 do
  70.                 a[i] = nil
  71.             end
  72.             return self
  73.         end
  74.  
  75.         function self.clearLine()
  76.             local a = self.act
  77.             if a[#a][1] ~= term.clearLine then
  78.                 a[#a + 1] = {term.clearLine}
  79.             end
  80.             return self
  81.         end
  82.  
  83.         function self.getCursorPos()
  84.             local p = self.pos
  85.             return p[1], p[2]
  86.         end
  87.  
  88.         function self.setCursorPos(x, y)
  89.             local p = self.pos
  90.             p[1], p[2] = x, y
  91.             return self
  92.         end
  93.  
  94.         function self.setCursorBlink(state)
  95.             self.blink = state
  96.             return self
  97.         end
  98.  
  99.         self.isColor = term.isColor
  100.  
  101.         self.getSize = term.getSize
  102.  
  103.         function self.redirect(target)
  104.             term.redirect(target)
  105.             return self
  106.         end
  107.  
  108.         function self.restore()
  109.             term.restore()
  110.             return self
  111.         end
  112.  
  113.         function self.scroll(n)
  114.             local a = self.act
  115.             if a[#a][1] ~= term.scroll then
  116.                 a[#a + 1] = {term.scroll, n}
  117.             else
  118.                 local s = a[#a]
  119.                 s[2] = s[2] + n
  120.             end
  121.             return self
  122.         end
  123.  
  124.         function self.setTextColor(color)
  125.             self.text = color
  126.             return self
  127.         end
  128.  
  129.         function self.setBackgroundColor(color)
  130.             self.back = color
  131.             return self
  132.         end
  133.  
  134.         return self
  135.     end;
  136. }
Advertisement
Add Comment
Please, Sign In to add comment