KaoSDlanor

logger mk2

Jan 25th, 2013
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.92 KB | None | 0 0
  1. local function overwrite(sName,replacement)
  2.     rawset(getfenv(),sName,replacement)
  3.     rawset(_G,sName,replacement)
  4.     local cur=0
  5.     repeat
  6.         cur=cur+1
  7.         local ok,t=pcall(function() return getfenv(cur) end)
  8.         if ok and type(t)=="table" and type(rawget(t,sName))=="table" then
  9.             rawset(t,sName,replacement)
  10.         end
  11.     until not ok
  12. end
  13.  
  14. local oldTerm=term
  15. local term=setmetatable({},{__index=oldTerm})
  16. term.old=oldTerm
  17. local tCharacterRecord={}
  18. local tTextColorRecord={}
  19. local tBackgroundColorRecord={}
  20.  
  21. local tColorShortHand={w=colors.white,o=colors.orange,m=colors.magenta,B=colors.lightBlue,y=colors.yellow,l=colors.lime,P=colors.pink,g=colors.gray,G=colors.lightGray,c=colors.cyan,p=colors.purple,b=colors.blue,z=colors.green,r=colors.red,a=colors.black}
  22.  
  23. local iTextColor=colors.white
  24. term.setTextColor=function(iColor)
  25.     iTextColor=iColor
  26.     return oldTerm.setTextColor(iColor)
  27. end
  28. term.setTextColour=function(iColor)
  29.     iTextColor=iColor
  30.     return oldTerm.setTextColour(iColor)
  31. end
  32.  
  33. term.getTextColor=function()
  34.     return iTextColor
  35. end
  36.  
  37. local iBackgroundColor=colors.black
  38. term.setBackgroundColor=function(iColor)
  39.     iBackgroundColor=iColor
  40.     return oldTerm.setBackgroundColor(iColor)
  41. end
  42. term.setBackgroundColor=function(iColor)
  43.     iBackgroundColor=iColor
  44.     return oldTerm.setBackgroundColor(iColor)
  45. end
  46.  
  47. term.getBackgroundColor=function()
  48.     return iBackgroundColor
  49. end
  50.  
  51. term.write=function(sText)
  52.     local CursorPosition={term.getCursorPos()}
  53.     local TextColorShorthand="w"
  54.     local backgroundColorShorthand="a"
  55.     for k,v in pairs(tColorShortHand) do
  56.         if v==term.getTextColor() then TextColorShorthand=k end
  57.         if v==term.getBackgroundColor() then backgroundColorShorthand=k end
  58.     end
  59.     local function replaceLine(tRecord,sAddText)
  60.         tRecord[CursorPosition[2]+1]=string.sub(tRecord[CursorPosition[2]+1] or "",1,CursorPosition[1]-1)..(CursorPosition[1]>string.len(tRecord[CursorPosition[2]+1] or "") and string.rep(" ",CursorPosition[1]-string.len(tRecord[CursorPosition[2]+1] or "")-1) or "")..sAddText..string.sub(tRecord[CursorPosition[2]+1] or "",CursorPosition[1]+#sAddText)
  61.     end
  62.  
  63.     replaceLine(tCharacterRecord,sText)
  64.     replaceLine(tTextColorRecord,string.rep(TextColorShorthand,#sText))
  65.     replaceLine(tBackgroundColorRecord,string.rep(backgroundColorShorthand,#sText))
  66.    
  67.     for k,v in ipairs(tCharacterRecord) do
  68.         if #v>CursorPosition[1] then
  69.             tCharacterRecord[k+1]=string.sub(v,CursorPosition[1]+1)..string.sub(tCharacterRecord[k-1],#v+1)
  70.         end
  71.     end
  72.     return oldTerm.write(sText)
  73. end
  74.  
  75. term.getTextAt=function(x,y,iLength)
  76.     if tCharacterRecord[y+1] then
  77.         return string.sub(tCharacterRecord[y+1],x,x+(iLength or 1)-1),tColorShortHand[string.sub(tTextColorRecord[y+1],x,x)],tColorShortHand[string.sub(tBackgroundColorRecord[y+1],x,x)]
  78.     else
  79.         return nil
  80.     end
  81. end
  82.  
  83. term.screenShot=function()
  84.     local tNew={{},{},{}}
  85.     for k,v in pairs(tCharacterRecord) do
  86.         tNew[1][k]=v
  87.         tNew[2][k]=tTextColorRecord[k]
  88.         tNew[3][k]=tBackgroundColorRecord[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(tColorShortHand[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(tColorShortHand[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. term.clear=function()
  113.     tCharacterRecord={}
  114.     tTextColorRecord={}
  115.     tBackgroundColorRecord={}
  116.     return oldTerm.clear()
  117. end
  118.  
  119. term.scroll=function(num)
  120.     for k,v in pairs(tCharacterRecord) do
  121.         if type(k)=="number" and type(num)=="number" and k-num>1 then
  122.             tCharacterRecord[k-num]=v
  123.             tTextColorRecord[k-num]=tTextColorRecord[k]
  124.             tBackgroundColorRecord[k-num]=tBackgroundColorRecord[k]
  125.         end
  126.     end
  127.     return oldTerm.scroll(num)
  128. end
  129.  
  130. overwrite("term",term)
Advertisement
Add Comment
Please, Sign In to add comment