AgentE382

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

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