Advertisement
Symmetryc

Buffer API

Dec 16th, 2013
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.37 KB | None | 0 0
  1. -- Buffer API, By Symmetryc
  2. return {
  3.     new = function()
  4.         return {
  5.             act = {pos = {}};
  6.             pos = {};
  7.             back = colors.white;
  8.             text = colors.lightGray;
  9.             write = function(self, _str)
  10.                 local act = self.act
  11.                 local selfpos = self.pos
  12.                 local append = true
  13.                 if selfpos[1] ~= act.pos[1] or selfpos[2] ~= act.pos[2] then
  14.                     act[#act + 1] = {term.setCursorPos, selfpos[1], selfpos[2]}
  15.                     append = false
  16.                 end
  17.                 if self.back ~= act.back then
  18.                     act[#act + 1] = {term.setBackgroundColor, self.back}
  19.                     act.back = self.back
  20.                     append = false
  21.                 end
  22.                 if self.text ~= act.text then
  23.                     act[#act + 1] = {term.setTextColor, self.text}
  24.                     act.text = self.text
  25.                     append = false
  26.                 end
  27.                 for line, nl in _str:gmatch("([^\n]*)(\n?)") do
  28.                     if append then
  29.                         act[#act][2] = act[#act][2]..line
  30.                         append = false
  31.                     else
  32.                         act[#act + 1] = {term.write, line}
  33.                     end
  34.                     selfpos[1] = selfpos[1] + #line
  35.                     if nl == "\n" then
  36.                         selfpos[1] = 1
  37.                         selfpos[2] = selfpos[2] + 1
  38.                         act[#act + 1] = {term.setCursorPos, 1, selfpos[2]}
  39.                     end
  40.                 end
  41.                 act.pos = {selfpos[1], selfpos[2]}
  42.                 return self
  43.             end;
  44.             draw = function(self)
  45.                 for i, v in ipairs(self.act) do
  46.                     if v[3] then
  47.                         v[1](v[2], v[3])
  48.                     else
  49.                         v[1](v[2])
  50.                     end
  51.                 end
  52.                 self.act = {}
  53.                 return self
  54.             end;
  55.         }
  56.     end;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement