Thomasims

colorConsole

Jan 16th, 2013
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.90 KB | None | 0 0
  1. function cut(str, len)
  2.   local returned = {}
  3.   local lastT = 1
  4.   for i = 1, str:len() do
  5.     if i == len+lastT then
  6.       returned[#returned + 1] = str:sub(lastT, i -1 )
  7.       lastT = i
  8.     end
  9.   end
  10.   returned[#returned+1] = str:sub(lastT, str:len())
  11.   return returned
  12. end
  13.  
  14. local colorCon = {
  15.  
  16.   addMessage = function(self, strs, clrs)
  17.     local totStr = ""
  18.     for k = 1, strs do
  19.       totStr = totStr..strs[k]
  20.     end
  21.     local linesN = math.ceil(totStr:len() / self.w)
  22.     local lines = cut(totStr, self.w)
  23.     for i = 1, #lines do
  24.       msgs[#msgs+1] = lines[i]
  25.       if #t == self.h then
  26.         for j = 1, #t do
  27.           t[j] = t[j+1]
  28.         end
  29.       end
  30.       local ttl = self.w - (strs[1]:len() + strs[2]:len())
  31.       if i == 1 then
  32.         t[#t] = {strs[1],strs[2],strs[3]:sub((strs[1]:len() + strs[2]:len() ) + 1, strs[3], ttl + 1)}
  33.         tC[#tC] = clrs
  34.       else
  35.         t[#t] = {strs[3]:sub(ttl + 2, (ttl + 1) + self.w)}
  36.         tC[#tC] = {clrs[3]}
  37.       end
  38.     end
  39.   end,
  40.  
  41.   display = function(self)
  42.     term.setBackgroundColor(self.backC)
  43.     for i = 1, self.h do
  44.       local lastT = 1
  45.       for j = 1, self.t[i] do
  46.         term.setTextColor(self.tC[i][j])
  47.         term.setCursorPos(self.x+lastT-1,self.y+i-1)
  48.         term.write(self.t[i][j])
  49.         lastT = lastT + self.t[i][j]:len()
  50.       end
  51.     end
  52.   end,
  53.   
  54.   update = function(self, e, a1, a2, a3)
  55.     return true
  56.   end,
  57.  
  58. }
  59.  
  60. local colorCon_mt = {
  61.   __index = colorCon,
  62. }
  63.  
  64. function new(x, y, w, h, c)
  65.   local l = {
  66.     x = x or 0,
  67.     y = y or 0,
  68.     w = w or 0,
  69.     h = h or 0,
  70.     t = {},
  71.     tC = {},
  72.     backC = c,
  73.     msgs = {}
  74.   }
  75.   setmetatable(l, colorCon_mt)
  76.   return l
  77. end
Advertisement
Add Comment
Please, Sign In to add comment