Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- Multishell Trystan Cannon
- 6 February 2013
- This is a clone of the multi-shell program
- posted in the programs section of ComputerCraft
- forums on the day of this file's creation.
- ]]--
- --========================================================
- -- Variables:
- local PROGRAM_PATH = "rom/programs/shell" -- The program to run an instance of.
- local NUM_INSTANCES = 10 -- The number of instances to run of PROGRAM_PATH.
- local instanceStack = {} -- The stack/table of instance objects. instaceStack[n] = { thread = coroutine, terminal = table }
- local currentInstance = 1 -- The index of the current instance object in the instance stack.
- --========================================================
- --========================================================
- -- Terminal functions:
- -- Creates and returns a new copy of the term.native function for redirection.
- local function createTerminal()
- local terminal = {
- buffer = {}
- }
- for index, item in pairs(term.native) do
- terminal[index] = function( ... )
- terminal.buffer[#terminal.buffer + 1] = {functionName = index, parameters = { ... }}
- return item( ... )
- end
- end
- terminal.flushBuffer = function()
- for index, item in pairs(terminal.buffer) do
- term.native[item.functionName](unpack(item.parameters))
- end
- end
- terminal.emptyBuffer = function()
- terminal.buffer = {}
- end
- terminal.clear = function()
- terminal.emptyBuffer()
- term.native.clear()
- end
- return terminal
- end
- --========================================================
- --========================================================
- -- Instance functions:
- -- Creates a new instance object using the global PROGRAM_PATH as the target program.
- local function createInstance()
- -- Create the new instance object by making a new thread for the program using coroutine.create() and a new terminal
- -- using createTerminal().
- local thread = coroutine.create(function()
- while true do
- local eventData = { os.pullEvent() }
- local hasFinished = coroutine.resume(os.run({}, PROGRAM_PATH), unpack( eventData ))
- if hasFinished then
- return "Finished."
- end
- end
- end)
- local instance = { thread = thread, terminal = createTerminal() }
- return instance
- end
- -- Fills the instance stack with desired instances of the desired program.
- local function initInstanceStack()
- for instanceNum = 1, NUM_INSTANCES do
- instanceStack[instanceNum] = createInstance()
- end
- end
- --========================================================
- --========================================================
- -- Main entry point:
- initInstanceStack()
- term.redirect(instanceStack[currentInstance].terminal)
- while true do
- local eventData = { os.pullEvent() }
- -- Handle switching instances before anything else.
- if eventData[1] == "key" and (eventData[2] == keys["leftCtrl"] or eventData[2] == keys["rightCtrl"]) then
- -- Start a timer to make sure we don't discard an event to an instance.
- local keyTimer = os.startTimer(0.8)
- local hotKeyEvent, instanceNumber = os.pullEvent()
- -- Swap instances if that is what the user wants to do.
- if hotKeyEvent == "key" and instanceNumber - 1 >= 1 and instanceNumber - 1 <= 10 then
- currentInstance = instanceNumber - 1
- -- Update the current instance.
- term.redirect(instanceStack[currentInstance].terminal)
- term.native.clear()
- term.native.setCursorPos(1, 1)
- instanceStack[currentInstance].terminal.flushBuffer()
- -- Nullify the event for this iteration.
- eventData = {}
- end
- end
- -- Clean up any dead instances.
- for index, instance in pairs(instanceStack) do
- if coroutine.status(instance.thread) == "dead" then
- instanceStack[index] = nil
- end
- end
- -- Update the current thread with this event.
- coroutine.resume(instanceStack[currentInstance].thread, unpack( eventData ))
- end
- -- If the loop breaks, then restore the terminal.
- term.redirect(term.native)
- --========================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement