The usage is really simple:
Spoiler
sync.addTarget(target)
-- add a target to use. Works like term.redirect.
-- target: a table with the term functions.
sync.removeTarget(target)
-- removes a target.
-- target: a previoulsy added target table.
sync.addMonitor(side)
-- adds the monitor on the given side to the targets.
-- side: the side of the monitor to add.
sync.removeMonitor(side)
-- removes the monitor on the given side from the targets.
-- side: the side of the monitor to remove.
sync.addMonitors()
-- adds all the attached monitors to the targets.
sync.removeMonitors()
-- removes all the attached monitors from the targets.
sync.redirect(addTerminal)
-- redirects the terminal output to all the targets
-- addTerminal: set to true to add the default term to the targets.
sync.restore()
-- restores the output to the previous state (console, monitor or whatever)
sync.useMaxSize(:D/>/>/>
-- whether to use the maximum or minimum size of all the targets. b should be a boolean (true to use max size, false for min size).
(sync is the name of the api, ie whatever you named the file)Example usage:
Spoiler
os.loadAPI("sync") -- load the api
sync.redirect(true) -- add the term to the targets and start syncing
print("I'm on the console")
sync.addMonitor("left") -- add the left monitor
print("I'm on the console and the left monitor")
sync.addMonitors() -- add all the monitors
print("I'm on the console and all the monitors!")
sync.restore() -- restore output
print("Back to normal output")
Download: pastebin (KhyGwUPm)
Code:
Spoiler
-- Sync API
-- Terminal-Monitor Synchronization
-- by MysticT
local tApi = {}
local tTargets = {}
local tMonitors = {}
local bMax = false
local nWidth, nHeight = 0, 0
local nCursorX, nCursorY = 1, 1
local bBlink = false
local bgColor = colors.black
local txtColor = colors.white
local function resize()
local w, h
for target in pairs(tTargets) do
local _w, _h = target.getSize()
if bMax then
if (w == nil or h == nil) or (_w * _h) > (w * h) then
w, h = _w, _h
end
else
if (w == nil or h == nil) or (_w * _h) < (w * h) then
w, h = _w, _h
end
end
end
if w and h then
nWidth = w
nHeight = h
end
end
local function call(func, ...)
for target in pairs(tTargets) do
target[func](...)
end
end
-- Sync Functions
function tApi.getSize()
return nWidth, nHeight
end
function tApi.getCursorPos()
return nCursorX, nCursorY
end
function tApi.setCursorPos(x, y)
nCursorX, nCursorY = x, y
call("setCursorPos", x, y)
end
function tApi.setCursorBlink(B)/>
bBlink = b
call("setCursorBlink", B)/>
end
function tApi.clear()
call("clear")
end
function tApi.clearLine()
call("clearLine")
end
function tApi.write(text)
call("write", text)
nCursorX = nCursorX + #text
end
function tApi.scroll(n)
call("scroll", n)
end
function tApi.isColor()
for target in pairs(tTargets) do
if not target.isColor() then
return false
end
end
return true
end
tApi.isColour = tApi.isColor
function tApi.setBackgroundColor(c)
bgColor = c
call("setBackgroundColor", c)
end
tApi.setBackgroundColour = tApi.setBackgroundColor
function tApi.setTextColor(c)
txtColor = c
call("setTextColor", c)
end
tApi.setTextColour = tApi.setTextColor
-- API Functions
function addTarget(target)
tTargets[target] = true
resize()
target.setCursorPos(nCursorX, nCursorY)
target.setCursorBlink(bBlink)
target.setBackgroundColor(bgColor)
target.setTextColor(txtColor)
end
function removeTarget(target)
tTargets[target] = nil
end
function addMonitor(sSide)
if tMonitors[sSide] then
return true
end
if peripheral.isPresent(sSide) and peripheral.getType(sSide) == "monitor" then
local mon = peripheral.wrap(sSide)
tMonitors[sSide] = mon
addTarget(mon)
return true
end
return false
end
function removeMonitor(sSide)
if tMonitors[sSide] then
removeTarget(tMonitors[sSide])
tMonitors[sSide] = nil
end
end
function addMonitors()
for _,s in ipairs(rs.getSides()) do
addMonitor(s)
end
end
function removeMonitors()
for _,s in ipairs(rs.getSides()) do
removeMonitor(s)
end
end
function useMaxSize(B)/>
if b ~= bMax then
bMax = b
resize()
end
end
function redirect(bAddTerm)
if bAddTerm then
addTarget(term.native or term)
end
term.redirect(tApi)
end
function restore()
term.restore()
end
If you found any bugs, let me know so I can fix them.
Any feddback is appreciated.
Changelog:
- 11/3/2013: updated for CC 1.5, added color supprt
- 20/9/2012: first release