Guest User

buffer

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