Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local args = {...}
- local recording
- local startTime = 0
- local cursorX, cursorY
- local logBuffer = ''
- local logThreshold = 8192
- local outFilename = args[1]
- assert(outFilename, 'Usage: ' .. shell.getRunningProgram() .. ' <output filename> <start paused>\nF11 to start recording and F12 to stop.')
- local function startRecording(outFilename)
- local fh = io.open(outFilename, 'w')
- local x, y = term.getSize()
- fh:write('0 - termSize - ' .. x .. ', ' .. y .. '\n')
- fh:close()
- startTime = os.clock()
- recording = true
- end
- local function stopRecording()
- term.restore()
- local log = io.open(outFilename, 'a')
- log:write(logBuffer)
- log:close()
- recording = false
- term.write('Recording stopped.')
- end
- if args[2] == 'true' then
- recording = false
- else
- startRecording(outFilename)
- end
- local screen = {}
- setmetatable(screen, {__index = term.native})
- local function appendLog(msg)
- timestamp = os.clock() - startTime
- if #logBuffer > logThreshold then
- local log = io.open(outFilename, 'a')
- log:write(logBuffer)
- log:close()
- logBuffer = timestamp .. ' - ' .. msg .. '\n'
- else
- logBuffer = logBuffer .. timestamp .. ' - ' .. msg .. '\n'
- end
- end
- local function escapeString(s)
- local s = string.gsub(s, '[\n\v\t\\"\']', '\\%1')
- return s
- end
- screen.write = function(s)
- if not cursorX or not cursorY then
- cursorX, cursorY = term.getCursorPos()
- end
- if recording then
- local escaped = escapeString(s)
- appendLog('write - ' .. cursorX .. ', ' .. cursorY .. ' - ' .. escaped)
- end
- cursorX = cursorX + #s
- return term.native.write(s)
- end
- screen.scroll = function(n)
- if recording then
- appendLog('scroll - ' .. n)
- end
- return term.native.scroll(n)
- end
- screen.clearLine = function()
- if recording then
- appendLog('clearLine - ' .. cursorX .. ', ' .. cursorY)
- end
- return term.native.clearLine()
- end
- screen.setCursorPos = function(x, y)
- cursorX, cursorY = x, y
- return term.native.setCursorPos(x, y)
- end
- screen.setBackgroundColor = function(n)
- if recording then
- appendLog('setBackgroundColor - ' .. n)
- end
- return term.native.setBackgroundColor(n)
- end
- screen.setTextColor = function(n)
- if recording then
- appendLog('setTextColor - ' .. n)
- end
- return term.native.setTextColor(n)
- end
- screen.setBackgroundColour = function(n)
- if recording then
- appendLog('setBackgroundColor - ' .. n)
- end
- return term.native.setBackgroundColor(n)
- end
- screen.setTextColour = function(n)
- if recording then
- appendLog('setTextColor - ' .. n)
- end
- return term.native.setTextColor(n)
- end
- screen.clear = function()
- if recording then
- appendLog('clear', log)
- end
- return term.native.clear()
- end
- local co = coroutine.create(function()
- term.clear()
- term.setCursorPos(1, 1)
- shell.run('shell')
- end)
- local function resume(...)
- local ok, args = coroutine.resume(co, ...)
- if not ok then
- error(args)
- end
- return args
- end
- term.redirect(screen)
- local filter = resume()
- while coroutine.status(co) ~= 'dead' do
- local event = {os.pullEventRaw()}
- resume(unpack(event))
- if event[1] == 'key' then
- -- Add pause/resume?
- if event[2] == 87 and not recording then
- -- F11 - Start Recording
- startRecording(outFilename)
- elseif event[2] == 88 and recording then
- -- F12 - Stop Recording
- stopRecording()
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement