KaoSDlanor

Screen logger

Oct 22nd, 2012
309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.85 KB | None | 0 0
  1. local oldt=term
  2. local term=setmetatable({},{__index=oldt})
  3. term.old=oldt
  4. local record={}
  5. local tcols={}
  6. local tback={}
  7.  
  8. local tsym={w=1,o=2,m=4,B=8,y=16,l=32,P=64,g=128,G=256,c=512,p=1024,b=2048,z=4096,r=8192,R=16384,a=32768}
  9.  
  10. local tC=colors.white
  11. term.setTextColor=function(col)
  12.     tC=col
  13.     return oldt.setTextColor(col)
  14. end
  15. term.setTextColour=function(col)
  16.     tC=col
  17.     return oldt.setTextColour(col)
  18. end
  19.  
  20. term.getTextColor=function()
  21.     return tC
  22. end
  23.  
  24. local bC=colors.black
  25. term.setBackgroundColor=function(col)
  26.     bC=col
  27.     return oldt.setBackgroundColor(col)
  28. end
  29. term.setBackgroundColour=function(col)
  30.     bC=col
  31.     return oldt.setBackgroundColour(col)
  32. end
  33.  
  34. term.getBackgroundColor=function()
  35.     return bC
  36. end
  37.  
  38. term.write=function(tex)
  39.     local pos={term.getCursorPos()}
  40.     local size={term.getSize()}
  41.     local tS="w"
  42.     local bS="a"
  43.     for k,v in pairs(tsym) do
  44.         if v==term.getTextColor() then tS=k end
  45.         if v==term.getBackgroundColor() then bS=k end
  46.     end
  47.     local function rep(t,txt)
  48.         t[pos[2]+1]=string.sub(t[pos[2]+1] or "",1,pos[1]-1)..(pos[1]>string.len(t[pos[2]+1] or "") and string.rep(" ",pos[1]-string.len(t[pos[2]+1] or "")-1) or "")..txt..string.sub(t[pos[2]+1] or "",pos[1]+#txt)
  49.     end
  50.  
  51.     rep(record,tex)
  52.     rep(tcols,string.rep(tS,#tex))
  53.     rep(tback,string.rep(bS,#tex))
  54.    
  55.     for k,v in ipairs(record) do
  56.         if #v>pos[1] then
  57.             record[k+1]=string.sub(v,pos[1]+1)..string.sub(record[k-1],#v+1)
  58.         end
  59.     end
  60.     return oldt.write(tex)
  61. end
  62.  
  63. term.getTextAt=function(x,y,len)
  64.     if record[y+1] then
  65.         return string.sub(record[y+1],x,x+(len or 1)-1),tsym[string.sub(tcols[y+1],x,x)],tsym[string.sub(tback[y+1],x,x)]
  66.     else
  67.         return nil
  68.     end
  69. end
  70.  
  71. term.scroll=function(num)
  72.     local tnums={}
  73.     for k,v in pairs(record) do if k>1 then table.insert(tnums,k) end end
  74.     table.sort(tnums)
  75.     for i=1,#tnums do
  76.         record[tnums[i]]=record[tnums[i]+num]
  77.         tcols[tnums[i]]=tcols[tnums[i]+num]
  78.         tback[tnums[i]]=tback[tnums[i]+num]
  79.     end
  80.     return oldt.scroll(num)
  81. end
  82.  
  83. term.screenShot=function()
  84.     local tNew={{},{},{}}
  85.     for k,v in pairs(record) do
  86.         tNew[1][k]=v
  87.         tNew[2][k]=tcols[k]
  88.         tNew[3][k]=tback[k]
  89.     end
  90.     return unpack(tNew)
  91. end
  92.  
  93. term.loadScreen=function(t,t2,t3)
  94.     for k,v in pairs(t) do
  95.         term.setCursorPos(1,k)
  96.         for i=1,#v do
  97.             if type(t2)=="table" then
  98.                 term.setTextColor(tsym[string.sub(t2[k],i,i)])
  99.             elseif type(t2)=="number" and i==1 then
  100.                 term.setTextColor(t2)
  101.             end
  102.             if type(t3)=="table" then
  103.                 term.setBackgroundColor(tsym[string.sub(t3[k],i,i)])
  104.             elseif type(t3)=="number" and i==1 then
  105.                 term.setTextColor(t3)
  106.             end
  107.             write(string.sub(v,i,i))
  108.         end
  109.     end
  110. end
  111.  
  112. rawset(getfenv(),"term",term)
  113. rawset(_G,"term",term)
  114. local cur=0
  115. repeat
  116.     cur=cur+1
  117.     local ok,t=pcall(function() return getfenv(cur) end)
  118.     if ok and type(t)=="table" and type(rawget(t,"term"))=="table" then
  119.         rawset(t,"term",term)
  120.     end
  121. until not ok
Advertisement
Add Comment
Please, Sign In to add comment