Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- Monc 2.1 - Monitor/Computer mirror program Beta
- Now with multi-monitor support
- and safe disabling
- pastebin get GdscC2Jv monc2b
- std PB GdscC2Jv monc2b
- std ld monc2 monc
- wget http://pastebin.com/raw/GdscC2Jv
- This is a beta release. You fool!
- Working on:
- + implementing settings API for basic options
- + borders --working on right now
- + fixing term.redirect
- --]]
- local centerMonc = true
- local getTableSize = function(tbl)
- local out = 0
- if type(tbl) == "table" then
- for k,v in pairs(tbl) do
- out = out + 1
- end
- end
- return out
- end
- local copyTable = function(tbl)
- local output = {}
- for k,v in pairs(tbl) do
- output[k] = v
- end
- return output
- end
- if not monc then
- monc = {}
- monc.monitors = (type(monc.monitors) == "table" and #monc.monitors > 0) and monc.monitors or {}
- if not monc.oldterm then monc.oldterm = copyTable(term) end
- if not monc.origterm then monc.origterm = term.current() end
- if monc.active == nil then monc.active = false end
- monc.addMonitor = function(object,position)
- local pos = position or (#monc.monitors+1)
- monc.monitors[pos] = object
- if object then
- object.setBackgroundColor(monc.oldterm.getBackgroundColor())
- object.setTextColor(monc.oldterm.getTextColor())
- object.clear()
- object.setCursorPos(monc.oldterm.getCursorPos())
- end
- end
- monc.removeMonitor = function(position)
- monc.monitors[position] = nil
- end
- monc.beforeFunc = function(v) --executed before all functions that alter the screen
- --screen X, Y sizes
- local scX,scY = v.getSize()
- --native terminal X, Y sizes
- local ntX,ntY = monc.oldterm.getSize()
- --adjust X, Y
- local adjX,adjY = (centerMonc and (scX>ntX)) and (math.floor(scX-ntX)/2) or 0,(centerMonc and (scY>ntY)) and (math.floor(scX-ntX)/2) or 0
- --native terminal cursor X, Y
- local ntCX,ntCY = monc.oldterm.getCursorPos()
- v.setCursorPos(ntCX+adjX,ntCY+adjY)
- end
- monc.afterFunc = function(v) --executed after all functions that alter the screen
- return nil --unused as of now
- end
- monc.term = {}
- monc.term.getSize = function()
- return monc.oldterm.getSize()
- end
- monc.term.getCursorPos = function()
- return monc.oldterm.getCursorPos()
- end
- monc.term.setCursorPos = function(x,y)
- monc.oldterm.setCursorPos(x,y)
- for k,v in pairs(monc.monitors) do
- local scX,scY = v.getSize()
- local ntX,ntY = monc.oldterm.getSize()
- local adjX,adjY = (centerMonc and (sxX)) and (scX) or 0
- local ntCX,ntCY = monc.oldterm.getCursorPos()
- if v.getSize() then
- v.setCursorPos(ntCX+adjX,ntCY+adjY)
- end
- end
- end
- monc.term.setVisible = function(...)
- --monitors do not have a setVisible function, for some reason
- monc.oldterm.setVisible(...)
- end
- monc.term.write = function(text)
- monc.oldterm.write(text)
- local sx,sy = monc.oldterm.getSize()
- for k,v in pairs(monc.monitors) do
- if v.getSize() then
- local cx,cy = v.getCursorPos()
- if cy > sy then
- --v.setCursorPos(cx,sy)
- break
- else
- v.write(tostring(text):sub(1,(sx-cx)+1))
- end
- end
- end
- end
- monc.term.clearLine = function()
- monc.oldterm.clearLine()
- local sx,sy = monc.oldterm.getSize()
- for k,v in pairs(monc.monitors) do
- if v.getSize() then
- local cx,cy = v.getCursorPos()
- if cy > sy then
- break
- else
- v.setCursorPos(1,cy)
- v.write((" "):rep(sx))
- v.setCursorPos(cx,cy)
- end
- end
- end
- end
- monc.term.clear = function()
- monc.oldterm.clear()
- local sx,sy = monc.oldterm.getSize()
- for k,v in pairs(monc.monitors) do
- if v.getSize() then
- local cx,cy = v.getCursorPos()
- for y = 1, sy do
- v.setCursorPos(1,y)
- v.write((" "):rep(sx))
- end
- v.setCursorPos(cx,cy)
- end
- end
- end
- monc.term.blit = function(text,tx,bg)
- monc.oldterm.blit(text,tx,bg)
- local sx,sy = monc.oldterm.getSize()
- for k,v in pairs(monc.monitors) do
- if v.getSize() then
- local cx,cy = v.getCursorPos()
- if cy > sy then
- break
- else
- v.blit(tostring(text):sub(1,1+sx-cx), tostring(tx):sub(1,1+sx-cx), tostring(bg):sub(1,1+sx-cx))
- end
- end
- end
- end
- monc.term.setCursorBlink = function(...)
- monc.oldterm.setCursorBlink(...)
- for k,v in pairs(monc.monitors) do
- if v.getSize() then
- v.setCursorBlink(...)
- end
- end
- end
- monc.term.isColor = function()
- for k,v in pairs(monc.monitors) do
- if v.getSize() then
- if not v.isColor() then
- return false
- end
- end
- end
- return monc.oldterm.isColor()
- end
- monc.term.scroll = function(amount)
- monc.oldterm.scroll(amount)
- local sx,sy = monc.oldterm.getSize()
- for k,v in pairs(monc.monitors) do
- if v.getSize() then
- local lastbg = monc.oldterm.getBackgroundColor()
- if amount > 0 then v.setBackgroundColor(colors.black) end
- v.scroll(amount)
- v.setBackgroundColor(lastbg)
- if amount > 0 then
- local bg = v.getBackgroundColor()
- local cx,cy = v.getCursorPos()
- for a = 1, math.abs(amount) do
- v.setCursorPos(1,sy-(a-1))
- v.clearLine()
- end
- v.setCursorPos(cx,cy)
- v.setBackgroundColor(bg)
- else
- for a = 1, math.abs(amount) do
- v.setBackgroundColor(colors.black)
- v.setCursorPos(1,sy+a)
- v.clearLine()
- end
- end
- end
- v.setCursorPos(monc.oldterm.getCursorPos())
- end
- end
- monc.term.setTextColor = function(...)
- monc.oldterm.setTextColor(...)
- for k,v in pairs(monc.monitors) do
- if v.getSize() then
- v.setTextColor(...)
- end
- end
- end
- monc.term.setBackgroundColor = function(...)
- monc.oldterm.setBackgroundColor(...)
- for k,v in pairs(monc.monitors) do
- if v.getSize() then
- v.setBackgroundColor(...)
- end
- end
- end
- monc.term.getTextColor = function()
- return monc.oldterm.getTextColor()
- end
- monc.term.getBackgroundColor = function()
- return monc.oldterm.getBackgroundColor()
- end
- monc.term.setTextScale = function(...)
- for k,v in pairs(monc.monitors) do
- if v.getSize() then
- v.setTextScale(...)
- end
- end
- end
- --[[
- monc.term.redirect = function(target)
- --local old = monc.oldterm
- --monc.oldterm = target
- --return old
- monc.oldterm = term.current()
- return term.current() -- ehhhh
- end
- --]]
- monc.term.native = function()
- return monc.origterm
- end
- monc.term.setTextColour = monc.term.setTextColor
- monc.term.setBackgroundColour = monc.term.setBackgroundColor
- monc.term.getTextColour = monc.term.getTextColor
- monc.term.getBackgroundColour = monc.term.getBackgroundColor
- monc.term.isColour = monc.term.isColor
- end
- local displayHelp = function()
- local helpdata = [[
- Monc 2.0 - Computer/Monitor mirror
- [optional argument] <required argument>
- Monitor list is stored in memory only.
- >monc add/remove <monitor1> [monitor2] ...
- >monc list
- >monc help
- >monc setsize <size> [monitor]
- >monc [on/off]
- >monc ... -p <program> [argument1] ...]]
- print(helpdata)
- end
- local tArg = {...}
- if tArg[1] == "help" then
- displayHelp()
- end
- local eugh = 4
- if tArg[1] == "add" then
- local newmons = {}
- for a = 2, #tArg do
- if tArg[a] == "-p" then
- break
- else
- newmons[#newmons+1] = tArg[a]
- end
- end
- for a = 1, #newmons do
- monname = newmons[a]
- newmon = peripheral.wrap(monname)
- if not newmon then
- return print("'"..monname.."' doesn't exist.")
- elseif peripheral.getType(monname) ~= "monitor" then
- return print("'"..monname.."' is not a monitor.") --idiot
- end
- monc.addMonitor(newmon,monname)
- end
- write("Monitor")
- if #newmons ~= 1 then write("s") end
- print(" added to list.")
- elseif tArg[1] == "delete" or tArg[1] == "remove" then
- local delmons = {}
- for a = 2, #tArg do
- if tArg[a] == "-p" then
- break
- else
- delmons[#delmons+1] = tArg[a]
- end
- end
- for a = 1, #delmons do
- local delname = delmons[a]
- if monc.monitors[delname] then
- monc.removeMonitor(delname)
- else
- print("Skipping nonexistant '"..delname.."'.")
- end
- end
- write("Deleted monitor")
- if #delmons > 1 then write("s") end
- print(" from list.")
- elseif tArg[1] == "list" then
- local allperiphs = peripheral.getNames()
- local allmons = {}
- for a = 1, #allperiphs do
- if peripheral.getType(allperiphs[a]) == "monitor" then
- allmons[#allmons+1] = allperiphs[a]
- end
- end
- if #allmons == 0 then
- print("No monitors found.")
- else
- for a = 1, #allmons do
- if monc.monitors[allmons[a]] then
- write(" ON ")
- else
- write("OFF ")
- end
- print("\""..allmons[a].."\"")
- end
- end
- local good = false --checks for any monitors on the list that were disconnected
- for k,v in pairs(monc.monitors) do
- for a = 1, #allmons do
- if allmons[a] == k then
- good = true
- break
- end
- end
- if not good then
- write("DIS ")
- print("\""..k.."\"")
- end
- end
- elseif tArg[1] == "setsize" then
- if not tonumber(tArg[2]) then
- print("Size must be a number.")
- elseif tonumber(tArg[2]) < 0.5 or tonumber(tArg[2]) > 5 then
- print("Size must be between 0.5 and 5 inclusive.")
- end
- if tArg[3] then
- if not monc.monitors[tArg[3]] then
- print("No such monitor.")
- else
- monc.monitors[tArg[3]].setTextScale(tonumber(tArg[2]) or 1)
- end
- else
- for k,v in pairs(monc.monitors) do
- v.setTextScale(tonumber(tArg[2]) or 1)
- end
- end
- write("Resized monitor")
- if not tArg[3] then
- if getTableSize(monc.monitors) ~= 1 then
- write("s")
- end
- end
- print(".")
- elseif tArg[1] == "on" then
- if monc.active then eugh = 3 else eugh = 1 end
- elseif tArg[1] == "off" then
- if not monc.active then eugh = 3 else eugh = 1 end
- elseif tArg[1] == "toggle" then
- eugh = 1
- elseif tArg[1] == "moo" then
- local mooMsgs = {
- "There are no Easter Eggs in this program.",
- "There really are no Easter Eggs in this program.",
- "Didn't I already tell you there are no easter eggs in this program?",
- "Stop it!",
- "Okay, okay, if I give you an Easter Egg, will you go away?",
- [[Alright, you win.
- /----\
- -------/ \
- / \
- / |
- -----------------/ --------\
- ----------------------------------------------]],
- "What is it? It's an elephant being eaten by a snake, of course.",
- }
- local mooLvl = 1
- for a = 1, #tArg do
- if tArg[a]:sub(1,1) == "-" and tArg[a]:gsub("v","") == "-" then
- mooLvl = mooLvl + #tArg[a]:sub(2)
- end
- end
- print(mooMsgs[mooLvl < #mooMsgs and mooLvl or #mooMsgs])
- elseif not tArg[1] then
- eugh = 2
- end
- local setTheTerm = function(newterm)
- for k,v in pairs(newterm) do
- term[k] = v
- end
- end
- local toggleMonc = function(active)
- if not monc.active then
- monc.active = true
- setTheTerm(monc.term)
- else
- monc.active = false
- setTheTerm(monc.oldterm)
- end
- end
- --time for the craaaaaazy stuff
- if eugh == 1 then
- toggleMonc()
- print("Monc "..(monc.active and "activated!" or "deactivated."))
- elseif eugh == 2 then
- print("Do 'monc toggle/on/off' to use, or 'monc help' for more info.")
- elseif eugh == 3 then
- print("It's already in that state.")
- end
- for a = 1, #tArg do
- if tArg[a] == "-p" then
- local filerunname = {}
- for b = a+1, #tArg do
- filerunname[#filerunname+1] = tArg[b]
- end
- if shell then
- shell.run(unpack(filerunname))
- else
- dofile(unpack(filerunname))
- end
- break
- end
- end
Add Comment
Please, Sign In to add comment