Advertisement
Symmetryc

Buffer v2

Dec 21st, 2013
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.46 KB | None | 0 0
  1. -- Buffer API, by Symmetryc
  2. return {
  3.     new = function()
  4.         return {
  5.             act = {};
  6.             pos_x = 1;
  7.             pos_y = 1;
  8.             shift_x = 0;
  9.             shift_y = 0;
  10.             back = colors.gray;
  11.             text = colors.lightGray;
  12.             blink = false;
  13.             write = function(self, _str)
  14.                 local act = self.act
  15.                 local append = true
  16.                 if self.pos_x ~= act.pos_y or self.pos_x ~= act.pos_y then
  17.                     act[#act + 1] = {term.setCursorPos, self.pos_x, self.pos_y}
  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.                     if nl == "\n" then
  38.                         self.pos_y = self.pos_y + 1
  39.                         act[#act + 1] = {term.setCursorPos, self.pos_x, self.pos_y}
  40.                     else
  41.                         self.pos_x = self.pos_x + #line
  42.                     end
  43.                 end
  44.                 act.pos_x, act.pos_y = self.pos_x, self.pox_y
  45.                 return self
  46.             end;
  47.             draw = function(self)
  48.                 for i, v in ipairs(self.act) do
  49.                     if v[3] then
  50.                         v[1](v[2] + self.shift_x, v[3] + self.shift_y)
  51.                     else
  52.                         v[1](v[2])
  53.                     end
  54.                 end
  55.                 term.setCursorBlink(self.blink)
  56.                 return self
  57.             end;
  58.         }
  59.     end;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement