dezindzer

tick profiler example

Jul 31st, 2015
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.75 KB | None | 0 0
  1. local monitorSide = "left"
  2.  
  3. if peripheral.isPresent(monitorSide) and peripheral.getType(monitorSide) == "monitor" then
  4.   term.redirect(peripheral.wrap(monitorSide))
  5. else
  6.   print("No monitor found")
  7.   return
  8. end
  9.  
  10. function explode(inSplitPattern, str)
  11.   str = str .. ""
  12.   local outResults = { }
  13.   local theStart = 1
  14.   local theSplitStart, theSplitEnd = string.find( str, inSplitPattern, theStart )
  15.   while theSplitStart do
  16.   local sub = string.sub( str, theStart, theSplitStart-1 )
  17.     table.insert( outResults,  sub)
  18.     theStart = theSplitEnd + 1
  19.     theSplitStart, theSplitEnd = string.find( str, inSplitPattern, theStart )
  20.   end
  21.   table.insert( outResults, string.sub( str, theStart ) )
  22.   return outResults
  23. end
  24.  
  25. function printColouredBars(str, first)
  26.   parts = explode("|", str)
  27.   local l = #parts
  28.   for k = 1, l do
  29.     if first then
  30.       term.setTextColor(colors.blue)
  31.     end
  32.     io.write(parts[k])
  33.     if first then
  34.       term.setTextColor(colors.white)
  35.     end
  36.     if k ~= l then
  37.       term.setTextColor(colors.red)
  38.       io.write("|")
  39.       term.setTextColor(colors.white)
  40.     end
  41.   end
  42. end
  43.  
  44. function profile()
  45.   term.setCursorPos(1, 1)
  46.   local file = fs.open("profile.txt", "r")
  47.   local text = file.readAll()
  48.   file.close()
  49.   local tables = explode("\n\n", string.gsub(text, "\r\n", "\n"))
  50.   term.clear()
  51.   local i, j
  52.   for i = 1, #tables do
  53.     lines = explode("\n", tables[i] .. "")
  54.     if #lines == 1 then
  55.       term.setTextColor(colors.green)
  56.       print(lines[1])
  57.       term.setTextColor(colors.white)
  58.     else
  59.       for j = 1, #lines do
  60.         printColouredBars(lines[j] .. "\n", j == 1)
  61.       end
  62.       if i ~= #tables then
  63.         io.write("\n")
  64.       end
  65.     end
  66.   end
  67. end
  68.  
  69. while true do
  70.   profile()
  71.   sleep(60)
  72. end
  73.  
  74. term.restore()
Add Comment
Please, Sign In to add comment