CoolisTheName007

ccdebug

Mar 22nd, 2013
593
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.25 KB | None | 0 0
  1. --debug
  2. local debug={}
  3. local error=_G._error or _G.error
  4.  
  5. local function wrap(text, limit) --from Kingdaroo
  6.         local lines = {''}
  7.         for word, space in text:gmatch('(%S+)(%s*)') do
  8.                 local temp = lines[#lines] .. word .. space:gsub('\n','')
  9.                 if #temp > limit then
  10.                         table.insert(lines, '')
  11.                 end
  12.                 if space:find('\n') then
  13.                         lines[#lines] = lines[#lines] .. word
  14.                        
  15.                         space = space:gsub('\n', function()
  16.                                 table.insert(lines, '')
  17.                                 return ''
  18.                         end)
  19.                 else
  20.                         lines[#lines] = lines[#lines] .. word .. space
  21.                 end
  22.         end
  23.         return lines
  24. end
  25.  
  26. function debug.step_print(s,_term)
  27.     local term=_term or term
  28.     local x,y=term.getSize()
  29.     y=y-1
  30.     local t=wrap(s,x)
  31.     local r=#t%y
  32.     local n=(#t-r)/y
  33.     for i=0,n-1 do
  34.         if i~=0 then write'\n' end
  35.         write(table.concat(t,'\n',i*y+1,(i+1)*y))
  36.         write('\nPress any key to continue')
  37.         os.pullEvent('key')
  38.     end
  39.     write('\n')
  40.     write(table.concat(t,'\n',n*y+1,n*y+r))
  41.     write('\n')
  42. end
  43.  
  44. function debug.printError( ... )
  45.     if term.isColour() then
  46.         term.setTextColour( colours.red )
  47.     end
  48.     local t={...}
  49.     for i=1,#t do t[i]=tostring(t[i]) end
  50.     debug.step_print(table.concat(t,''),term)
  51.     term.setTextColour( colours.white )
  52. end
  53.  
  54. --debug.traceback
  55. do
  56. --[[ Notes:
  57. getfenv(1)=getfenv()~=getfenv(0)~~_G
  58. error('',1)~=error('',0)
  59. ]]
  60. rawset(_G,'loadfile',function( _sFile )
  61.   local file = fs.open( _sFile, "r" )
  62.   if file then
  63.     local func, err = loadstring( file.readAll(),_sFile)
  64.     file.close()
  65.     return func, err
  66.   end
  67.   return nil, "File not found"
  68. end)
  69. local traceback=function(level,terr)
  70.   local terr=terr or {}
  71.   local passed={}
  72.   local err,ok=terr
  73.   local j=2+(level or 0)
  74.   repeat
  75.     j=j+1
  76.     passed[err]=true
  77.     ok,err=pcall(error,'',j)
  78.     if err:match('^[^:]+')=='bios' then break end
  79.     table.insert(terr,err)
  80.   until (passed[err])
  81.  
  82.   return terr
  83. end
  84.  
  85. local function classify(t,f)
  86.   local bins={}
  87.   local c
  88.   for i,v in ipairs(t) do
  89.     local fi,r=f(v)
  90.     if fi~=nil then
  91.       if c==nil or c.n~=fi then
  92.         table.insert(bins,c)
  93.         c={n=fi}
  94.       end
  95.       table.insert(c,r)
  96.     end
  97.   end
  98.   table.insert(bins,c)
  99.   return bins
  100. end
  101. local function get_chunks(terr)
  102.   return classify(terr,function(v) return v:match('^([^:]+):(.*)') end)
  103. end
  104.  
  105. local function get_lines(terr)
  106.   return classify(terr,function(v) return v:match('^(%d+):(.*)') end)
  107. end
  108.  
  109. local function put_lines(path,diameter,ts,l)
  110.   local ok,f=pcall(fs.open,path,'r')
  111.   if not ok or not f then table.insert(ts,'  (could not open file)')
  112.   else
  113.     for i=1,l-diameter-1 do
  114.       f.readLine()
  115.     end
  116.     for i=1,diameter do
  117.       table.insert(ts,'  '..f.readLine())
  118.     end
  119.     table.insert(ts,'->'..f.readLine())
  120.     for i=1,diameter do
  121.       local l=f.readLine()
  122.       if l then table.insert(ts,'  '..l)
  123.       else break end
  124.     end
  125.   end
  126. end
  127.  
  128. local function format_traceback(terr)
  129.   local ts={}
  130.   for i,v in ipairs(get_chunks(terr)) do
  131.     local name=v.n or '(no name)'
  132.     local path
  133.     if fs.exists(name) then
  134.       name,dir,path=fs.getName(name),name:match('^(.*)/') or '/',name
  135.       name=name..' ('..(dir or 'unknown')..')'
  136.     elseif false then
  137.       --shell has input string; modify to behave as file?
  138.     end
  139.     table.insert(ts,name)
  140.     for j,line in ipairs(get_lines(v)) do
  141.       table.insert(ts,' line '..(line.n or '(no line)')..', *'..(#line))
  142.       local n=line.n and tonumber(line.n) or 1
  143.       if path and n then
  144.         put_lines(path,1,ts,n)
  145.       end
  146.     end
  147.   end
  148.   return ts
  149. end
  150.  
  151. function debug.traceback(msg,level)
  152.   local level=level or 0
  153.   local ts=format_traceback(traceback(level+1,{}))
  154.   msg='['..(msg or 'debug.traceback')
  155.   table.insert(ts,1,msg)
  156.   return table.concat(ts,'\n')..'\n]'
  157. end
  158.  
  159. function debug.printTraceback(msg,level)
  160.     debug.printError(debug.traceback(msg,(level or 0)+1))
  161. end
  162.  
  163. end
  164.  
  165.  
  166. function debug.override()
  167.     rawset(_G,'_error',error)
  168.     rawset(_G,'error',debug.traceback)
  169.     rawset(_G,'printError',debug.printError)
  170. end
  171.  
  172.  
  173. return debug
Advertisement
Add Comment
Please, Sign In to add comment