Advertisement
billysback

WaterRec

Sep 14th, 2013
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.51 KB | None | 0 0
  1.  
  2.  
  3. local args = {...}
  4.  
  5. local recording
  6.  
  7. local startTime = 0
  8.  
  9. local cursorX, cursorY
  10.  
  11. local logBuffer = ''
  12. local logThreshold = 8192
  13.  
  14. local outFilename = args[1]
  15.  
  16. assert(outFilename, 'Usage: ' .. shell.getRunningProgram() .. ' <output filename> <start paused>\nF11 to start recording and F12 to stop.')
  17.  
  18. local function startRecording(outFilename)
  19. local fh = io.open(outFilename, 'w')
  20.  
  21. local x, y = term.getSize()
  22. fh:write('0 - termSize - ' .. x .. ', ' .. y .. '\n')
  23.  
  24. fh:close()
  25.  
  26. startTime = os.clock()
  27.  
  28. recording = true
  29. end
  30.  
  31. local function stopRecording()
  32. term.restore()
  33.  
  34. local log = io.open(outFilename, 'a')
  35. log:write(logBuffer)
  36. log:close()
  37.  
  38. recording = false
  39.  
  40. term.write('Recording stopped.')
  41. end
  42.  
  43. if args[2] == 'true' then
  44. recording = false
  45.  
  46. else
  47. startRecording(outFilename)
  48. end
  49.  
  50. local screen = {}
  51. setmetatable(screen, {__index = term.native})
  52.  
  53. local function appendLog(msg)
  54. timestamp = os.clock() - startTime
  55.  
  56. if #logBuffer > logThreshold then
  57. local log = io.open(outFilename, 'a')
  58. log:write(logBuffer)
  59. log:close()
  60.  
  61. logBuffer = timestamp .. ' - ' .. msg .. '\n'
  62.  
  63. else
  64. logBuffer = logBuffer .. timestamp .. ' - ' .. msg .. '\n'
  65. end
  66. end
  67.  
  68. local function escapeString(s)
  69. local s = string.gsub(s, '[\n\v\t\\"\']', '\\%1')
  70.  
  71. return s
  72. end
  73.  
  74. screen.write = function(s)
  75. if not cursorX or not cursorY then
  76. cursorX, cursorY = term.getCursorPos()
  77. end
  78. if recording then
  79. local escaped = escapeString(s)
  80.  
  81. appendLog('write - ' .. cursorX .. ', ' .. cursorY .. ' - ' .. escaped)
  82. end
  83.  
  84. cursorX = cursorX + #s
  85.  
  86. return term.native.write(s)
  87. end
  88.  
  89. screen.scroll = function(n)
  90. if recording then
  91. appendLog('scroll - ' .. n)
  92. end
  93.  
  94. return term.native.scroll(n)
  95. end
  96.  
  97. screen.clearLine = function()
  98. if recording then
  99. appendLog('clearLine - ' .. cursorX .. ', ' .. cursorY)
  100. end
  101.  
  102. return term.native.clearLine()
  103. end
  104.  
  105. screen.setCursorPos = function(x, y)
  106. cursorX, cursorY = x, y
  107.  
  108. return term.native.setCursorPos(x, y)
  109. end
  110.  
  111. screen.setBackgroundColor = function(n)
  112. if recording then
  113. appendLog('setBackgroundColor - ' .. n)
  114. end
  115.  
  116. return term.native.setBackgroundColor(n)
  117. end
  118.  
  119. screen.setTextColor = function(n)
  120. if recording then
  121. appendLog('setTextColor - ' .. n)
  122. end
  123.  
  124. return term.native.setTextColor(n)
  125. end
  126.  
  127. screen.setBackgroundColour = function(n)
  128. if recording then
  129. appendLog('setBackgroundColor - ' .. n)
  130. end
  131.  
  132. return term.native.setBackgroundColor(n)
  133. end
  134.  
  135. screen.setTextColour = function(n)
  136. if recording then
  137. appendLog('setTextColor - ' .. n)
  138. end
  139.  
  140. return term.native.setTextColor(n)
  141. end
  142.  
  143. screen.clear = function()
  144. if recording then
  145. appendLog('clear', log)
  146. end
  147.  
  148. return term.native.clear()
  149. end
  150.  
  151. local co = coroutine.create(function()
  152. term.clear()
  153. term.setCursorPos(1, 1)
  154.  
  155. shell.run('shell')
  156. end)
  157.  
  158. local function resume(...)
  159. local ok, args = coroutine.resume(co, ...)
  160.  
  161. if not ok then
  162. error(args)
  163. end
  164.  
  165. return args
  166. end
  167.  
  168. term.redirect(screen)
  169.  
  170. local filter = resume()
  171.  
  172. while coroutine.status(co) ~= 'dead' do
  173. local event = {os.pullEventRaw()}
  174.  
  175. resume(unpack(event))
  176.  
  177. if event[1] == 'key' then
  178. -- Add pause/resume?
  179.  
  180. if event[2] == 87 and not recording then
  181. -- F11 - Start Recording
  182.  
  183. startRecording(outFilename)
  184.  
  185. elseif event[2] == 88 and recording then
  186. -- F12 - Stop Recording
  187.  
  188. stopRecording()
  189. end
  190. end
  191. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement