Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- args = { ... }
- term.setTextColour( colours.blue )
- runType = ( args[1] == "run" or args[1] == "log" or args[1] == "data" or args[1] == "clear" ) and args[1] or print( "Usage: \"run [run type] [program name]\"" ) and error()
- name = (args[2] or fs.exists( args[2] )) and args[2] or print( "Usage: \"run [run type] [program name]\"" ) and error()
- table.remove( args, 1 )
- table.remove( args, 1 )
- log = { }
- log.data = { }
- log.savedData = log.data
- log.saveLogExcludeTypes = { }
- function log.add( type, ... )
- for i = 1,#log.saveLogExcludeTypes do
- if type == log.saveLogExcludeTypes[i] then return false end
- end
- if type == nil then
- type = "Undefined"
- end
- arg = { ... }
- table.insert( log.data, { type = type, unpack( arg ) } )
- return true
- end
- function log.save( tSave )
- log.add( "System", "Saving Event Log" )
- if #log.data > 1 then
- table.insert( tSave, "--[[[Program Log] = Welcome to the program log")
- for i = 1, #log.data do
- log.toWrite = true
- for m = 1,#log.saveLogExcludeTypes do
- if log.data[i]["type"] == log.saveLogExcludeTypes[m] then
- log.toWrite = false
- end
- end
- if log.toWrite then
- local str = ""
- for k = 1,#log.data[i] do
- str = str..tostring( log.data[i][k] ).." "
- end
- table.insert( tSave, tostring( log.data[i]["type"] ).." - "..str )
- end
- end
- table.insert( tSave, "]]" )
- end
- return tSave
- end
- function string.split(str,pat)
- local t = {}
- local fpat = "(.-)" .. pat
- local last_end = 1
- local s, e, cap = str:find(fpat, 1)
- while s do
- if s ~= 1 or cap ~= "" then
- table.insert(t,cap)
- end
- last_end = e+1
- s, e, cap = str:find(fpat, last_end)
- end
- if last_end <= #str then
- cap = str:sub(last_end)
- table.insert(t, cap)
- end
- return t
- end
- function try( strName, name, ... )
- local args = { ... }
- log.add( "Trying", strName, "with args:", unpack ( args ) )
- err = { pcall( name, unpack( args ) ) }
- ok = err[1]
- if not ok then
- log.add( "Error", "with function:", strName, "with args:", unpack( args ) )
- return false
- end
- return unpack( err )
- end
- defaultProperties = { true, "", true, { } }
- programProperties = { [1] = "", [2] = "", [3] = "", [4] = "" }
- function getProperties( tContent )
- for i = 1,#programProperties do
- programProperties[i] = defaultProperties[i]
- end
- for i = 1,#tContent do
- if tContent[i] == "--[[[Properties] = File properties" then
- local z = 1
- while tContent[z+i] ~= "]]" do
- m = string.split( tContent[z+i], "=" )
- if m[2] ~= nil then
- if z == 1 then
- local m = m[2]
- if m == "false" then
- programProperties[1] = false
- else
- programProperties[1] = true
- end
- elseif z == 2 then
- log.saveLogExcludeTypes = string.split( m[2], "," )
- programProperties[3] = log.saveLogExcludeTypes
- elseif z == 3 then
- local m = m[2]
- if m == "false" then
- programProperties[3] = false
- else
- programProperties[3] = true
- end
- end
- end
- z = z+1
- end
- break
- end
- end
- end
- function insertInformation( tFile )
- if tFile[1] ~= "--"..name.." was run" then
- table.insert( tFile, 1, "--Code:" )
- table.insert( tFile, 1, "]]" )
- table.insert( tFile, 1, "Save running data="..tostring( programProperties[3] ) )
- table.insert( tFile, 1, "Exclude From Save Log="..tostring( programProperties[2] ) )
- table.insert( tFile, 1, "Save Log="..tostring( programProperties[1] ) )
- table.insert( tFile, 1, "--[[[Properties] = File properties" )
- end
- if tFile[1] ~= "--"..name.." was run" then
- table.insert( tFile, 1, "]]" )
- table.insert( tFile, 1, "times run = 1" )
- table.insert( tFile, 1, "runtime = "..tostring( run.d ).." days, "..tostring( run.h ).." hours, "..tostring( run.m ).." minutes, "..tostring( run.s ).." seconds" )
- table.insert( tFile, 1, "--[[[Info] = Information gathered while program was running" )
- table.insert( tFile, 1, "--"..name.." was run")
- else
- if programProperties[3] then
- tFile[3] = "runtime = "..tostring( run.d ).." days, "..tostring( run.h ).." hours, "..tostring( run.m ).." minutes, "..tostring( run.s ).." seconds"
- tFile[4] = "times run = "..( type( tonumber( string.split( tFile[4], " = " )[2]) ) == "number" and tostring( tonumber( string.split( tFile[4], " = " )[2])+1 ) or "1" )
- else
- tFile[3] = "runtime = enable in properties"
- tFile[4] = "times run = enable in properties"
- end
- end
- oldTFile = tFile
- tFile = {}
- for i = 1,#oldTFile do
- if oldTFile[i] ~= "--[[[Program Log] = Welcome to the program log" then
- table.insert( tFile, oldTFile[i] )
- else
- break
- end
- end
- if programProperties[1] == true then
- tFile = log.save( tFile )
- end
- k = fs.open( name, "w" )
- for i = 1,#tFile do
- k.writeLine( tFile[i] )
- end
- k.close( )
- return true
- end
- function timer( )
- local s = 0
- local m = 0
- local h = 0
- local d = 0
- local ms = 0
- while true do
- ms = ms+1
- if ms >= 10 then
- ms = 0
- s = s+1
- end
- if s >= 60 then
- s = 0
- m = m+1
- end
- if m >= 60 then
- m = 0
- h = h+1
- end
- if h >= 24 then
- h = 0
- d = d+1
- end
- run.s = s
- run.m = m
- run.h = h
- run.d = d
- sleep( 0.1 )
- end
- end
- function event( )
- while true do
- ev = { os.pullEvent( "mouse_click" ) }
- if ev[2] == 2 and ev[3] == 51 and ev[4] == 19 then
- draw = true
- elseif ev[2] == 1 and ev[3] == 51 and ev[4] == 18 and draw == true then
- os.queueEvent( "terminate" )
- elseif draw == true then
- draw = false
- end
- end
- end
- oldTerm = { }
- oldTerm.write = _G["term"]["write"]
- oldTerm.setCursorPos = _G["term"]["setCursorPos"]
- oldTerm.clear = _G["term"]["clear"]
- oldTerm.setBackgroundColour = _G["term"]["setBackgroundColour"]
- oldTerm.setTextColour = _G["term"]["setTextColour"]
- log.add( "System", "rewriting term library" )
- function term.write( ... )
- local arg = { ... }
- x, y = term.getCursorPos( )
- log.add( "Screen", "wrote", unpack( arg ), "at", x, y )
- oldTerm.write( unpack( arg ) )
- oldTerm.setCursorPos( 51, 19 )
- oldTerm.write( "^" )
- if draw then
- oldTerm.setCursorPos( 50, 18 )
- oldTerm.write( "_X" )
- end
- end
- function term.clear( )
- log.add( "Screen", "cleared")
- oldTerm.clear( )
- end
- function term.setCursorPos( x, y )
- log.add( "Screen", "Set cursor pos to", x, y )
- oldTerm.setCursorPos( x, y )
- end
- run = { }
- draw = false
- runProg = function( )
- try( "shell.run", shell.run, name, unpack( args ) )
- end
- k = fs.open( name, "r" )
- t = { }
- m = k.readLine( )
- while m do
- table.insert( t, m )
- m = k.readLine( )
- end
- k.close()
- if runType == "run" then
- getProperties( t )
- log.add( "System", "Running program" )
- parallel.waitForAny( runProg, timer, event )
- insertInformation( t )
- end
- term.write = oldTerm.write
- term.clear = oldTerm.clear
- term.setCursorPos = oldTerm.setCursorPos
- if runType == "log" then
- m = { }
- isInLog = false
- for i = 1,#t do
- if isInLog then
- table.insert( m, t[i] )
- end
- if t[i] == "--[[[Program Log] = Welcome to the program log" then
- isInLog = true
- end
- end
- table.remove( m, #m )
- term.clear( )
- term.setCursorPos( 1, 1 )
- y = 1
- for i = 1,#m do
- term.write( m[i] )
- y = y+1
- term.setCursorPos( 1, y )
- end
- end
- if runType == "clear" then
- oldTFile = t
- tFile = { }
- for i = 1,#oldTFile do
- if oldTFile[i] ~= "--[[[Program Log] = Welcome to the program log" then
- table.insert( tFile, oldTFile[i] )
- else
- break
- end
- end
- while tFile[1] ~= "--Code:" do
- table.remove( tFile, 1 )
- end
- table.remove( tFile, 1 )
- k = fs.open( name, "w" )
- for i = 1,#tFile do
- k.writeLine( tFile[i] )
- end
- k.close( )
- end
Advertisement
Add Comment
Please, Sign In to add comment