This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
cntkillme's profile picture

Running 2 programs within the same shell on 2 different windows?

Started by cntkillme, 20 May 2017 - 09:44 AM
cntkillme #1
Posted 20 May 2017 - 11:44 AM
I'm trying to figure out how I can run 2 different programs within the same shell using 2 different windows.
So my first approach was to do:


local sX, sY = term.getSize()
local fileA, fileB = "blah", "blah blah"
local termA = window.create(term.current(), 1, 1, sX, sY/2)
local termB = window.create(term.current(), 1, sY/2+1, sX, sY/2)


parallel.waitForAny(
   function() os.run({ term = termA }, fileA) end,
   function() os.run({ term = termB }, fileB) end
)

However this fails to work unless all outputting I do is through term (for example when I call print). I then tried to wrap term.current and force it to return termA if the first script called it and termB if the second one did:


local envA = { term = termA }
local envB = { term = termB }
local _current = term.current
function term.current()
  local env = getfenv(2)
  if env == envA then
    return termA
  elseif env == envB then
    return termB
  else
   return _current()
  end
end

But that didn't work. Is there a way to do what I want? I don't know much about the CC API so if this is obvious I'm sorry :P/>.
cntkillme #2
Posted 20 May 2017 - 12:05 PM
I ended up wrapping the term table entirely, although I still don't like this method.


local _term = term
_G.term = setmetatable({ }, {
  __index = function(_, key)
	local env
	_, env = pcall(getfenv, 4) -- i dont know how reliable this is going to be
	if _ then
	  if env == envA then
		return termA[key]
	  elseif env == envB then
		return termB[key]
	  end
	end
	return _term[key]
  end})
Edited on 20 May 2017 - 10:12 AM
cntkillme #3
Posted 20 May 2017 - 12:26 PM
I guess I could probably use coroutines here, any time the first thread pauses I can redirect the terminal if I have to continue the other one (and vice versa).
cntkillme #4
Posted 21 May 2017 - 01:07 AM
Aha. This turned out cleaner than I expected. https://puu.sh/vWtqN/3d18573c86.mp4
Still have to handle certain things like the mouse_click events and such to make sure they only fire for windows they're in but that'll be a piece of cake.

Is there a list of all events somewhere on the wiki? found it

Hmm. I'm reinventing the wheel. There's probably a better "windows" API that handles this for me. Anyways:
https://puu.sh/vWx6J/80685016ed.mp4
Edited on 21 May 2017 - 12:22 AM
Bomb Bloke #5
Posted 21 May 2017 - 05:29 AM
What you're building seems very close to how multishell behaves; CC's window API was basically written to suit its purposes.

The main difference between what you're doing and what multishell does is that you're showing all your active applications at once, whereas multishell flips all windows but two to "invisible" (keeping one for the menu bar up the top, and one for the current foreground application below). You'll want to filter most events in the same way, though - the linked source gives some pointers as to which ones you'll want to account for.
cntkillme #6
Posted 21 May 2017 - 06:19 AM
So it'd probably have a good idea to have some sort of "active" window in the case to handle key events and such (perhaps flip around these windows via ctrl + arrow keys). Alright gonna check out multishell's source, thanks!

Heh, I'll just use lyqydos and not reinvent the wheel since it's already been done and better. Sure it's slightly different than what I want but it's far more flexible.
Edited on 21 May 2017 - 05:22 AM
cntkillme #7
Posted 21 May 2017 - 07:58 AM
Hmm this is gonna be painful, since the new versions of CC have package now it's tricking the packman script.
Lyqyd #8
Posted 21 May 2017 - 05:04 PM
Could you post that as a bug report either in the packman topic here on the forums or on the packman github repo? In the meantime, you could get it to work temporarily by using the lua console to set the API to nil before using packman: _G.package = nil
cntkillme #9
Posted 21 May 2017 - 10:48 PM
I just opened all relevant scripts in a text editor and just did a find-and-replace of package to pkmnpackage but yeah I've posted a bug report on the github repo.