Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local function max(tPar)
- local count
- for k,v in pairs(tPar) do
- if (tonumber(k) or 0)>(tonumber(count) or 0) then count=k end
- end
- return count
- end
- local messages=function()
- local timer
- local len
- local function clear()
- if len then
- local size={term.getSize()}
- local pos={term.getCursorPos()}
- term.setCursorPos(size[1]-len,1)
- write(string.rep(" ",len))
- len=nil
- timer=nil
- term.setCursorPos(unpack(pos))
- end
- end
- while true do
- local evts={os.pullEventRaw()}
- if evts[1]=="message" and type(evts[2]=="string") then
- clear()
- local size={term.getSize()}
- local pos={term.getCursorPos()}
- term.setCursorPos(size[1]-#evts[2],1)
- write(evts[2])
- term.setCursorPos(unpack(pos))
- timer=os.startTimer(2)
- len=#evts[2]
- elseif evts[1]=="timer" and timer==evts[2] then
- clear()
- end
- end
- end
- local tO={}
- local tCo={coroutine.create(function() os.run({},"rom/programs/shell") end),coroutine.create(messages)}
- local pE=printError
- local current=0
- local pE2=function(...)
- term.clear()
- term.setCursorPos(1,1)
- for k,v in pairs(tO) do
- rawset(k,"printError",pE)
- end
- for k,v in pairs(fs.list("rom/apis")) do
- if type(_G[v])=="table" and v~="term" then
- os.unloadAPI(v)
- os.loadAPI("rom/apis/"..v)
- end
- end
- tCo[3]=coroutine.create(rednet.run)
- local tEvts={}
- while true do
- local tDone={}
- for k,v in pairs(tCo) do
- current=k
- if coroutine.status(v)=="dead" then
- table.insert(tDone,k)
- os.queueEvent("message","coroutine "..k.." has died")
- else
- pcall(coroutine.resume(v,unpack(tEvts)))
- end
- end
- for k,v in pairs(tDone) do
- tCo[v]=nil
- end
- if #tCo==0 then
- break
- end
- os.startTimer(1)
- tEvts={os.pullEventRaw()}
- end
- print("all coroutines have been terminated, ending now")
- sleep(2)
- end
- for i=1,5 do
- if rawget(getfenv(i),"printError")~=nil and not tO[getfenv(i)] then
- tO[getfenv(i)]=true
- rawset(getfenv(i),"printError",pE2)
- end
- end
- rawset(_G,"addCo",function(func,ref)
- local t=type(func)
- if not t=="function" then
- print("incorrect param #1 function expected, got "..t)
- return nil, ("incorrect param #1 function expected, got "..t)
- end
- tCo[ref or max(tCo)+1]=coroutine.create(func)
- return ref or max(tCo)
- end)
- rawset(_G,"remCo",function(num,b)
- num=num or 0
- if not tCo[num] then
- print("There is no coroutine under that reference")
- return nil
- elseif num==current then
- print("cannot remove the current coroutine")
- return nil
- end
- tCo[num]=nil
- if b then
- os.queueEvent("message","coroutine "..num.." has died")
- end
- return true
- end)
- os.queueEvent("terminate")
Advertisement
Add Comment
Please, Sign In to add comment