LDDestroier

Monc 2.0 - Computer/Monitors mirroring

Dec 14th, 2016
692
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.37 KB | None | 0 0
  1. --[[
  2.     Monc 2.0 - Monitor/Computer mirror program
  3.      Now with multi-monitor support
  4.       and safe disabling
  5.  
  6.     pastebin get LxbBS18S monc
  7.     std PB LxbBS18S monc
  8.     std ld monc2 monc
  9.     wget http://pastebin.com/raw/LxbBS18S
  10.  
  11.     This is a stable release. You fool!
  12. --]]
  13.  
  14. local getTableSize = function(tbl)
  15.     local out = 0
  16.     if type(tbl) == "table" then
  17.         for k,v in pairs(tbl) do
  18.             out = out + 1
  19.         end
  20.     end
  21.     return out
  22. end
  23.  
  24. local copyTable = function(tbl)
  25.     local output = {}
  26.     for k,v in pairs(tbl) do
  27.         output[k] = v
  28.     end
  29.     return output
  30. end
  31.  
  32. if not monc then
  33.     monc = {}
  34.     monc.monitors = (type(monc.monitors) == "table" and #monc.monitors > 0) and monc.monitors or {}
  35.     if not monc.oldterm then monc.oldterm = copyTable(term) end
  36.     if not monc.origterm then monc.origterm = term.current() end
  37.     if monc.active == nil then monc.active = false end
  38.     monc.addMonitor = function(object,position)
  39.         local pos = position or (#monc.monitors+1)
  40.         monc.monitors[pos] = object
  41.         if object then
  42.             object.setBackgroundColor(monc.oldterm.getBackgroundColor())
  43.             object.setTextColor(monc.oldterm.getTextColor())
  44.             object.clear()
  45.             object.setCursorPos(monc.oldterm.getCursorPos())
  46.         end
  47.     end
  48.     monc.removeMonitor = function(position)
  49.         monc.monitors[position] = nil
  50.     end
  51.    
  52.     monc.adjustCoords = function(v,termX,termY)
  53.         if v.getSize() then
  54.            
  55.         end
  56.     end
  57.    
  58.     monc.term = {}
  59.     monc.term.getSize = function()
  60.         return monc.oldterm.getSize()
  61.     end
  62.     monc.term.getCursorPos = function()
  63.         return monc.oldterm.getCursorPos()
  64.     end
  65.     monc.term.setCursorPos = function(...)
  66.         monc.oldterm.setCursorPos(...)
  67.         for k,v in pairs(monc.monitors) do
  68.             if v.getSize() then
  69.                 v.setCursorPos(...)
  70.             end
  71.         end
  72.     end
  73.     monc.term.setVisible = function(...)
  74.         --monitors do not have a setVisible function, for some reason
  75.         monc.oldterm.setVisible(...)
  76.     end
  77.     monc.term.write = function(text)
  78.         monc.oldterm.write(text)
  79.         local sx,sy = monc.oldterm.getSize()
  80.         for k,v in pairs(monc.monitors) do
  81.             if v.getSize() then
  82.                 local cx,cy = v.getCursorPos()
  83.                 if cy > sy then
  84.                     --v.setCursorPos(cx,sy)
  85.                     break
  86.                 else
  87.                     v.write(tostring(text):sub(1,(sx-cx)+1))
  88.                 end
  89.             end
  90.         end
  91.     end
  92.     monc.term.clearLine = function()
  93.         monc.oldterm.clearLine()
  94.         local sx,sy = monc.oldterm.getSize()
  95.         for k,v in pairs(monc.monitors) do
  96.             if v.getSize() then
  97.                 local cx,cy = v.getCursorPos()
  98.                 if cy > sy then
  99.                     break
  100.                 else
  101.                     v.setCursorPos(1,cy)
  102.                     v.write((" "):rep(sx))
  103.                     v.setCursorPos(cx,cy)
  104.                 end
  105.             end
  106.         end
  107.     end
  108.     monc.term.clear = function()
  109.         monc.oldterm.clear()
  110.         local sx,sy = monc.oldterm.getSize()
  111.         for k,v in pairs(monc.monitors) do
  112.             if v.getSize() then
  113.                 local cx,cy = v.getCursorPos()
  114.                 for y = 1, sy do
  115.                     v.setCursorPos(1,y)
  116.                     v.write((" "):rep(sx))
  117.                 end
  118.                 v.setCursorPos(cx,cy)
  119.             end
  120.         end
  121.     end
  122.     monc.term.blit = function(text,tx,bg)
  123.         monc.oldterm.blit(text,tx,bg)
  124.         local sx,sy = monc.oldterm.getSize()
  125.         for k,v in pairs(monc.monitors) do
  126.             if v.getSize() then
  127.                 local cx,cy = v.getCursorPos()
  128.                 if cy > sy then
  129.                     break
  130.                 else
  131.                     v.blit(tostring(text):sub(1,1+sx-cx), tostring(tx):sub(1,1+sx-cx), tostring(bg):sub(1,1+sx-cx))
  132.                 end
  133.             end
  134.         end
  135.     end
  136.     monc.term.setCursorBlink = function(...)
  137.         monc.oldterm.setCursorBlink(...)
  138.         for k,v in pairs(monc.monitors) do
  139.             if v.getSize() then
  140.                 v.setCursorBlink(...)
  141.             end
  142.         end
  143.     end
  144.     monc.term.isColor = function()
  145.         for k,v in pairs(monc.monitors) do
  146.             if v.getSize() then
  147.                 if not v.isColor() then
  148.                     return false
  149.                 end
  150.             end
  151.         end
  152.         return monc.oldterm.isColor()
  153.     end
  154.     monc.term.scroll = function(amount)
  155.         monc.oldterm.scroll(amount)
  156.         local sx,sy = monc.oldterm.getSize()
  157.         for k,v in pairs(monc.monitors) do
  158.             if v.getSize() then
  159.                 local lastbg = monc.oldterm.getBackgroundColor()
  160.                 if amount > 0 then v.setBackgroundColor(colors.black) end
  161.                 v.scroll(amount)
  162.                 v.setBackgroundColor(lastbg)
  163.                 if amount > 0 then
  164.                     local bg = v.getBackgroundColor()
  165.                     local cx,cy = v.getCursorPos()
  166.                     for a = 1, math.abs(amount) do
  167.                         v.setCursorPos(1,sy-(a-1))
  168.                         v.clearLine()
  169.                     end
  170.                     v.setCursorPos(cx,cy)
  171.                     v.setBackgroundColor(bg)
  172.                 else
  173.                     for a = 1, math.abs(amount) do
  174.                         v.setBackgroundColor(colors.black)
  175.                         v.setCursorPos(1,sy+a)
  176.                         v.clearLine()
  177.                     end
  178.                 end
  179.             end
  180.             v.setCursorPos(monc.oldterm.getCursorPos())
  181.         end
  182.     end
  183.     monc.term.setTextColor = function(...)
  184.         monc.oldterm.setTextColor(...)
  185.         for k,v in pairs(monc.monitors) do
  186.             if v.getSize() then
  187.                 v.setTextColor(...)
  188.             end
  189.         end
  190.     end
  191.     monc.term.setBackgroundColor = function(...)
  192.         monc.oldterm.setBackgroundColor(...)
  193.         for k,v in pairs(monc.monitors) do
  194.             if v.getSize() then
  195.                 v.setBackgroundColor(...)
  196.             end
  197.         end
  198.     end
  199.     monc.term.getTextColor = function()
  200.         return monc.oldterm.getTextColor()
  201.     end
  202.     monc.term.getBackgroundColor = function()
  203.         return monc.oldterm.getBackgroundColor()
  204.     end
  205.     monc.term.setTextScale = function(...)
  206.         for k,v in pairs(monc.monitors) do
  207.             if v.getSize() then
  208.                 v.setTextScale(...)
  209.             end
  210.         end
  211.     end
  212.     --[[
  213.     monc.term.redirect = function(target)
  214.         --local old = monc.oldterm
  215.         --monc.oldterm = target
  216.         --return old
  217.         monc.oldterm = term.current()
  218.         return term.current() -- ehhhh
  219.     end
  220.     --]]
  221.     monc.term.native = function()
  222.         return monc.origterm
  223.     end
  224.     monc.term.setTextColour = monc.term.setTextColor
  225.     monc.term.setBackgroundColour = monc.term.setBackgroundColor
  226.     monc.term.getTextColour = monc.term.getTextColor
  227.     monc.term.getBackgroundColour = monc.term.getBackgroundColor
  228.     monc.term.isColour = monc.term.isColor
  229.    
  230. end
  231.  
  232. local displayHelp = function()
  233.     local helpdata = [[
  234. Monc 2.0 - Computer/Monitor mirror
  235.  [optional argument] <required argument>
  236. Monitor list is stored in memory only.
  237. >monc add/remove <monitor1> [monitor2] ...
  238. >monc list
  239. >monc help
  240. >monc setsize <size> [monitor]
  241. >monc [on/off]
  242. >monc ... -p <program> [argument1] ...]]
  243.     print(helpdata)
  244. end
  245.  
  246. local tArg = {...}
  247. if tArg[1] == "help" then
  248.     displayHelp()
  249. end
  250.  
  251. local eugh = 4
  252. if tArg[1] == "add" then
  253.     local newmons = {}
  254.     for a = 2, #tArg do
  255.         if tArg[a] == "-p" then
  256.             break
  257.         else
  258.             newmons[#newmons+1] = tArg[a]
  259.         end
  260.     end
  261.     for a = 1, #newmons do
  262.         monname = newmons[a]
  263.         newmon = peripheral.wrap(monname)
  264.         if not newmon then
  265.             return print("'"..monname.."' doesn't exist.")
  266.         elseif peripheral.getType(monname) ~= "monitor" then
  267.             return print("'"..monname.."' is not a monitor.") --idiot
  268.         end
  269.         monc.addMonitor(newmon,monname)
  270.     end
  271.     write("Monitor")
  272.     if #newmons ~= 1 then write("s") end
  273.     print(" added to list.")
  274. elseif tArg[1] == "delete" or tArg[1] == "remove" then
  275.     local delmons = {}
  276.     for a = 2, #tArg do
  277.         if tArg[a] == "-p" then
  278.             break
  279.         else
  280.             delmons[#delmons+1] = tArg[a]
  281.         end
  282.     end
  283.     for a = 1, #delmons do
  284.         local delname = delmons[a]
  285.         if monc.monitors[delname] then
  286.             monc.removeMonitor(delname)
  287.         else
  288.             print("Skipping nonexistant '"..delname.."'.")
  289.         end
  290.     end
  291.     write("Deleted monitor")
  292.     if #delmons > 1 then write("s") end
  293.     print(" from list.")
  294. elseif tArg[1] == "list" then
  295.     local allperiphs = peripheral.getNames()
  296.     local allmons = {}
  297.     for a = 1, #allperiphs do
  298.         if peripheral.getType(allperiphs[a]) == "monitor" then
  299.             allmons[#allmons+1] = allperiphs[a]
  300.         end
  301.     end
  302.     if #allmons == 0 then
  303.         print("No monitors found.")
  304.     else
  305.         for a = 1, #allmons do
  306.             if monc.monitors[allmons[a]] then
  307.                 write(" ON ")
  308.             else
  309.                 write("OFF ")
  310.             end
  311.             print("\""..allmons[a].."\"")
  312.         end
  313.     end
  314.     local good = false --checks for any monitors on the list that were disconnected
  315.     for k,v in pairs(monc.monitors) do
  316.         for a = 1, #allmons do
  317.             if allmons[a] == k then
  318.                 good = true
  319.                 break
  320.             end
  321.         end
  322.         if not good then
  323.             write("DIS ")
  324.             print("\""..k.."\"")
  325.         end
  326.     end
  327. elseif tArg[1] == "setsize" then
  328.     if not tonumber(tArg[2]) then
  329.         print("Size must be a number.")
  330.     elseif tonumber(tArg[2]) < 0.5 or tonumber(tArg[2]) > 5 then
  331.         print("Size must be between 0.5 and 5 inclusive.")
  332.     end
  333.     if tArg[3] then
  334.         if not monc.monitors[tArg[3]] then
  335.             print("No such monitor.")
  336.         else
  337.             monc.monitors[tArg[3]].setTextScale(tonumber(tArg[2]) or 1)
  338.         end
  339.     else
  340.         for k,v in pairs(monc.monitors) do
  341.             v.setTextScale(tonumber(tArg[2]) or 1)
  342.         end
  343.     end
  344.     write("Resized monitor")
  345.     if not tArg[3] then
  346.         if getTableSize(monc.monitors) ~= 1 then
  347.             write("s")
  348.         end
  349.     end
  350.     print(".")
  351. elseif tArg[1] == "on" then
  352.     if monc.active then eugh = 3 else eugh = 1 end
  353. elseif tArg[1] == "off" then
  354.     if not monc.active then eugh = 3 else eugh = 1 end
  355. elseif tArg[1] == "toggle" then
  356.     eugh = 1
  357. elseif tArg[1] == "moo" then
  358.     local mooMsgs = {
  359.         "There are no Easter Eggs in this program.",
  360.         "There really are no Easter Eggs in this program.",
  361.         "Didn't I already tell you there are no easter eggs in this program?",
  362.         "Stop it!",
  363.         "Okay, okay, if I give you an Easter Egg, will you go away?",
  364.         [[Alright, you win.
  365.  
  366.                                /----\
  367.                        -------/      \
  368.                       /               \
  369.                      /                |
  370.    -----------------/                  --------\
  371.    ----------------------------------------------]],
  372.         "What is it?  It's an elephant being eaten by a snake, of course.",
  373.     }
  374.     local mooLvl = 1
  375.     for a = 1, #tArg do
  376.         if tArg[a]:sub(1,1) == "-" and tArg[a]:gsub("v","") == "-" then
  377.             mooLvl = mooLvl + #tArg[a]:sub(2)
  378.         end
  379.     end
  380.     print(mooMsgs[mooLvl < #mooMsgs and mooLvl or #mooMsgs])
  381. elseif not tArg[1] then
  382.     eugh = 2
  383. end
  384.  
  385. local setTheTerm = function(newterm)
  386.     for k,v in pairs(newterm) do
  387.         term[k] = v
  388.     end
  389. end
  390.  
  391. local toggleMonc = function(active)
  392.     if not monc.active then
  393.         monc.active = true
  394.         setTheTerm(monc.term)
  395.     else
  396.         monc.active = false
  397.         setTheTerm(monc.oldterm)
  398.     end
  399. end
  400. --time for the craaaaaazy stuff
  401. if eugh == 1 then
  402.     toggleMonc()
  403.     print("Monc "..(monc.active and "activated!" or "deactivated."))
  404. elseif eugh == 2 then
  405.     print("Do 'monc toggle/on/off' to use, or 'monc help' for more info.")
  406. elseif eugh == 3 then
  407.     print("It's already in that state.")
  408. end
  409.  
  410. for a = 1, #tArg do
  411.     if tArg[a] == "-p" then
  412.         local filerunname = {}
  413.         for b = a+1, #tArg do
  414.             filerunname[#filerunname+1] = tArg[b]
  415.         end
  416.         if shell then
  417.             shell.run(unpack(filerunname))
  418.         else
  419.             dofile(unpack(filerunname))
  420.         end
  421.         break
  422.     end
  423. end
Add Comment
Please, Sign In to add comment