Advertisement
sanovskiy

CC debug API

Aug 8th, 2014
330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.13 KB | None | 0 0
  1. -- DEBUG api
  2. local indentChar = " "
  3. function dump(data)
  4.   localdmp = performDump(data)
  5.   termX,termY = term.getSize()
  6.   splitted = split(localdmp, "\n")
  7.   term.setTextColor(colors.white)
  8.   term.setBackgroundColor(colors.black)
  9.   for i=1,#splitted do
  10.     print(splitted[i])
  11.     if i%(termY-1)==0 and not(i==0) then
  12.         term.setTextColor(colors.white)
  13.         term.setBackgroundColor(colors.red)
  14.         term.write("Press any key for next page, \'c\' to interrupt")
  15.         term.setTextColor(colors.white)
  16.         term.setBackgroundColor(colors.black)
  17.         local event, keycode = os.pullEvent("key")
  18.         if keycode == 46 then
  19.             print("")
  20.             return
  21.         end
  22.         curX,curY = term.getCursorPos()
  23.         term.setCursorPos(1, curY)
  24.         term.write(string.rep(" ",termX-1))
  25.         term.setCursorPos(1, curY)
  26.     end
  27.   end
  28. end
  29.  
  30. function performDump(data,indent)
  31.   local indent = indent or 1
  32.   local datatype = type(data)
  33.   if datatype == "table" then
  34.       type_string = "table[" .. (#data) .. "] ".. "\n"
  35.   elseif datatype == "string" then
  36.     return "(string[" .. tostring(string.len ( data )) .. "]) "..(string.sub(data,1,16)) .. "\n"
  37.   elseif datatype == "number" then
  38.     return "(number) " .. tostring(data) .. "\n"
  39.   elseif datatype == "boolean" then
  40.     return "(boolean) " .. (data and "true" or "false") .. "\n"
  41.   else
  42.     return datatype .. "\n"
  43.   end
  44.   local Buf = type_string;
  45.   indent = indent+1
  46.   for k, v in pairs(data) do
  47.     Buf = Buf .. string.rep(indentChar,indent) .. k .. " => " .. performDump ( v, indent )
  48.   end
  49.   return Buf .. "\n"
  50. end
  51.  
  52. function split(pString, pPattern)
  53.    local Table = {}  -- NOTE: use {n = 0} in Lua-5.0
  54.    local fpat = "(.-)" .. pPattern
  55.    local last_end = 1
  56.    local s, e, cap = pString:find(fpat, 1)
  57.    while s do
  58.           if s ~= 1 or cap ~= "" then
  59.          table.insert(Table,cap)
  60.           end
  61.           last_end = e+1
  62.           s, e, cap = pString:find(fpat, last_end)
  63.    end
  64.    if last_end <= #pString then
  65.           cap = pString:sub(last_end)
  66.           table.insert(Table, cap)
  67.    end
  68.    return Table
  69. end
  70.  
  71. params = {...}
  72. if params[1]~=nil then
  73.   print("Debugging "..params[1])
  74.   tmp = peripheral.wrap(params[1])
  75.   dump(tmp)
  76. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement