Posted 14 September 2012 - 11:03 AM
Hey Pro's, I am currently trying to make a multitasking system where you can call multiple coroutines and run one at a time, essentially minimizing and switching between windows. I know there is an existing utility for this but I need to make my own so I can adapt it to work with my OS… at the moment it does not work and I am not sure why. you can create a new window but after that you cannot switch between them or open another window. please take a look
http://pastebin.com/YmznkxvM
http://pastebin.com/YmznkxvM
Spoiler
local function record(existing)
local funcs={}
local temp=existing or {}
for k,v in pairs(term.native) do
funcs[k]=function(...) table.insert(temp,{v,[2]={...}}) return v(...) end
funcs[k..'2']=v
end
term.redirect(funcs)
return temp
end
local function assign(record)
funcs={}
for k,v in pairs(term.native) do
funcs[k]=function(...) table.insert(tThreads[record][2],{v,[2]={...}}) return v(...) end
end
term.redirect(funcs)
end
local function replay(record)
for k,v in pairs(tThreads[record][2]) do
v[1](unpack(v[2]))
sleep(0)
end
end
local function manage()
local running=1
while true do
local events={os.pullEvent()}
if events[1]=='key' and events[2]==15 then
term.native.clear()
term.native.setCursorPos(1,1)
tRunning['current']=coroutine.create(function() shell.run('shell') end)
table.insert(tThreads,{tRunning['current'],{{term.native.clear,{}},{term.native.setCursorPos,{1,1}}}})
elseif events[1]=='char' and events[2]=='>' and running~=#tThreads then
running=running+1
tRunning['current']=tThreads[running]
replay(running)
assign(running)
elseif events[1]=='char' and events[2]=='<' and running~=1 then
running=running-1
tRunning['current']=tThreads[running]
replay(running)
assign(running)
end
end
end
local tRunning={}
tRunning['main']=coroutine.create(function() shell.run('shell') end)
tRunning['back']=coroutine.create(manage)
local tThreads={{tRunning['current'],{{term.native.clear,{}},{term.native.setCursorPos,{1,1}}}}}
while true do
local events={os.pullEvent()}
for k,v in pairs(tRunning) do
coroutine.resume(v,unpack(events))
end
end