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

Sync API - 1.63

Started by Terminator_NL, 31 January 2015 - 07:05 PM
Terminator_NL #1
Posted 31 January 2015 - 08:05 PM
Hello! This program is depreciated since a better code by SquidDev has much better results,
What it does: It shows on the monitors what terminal shows.
Easy for multiplayer servers with restricted access and at the same time play games and show how you play, ect. Really cool!
Here's the code, not by me. But by SquidDev


local redirect = {}
local current  = term.current()
local monitor = peripheral.wrap("right")
for k in pairs(current) do
  -- Get the function for both the current terminal and the monitor object
  local curFunc, monFunc = current[k], monitor[k]
  redirect[k] = function(...)
	-- Call both, but only return the value for the current one (so term.getSize/term.getCursorPos/term.getColor return for the main terminal)
	monFunc(...)
	return curFunc(...)
  end
end
term.redirect(redirect)

Please use his code instead and forget about my old code, If you do feel like you want my code for some reason. It's in the spoiler below.

SpoilerOrigional post:

This is a program which is capable of showing whatever you write in the terminal on the monitor AND terminal at the same time.
Simply run it, It changes some variables and after that do what you normally do with programs!
Please note that after running this program it's best to clear the terminal right after.

Steps:
  • Install program
  • Configure the variables at the top
  • Run the program, using your own program or as startup
  • Clear the terminal for better results
Detailed information about the variables:
Spoilermonitors = {"top"}
Place your own sides of the monitors here, for multiple monitors use: {"top","bottom","other sides"} ect

bOutlineColor = colors.gray
This is the background color.
Place your own color here to change the outline color (Showing where the terminal ends)

tOutlineColor = colors.black
This is the text color
Place your own color here if you want to add text or a symbol in the border.

drawRightBorder = true
Change to anything else to stop the righthandside border from placing.
Useful for dirty codes that write outside the terminal.

drawBottomBorder = true
Change to anything else to stop the bottom border line from placing.
It's recommended to leave this off, since no "reasonable" program will write anything here, it's dead space.

borderChar = " "
Change to anything you like! As of now it's just the background. but it could be a symbol, or even words!


Picture of program in action on default settings
Spoiler
Note: The program tends to stop running programs randomly. I will fix this issue as soon as I can.

You are free to research the issue on your own, and report it here ;)/>

Link: http://pastebin.com/juFpAwLM

Program:
Spoiler

monitors = {"top"}
bOutLineColor = colors.gray
tOutLineColor = colors.black
drawBottomBorder = true
drawRightBorder = true
borderChar = " "
function drawOutSide(boolyleen)
tXFF, tYFF = term.getSize()
for i=1,#monitors do
  local tempMon = peripheral.wrap(monitors[i])
  mXFF, mYFF = tempMon.getSize()
  mCXP, mCYP = tempMon.getCursorPos()
  if boolyleen == nil then
   tempMon.setTextColor(tOutLineColor)
   tempMon.setBackgroundColor(bOutLineColor)
  end
  local stringAdd = ""
  if drawBottomBorder == true then
   for i=1,mXFF do
	if boolyleen == nil then
	 stringAdd = stringAdd..borderChar
	else
	 stringAdd = stringAdd.." "
	end
   end
   for i=1,(mYFF-tYFF) do
	tempMon.setCursorPos(1,tYFF+i)
	tempMon.write(stringAdd)
   end
  end

  local stringAdd = ""
  if drawRightBorder == true and boolyleen == nil then
   for i=1,mXFF-tXFF do
	stringAdd = stringAdd..borderChar
   end
   for i=1,tYFF do
	tempMon.setCursorPos(tXFF+1,i)
	tempMon.write(stringAdd)
   end
  end
  tempMon.setTextColor(lastTextColorF)
  tempMon.setBackgroundColor(lastBackgroundColorF)
  tempMon.setCursorPos(mCXP,mCYP)
end
end
if not normalCursorBlink then
normalCursorBlink = term.setCursorBlink
end
term.setCursorBlink = function(input)
normalCursorBlink(input)
for i=1,#monitors do
  peripheral.call(monitors[i],"setCursorBlink",input)
end
return
end
if not normalBackgroundColor then
normalBackgroundColor = term.setBackgroundColour
end
term.setBackgroundColor = function(input)
normalBackgroundColor(input)
for i=1,#monitors do
  peripheral.call(monitors[i],"setBackgroundColor",input)
end
lastBackgroundColorF = input
return
end
term.setBackgroundColour = term.setBackgroundColor
if not normalWrite then
normalWrite = term.write
end
term.write = function(input)
normalWrite(input)
for i=1,#monitors do
  peripheral.call(monitors[i],"write",input)
end
drawOutSide()
return
end
if not normalsetCursorPos then
normalsetCursorPos = term.setCursorPos
end
term.setCursorPos = function(input1, input2)
normalsetCursorPos(input1,input2)
for i=1,#monitors do
  peripheral.call(monitors[i],"setCursorPos",input1,input2)
end
return
end
if not normalClear then
normalClear = term.clear
end
term.clear = function(input)
normalClear()
for i=1,#monitors do
  peripheral.call(monitors[i],"clear")
end
drawOutSide()
return
end
if not normalsetTextColor then
normalsetTextColor = term.setTextColour
end
term.setTextColour = function(input)
normalsetTextColor(input)
for i=1,#monitors do
  peripheral.call(monitors[i],"setTextColor",input)
end
lastTextColorF = input
return
end
term.setTextColor = term.setTextColour
if not normalClearLine then
normalClearLine = term.clearLine
end
term.clearLine = function()
normalClearLine()
for i=1,#monitors do
  peripheral.call(monitors[i],"clearLine")
end
drawOutSide()
return
end
if not normalScroll then
normalScroll = term.scroll
end
term.scroll = function(input)
normalScroll(input)
for i=1,#monitors do
  prevCursorPosXF, prevCursorPosYF = peripheral.call(monitors[i],"getCursorPos")
  if input > 0 then
   drawOutSide(false)
   peripheral.call(monitors[i],"scroll",input)
   else
   peripheral.call(monitors[i],"scroll",input)
  end
  peripheral.call(monitors[i],"setCursorPos",prevCursorPosXF,prevCursorPosYF)
end
drawOutSide()
return
end
Edited on 02 February 2015 - 06:06 PM
Creator #2
Posted 31 January 2015 - 09:30 PM
Interesting, but what would be the use?
Terminator_NL #3
Posted 31 January 2015 - 09:53 PM
This program can be used for multiplayer servers, where you are not allowed to access a computer, or for games. Games will be shown on terminal and a monitor so people can "Watch" the game while not having to look inside the computer. Useful for arcades, and even your own home.
MKlegoman357 #4
Posted 31 January 2015 - 10:12 PM
Nice! Not a bad first public program. The issue with your current setup is that you overwrite the term APIs functions. The term API has a 'redirect' function specifically made for custom terminal objects. Basically, you make a table with all the modified/custom term functions (not including 'redirect', 'current', but including all the functions you can find in term.native) and pass it to the term.redirect. In CC 1.6+ it is best to then run a program you'll want to be 'syncing' the screen with. Otherwise, it will just look like it's not working. You have to run the program inside your redirect script for it to work. Good luck! :)/>
Terminator_NL #5
Posted 31 January 2015 - 10:18 PM
Dear MKlegoman, Thank you for your reply, I have found that using term.redirect() only sends to one screen, and this program was intended to copy all terminal functions, override and send to multiple outputs. Reading your post makes me wonder. Is it possible to use term.redirect() on multiple outputs? if so, please let me know how because I'm very intrested :D/>
Edited on 31 January 2015 - 09:45 PM
SquidDev #6
Posted 01 February 2015 - 11:24 AM
I have found that using term.redirect() only sends to one screen, and this program was intended to copy all terminal functions, override and send to multiple outputs. Reading your post makes me wonder. Is it possible to use term.redirect() on multiple outputs? if so, please let me know how because I'm very intrested :D/>

The easiest method is doing something like:

local redirect = {}
local current  = term.current()
local monitor = peripheral.wrap("right")
for k in pairs(current) do
  -- Get the function for both the current terminal and the monitor object
  local curFunc, monFunc = current[k], monitor[k]
  redirect[k] = function(...)
    -- Call both, but only return the value for the current one (so term.getSize/term.getCursorPos/term.getColor return for the main terminal)
    monFunc(...)
    return curFunc(...)
  end
end
term.redirect(redirect)

This simply gets every function in the current terminal object and creates a new table which calls both the monitor and the current terminal. There are obvious issues with this - if one is a colour monitor and the other isn't then there will be issues, term.getSize is a bit odd, etc… Ideally you would write individual functions for each of them but I am lazy.
Terminator_NL #7
Posted 01 February 2015 - 12:00 PM
The easiest method is doing something like:

local redirect = {}
local current  = term.current()
local monitor = peripheral.wrap("right")
for k in pairs(current) do
  -- Get the function for both the current terminal and the monitor object
  local curFunc, monFunc = current[k], monitor[k]
  redirect[k] = function(...)
	-- Call both, but only return the value for the current one (so term.getSize/term.getCursorPos/term.getColor return for the main terminal)
	monFunc(...)
	return curFunc(...)
  end
end
term.redirect(redirect)

Thank you for your advice! I have one question, I have tried your program, and I have ran it. but I have failed to make it output multiple outputs. Do I need to do anything else to make it work?

I have the feeling I understand the program, but it will not output to the monitor. I changed nothing in your code.
SquidDev #8
Posted 01 February 2015 - 12:37 PM
-snip-
Thank you for your advice! I have one question, I have tried your program, and I have ran it. but I have failed to make it output multiple outputs. Do I need to do anything else to make it work?

I have the feeling I understand the program, but it will not output to the monitor. I changed nothing in your code.

Hmmm, I didn't test the code so it may not be perfect. You might need to change line 3: change "right" to whatever side the monitor is on.
Terminator_NL #9
Posted 01 February 2015 - 01:31 PM
Hmmm, I didn't test the code so it may not be perfect. You might need to change line 3: change "right" to whatever side the monitor is on.

Still no results. It does not output anything either.
This is what happens when:
  • No programs have run before
  • Program name: SquidDev
  • no startup program is ran
  • The disk drive is empty.
Spoiler
SquidDev #10
Posted 01 February 2015 - 02:27 PM
-snip-
You need to add some output too. So add some print statements or whatever afterwards. So for instance you could copy the monitor program but using this redirect object instead.:
Terminator_NL #11
Posted 01 February 2015 - 04:32 PM
You need to add some output too. So add some print statements or whatever afterwards. So for instance you could copy the monitor program but using this redirect object instead.:
You have me baffled.Amazing job! I wish I knew this before. This does however render my program useless but now I have learned!
Thank you so much!
Spoiler
SquidDev #12
Posted 01 February 2015 - 05:53 PM
You have me baffled.Amazing job! I wish I knew this before. This does however render my program useless but now I have learned!
Thank you so much!

You have no idea how many times I have written code just to find something much better on the internet already. I guess the key thing is learning (urgh, that sounds like such a cliché).
MKlegoman357 #13
Posted 02 February 2015 - 06:25 AM
…In CC 1.6+ it is best to then run a program you'll want to be 'syncing' the screen with. Otherwise, it will just look like it's not working…

What I meant here is that after you redirect the screen in your program you should clear it and run a program you want to redirect in the same script (ex.: shell.run("/rom/programs/shell")).
Terminator_NL #14
Posted 02 February 2015 - 06:57 PM
What I meant here is that after you redirect the screen in your program you should clear it and run a program you want to redirect in the same script (ex.: shell.run("/rom/programs/shell")).

Dear MKlegoman, I have made this program with the intention to completely sync all the actions you do on the computer, which includes the shell and all running programs after. However, it no longer matters because this can be done much more simple with the code SquidDev provided here, This code also seems to work exactly the way you describe. So I suggest you use this code instead, with great thanks to SquidDev:

local redirect = {}
local current  = term.current()
local monitor = peripheral.wrap("right")
for k in pairs(current) do
  -- Get the function for both the current terminal and the monitor object
  local curFunc, monFunc = current[k], monitor[k]
  redirect[k] = function(...)
	-- Call both, but only return the value for the current one (so term.getSize/term.getCursorPos/term.getColor return for the main terminal)
	monFunc(...)
	return curFunc(...)
  end
end
term.redirect(redirect)

Kind Regards,
-Term