Guest User

Untitled

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