AgentE382

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

Dec 18th, 2013
80
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. return {
  4.     new = function()
  5.         local self = {
  6.             act = {pos = {}};
  7.             pos = {};
  8.             back = colors.white;
  9.             text = colors.lightGray;
  10.         }
  11.  
  12.         function self.write(_str)
  13.             local act = self.act
  14.             local selfpos = self.pos
  15.             local append = true
  16.             if selfpos[1] ~= act.pos[1] or selfpos[2] ~= act.pos[2] then
  17.                 act[#act + 1] = {term.setCursorPos, selfpos[1], selfpos[2]}
  18.                 append = false
  19.             end
  20.             if self.back ~= act.back then
  21.                 act[#act + 1] = {term.setBackgroundColor, self.back}
  22.                 act.back = self.back
  23.                 append = false
  24.             end
  25.             if self.text ~= act.text then
  26.                 act[#act + 1] = {term.setTextColor, self.text}
  27.                 act.text = self.text
  28.                 append = false
  29.             end
  30.             for line, nl in _str:gmatch("([^\n]*)(\n?)") do
  31.                 if append then
  32.                     act[#act][2] = act[#act][2]..line
  33.                     append = false
  34.                 else
  35.                     act[#act + 1] = {term.write, line}
  36.                 end
  37.                 selfpos[1] = selfpos[1] + #line
  38.                 if nl == "\n" then
  39.                     selfpos[1] = 1
  40.                     selfpos[2] = selfpos[2] + 1
  41.                     act[#act + 1] = {term.setCursorPos, 1, selfpos[2]}
  42.                 end
  43.             end
  44.             act.pos = {selfpos[1], selfpos[2]}
  45.             return self
  46.         end;
  47.         function self.draw(self)
  48.             for i, v in ipairs(self.act) do
  49.                 if v[3] then
  50.                     v[1](v[2], v[3])
  51.                 else
  52.                     v[1](v[2])
  53.                 end
  54.             end
  55.             self.act = {}
  56.             return self
  57.         end;
  58.  
  59.         return self
  60.     end;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment