Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[ realtime program editor ]]--
- --[[
- set myFile equal to the file you want to run and rerun whenever possible.
- leave it blank for it to pick a file on its own.
- set myDrive equal to the ID of the drive you want run from.
- leave it blank for it to pick a drive on its own
- Note: Can pick the wrong drive if more than one exists :)
- ]]
- local myFile = "testFile.lua"
- local myDrive = ""
- -- [[ Code Do Not Touch ]] --
- local thread = {
- threads = {},
- watcherThread = nil,
- shouldRun = true
- }
- local fs = filesystem
- -- init filesystem
- if fs.initFileSystem("/dev") == false then
- computer.panic("Cannot initialize /dev!")
- end
- -- find drive if one isn't stated
- if (myDrive:len() == 0) then
- for _,f in pairs(fs.childs("/dev")) do
- if f ~= "serial" then
- myDrive = f
- break
- end
- end
- end
- -- mount drive
- if (myDrive:len() > 0) then
- local ch = fs.mount("/dev/"..myDrive, "/")
- if (not ch) then
- computer.panic(myDrive .. " could not be found!")
- end
- else
- computer.panic("No HardDrive Found!")
- end
- -- find a file if one isn't stated
- if (myFile:len() == 0) then
- for _,f in pairs(fs.childs("/")) do
- if fs.isFile(f) then
- myFile = f
- break
- end
- end
- end
- local function sleep(sec)
- sec = sec or 0.0
- local curTime = computer.millis()
- local wantedTime = curTime + (sec*1000)
- while computer.millis() < wantedTime do
- computer.skip()
- fs.exists("/")
- end
- end
- thread.create = function(func, isWatcher)
- if type(func) == 'function' then
- local index = tostring(func)
- if thread.threads[index] == nil then
- local t = {}
- t.co = coroutine.create(func)
- function t:stop()
- if thread.threads[index] ~= nil then
- thread.threads[index] = nil
- end
- end
- thread.threads[index] = t
- return t
- end
- if isWatcher then
- thread.watcherThread = t
- end
- end
- return false
- end
- local function doFileThread()
- local status, err = pcall(function()
- fs.doFile(myFile)
- end)
- if err ~= nil then
- print(err)
- end
- end
- local function resumeThreads()
- for i,v in pairs(thread.threads) do
- computer.skip()
- if (v.co ~= nil) then
- coroutine.resume(true,v.co)
- end
- end
- end
- local function CreateFileThread(v)
- print("creating new thread")
- v:stop()
- sleep(0.1)
- thread.create(doFileThread)
- end
- --TODO: override default event.pull() method
- thread.watcher = function()
- local file = fs.open(myFile,"r")
- local size = file:seek("end")
- local newSize = size
- file:close()
- while thread.shouldRun do
- sleep(5)
- --file = fs.open(myFile,"r")
- --local newSize = file:seek("end")
- --file:close()
- for i, v in pairs(thread.threads) do
- sleep(0.1)
- if (v.co == nil or coroutine.status(v.co) == "dead") then
- thread.shouldRun = false
- break
- end
- if (newSize ~= size and v ~= thread.watcherThread) then
- size = newSize
- CreateFileThread(v)
- break
- end
- end
- end
- end
- thread.create(doFileThread)
- thread.create(thread.watcher)
- while (coroutine.status(thread.watcherThread) ~= "dead") do
- resumeThreads()
- sleep(1)
- end
Add Comment
Please, Sign In to add comment