Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- 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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement