LDDestroier

Monc 2.0 BETA

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