Advertisement
LDDestroier

Monc - monitor/screen sync program

Apr 24th, 2015
622
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.48 KB | None | 0 0
  1. local tArg = {...}
  2.  
  3. local textscale = 0.5
  4. local monitorName, mon
  5.  
  6. if not monIsInUse then monIsInUse = {} end
  7. if type(monIsInUse) ~= "table" then monIsInUse = {} end
  8.  
  9. local function displayHelp()
  10.     print("monc <monitor>")
  11.     print("monc <monitor> <program>")
  12.     print("monc <monitor> -s <textscale>")
  13.     print("monc <monitor> -s <textscale> <program>")
  14. end
  15.  
  16. local function setMonFuncs()
  17.     if not monIsInUse[monitorName] then
  18.         local msx,msy = mon.getSize()
  19.         oldTermWrite = term.write
  20.         term.write = function( text )
  21.                 oldTermWrite( text )
  22.                 mon.write( text )
  23.         end
  24.        
  25.         oldCursorPos = term.setCursorPos
  26.         term.setCursorPos = function( x, y )
  27.                 oldCursorPos( x, y )
  28.                 mon.setCursorPos( x, y )
  29.         end
  30.        
  31.         oldClear = term.clear
  32.         term.clear = function()
  33.                 oldClear()
  34.                 local cx,cy = mon.getCursorPos()
  35.                 for a = 1, msy do
  36.                     mon.setCursorPos(1,a)
  37.                     mon.write((" "):rep(msx))
  38.                 end
  39.                 mon.setCursorPos(cx,cy)
  40.                 --mon.clear()
  41.         end
  42.        
  43.         oldBlink = term.setCursorBlink
  44.         term.setCursorBlink = function( boolean )
  45.                 oldBlink( boolean )
  46.                 mon.setCursorBlink( boolean )
  47.         end
  48.  
  49.         oldSetTextColor = term.setTextColour
  50.         term.setTextColor = function( colour )
  51.             oldSetTextColor( colour )
  52.             mon.setTextColor( colour )
  53.         end
  54.        
  55.         oldSetBackgroundColor = term.setBackgroundColour
  56.         term.setBackgroundColor = function( bgcolu )
  57.             oldSetBackgroundColor( bgcolu )
  58.             mon.setBackgroundColor( bgcolu )
  59.         end
  60.        
  61.         oldClearLine = term.clearLine
  62.         term.clearLine = function()
  63.             oldClearLine()
  64.             local cx,cy = mon.getCursorPos()
  65.             mon.setCursorPos(1,cy)
  66.             mon.write((" "):rep(msx))
  67.             mon.setCursorPos(cx,cy)
  68.         end
  69.        
  70.         oldScroll = term.scroll
  71.         term.scroll = function( scrnum )
  72.             oldScroll(scrnum)
  73.             mon.scroll(scrnum)
  74.         end
  75.        
  76.         oldBlit = term.blit
  77.         term.blit = function(text,t,b)
  78.             oldBlit(text,t,b)
  79.             mon.blit(text,t,b)
  80.         end
  81.        
  82.         oldIsColor = term.isColor
  83.         term.isColor = function()
  84.             return oldIsColor() and mon.isColor()
  85.         end
  86.        
  87.         term.setTextColour = term.setTextColor
  88.        
  89.         term.setBackgroundColour = term.setBackgroundColor
  90.        
  91.         monIsInUse[monitorName] = true
  92.     end
  93. end
  94.  
  95. local function setRegFuncs()
  96.     if #monIsInUse > 0 then
  97.         if monIsInUse[monitorName] == true then
  98.             term.write = oldTermWrite
  99.             term.setCursorPos = oldCursorPos
  100.             term.clear = oldClear
  101.             term.setCursorBlink = oldBlink
  102.             term.setTextColor = oldSetTextColor
  103.             term.setBackgroundColor = oldSetBackgroundColor
  104.             term.setTextColour = oldSetTextColor
  105.             term.setBackgroundColour = oldSetBackgroundColor
  106.             term.blit = oldBlit
  107.             term.isColor = oldIsColor
  108.             monIsInUse[monitorName] = nil
  109.         end
  110.     end
  111. end
  112.  
  113. if tArg[1] ~= nil then
  114.     mon = nil
  115.     monitorName = tArg[1]
  116.     local mons = {peripheral.find("monitor")}
  117.     if monitorName == "exit" then
  118.         for a = 1, #mons do
  119.             monitorName = mons[a]
  120.             setRegFuncs()
  121.         end
  122.         return
  123.     elseif monitorName == "list" then
  124.         local allperiphs = peripheral.getNames()
  125.         local monnames = {}
  126.         for a = 1, #allperiphs do
  127.             if peripheral.getType(allperiphs[a]) == "monitor" then
  128.                 table.insert(monnames,allperiphs[a])
  129.             end
  130.         end
  131.         print("Monitors:")
  132.         for a = 1, #monnames do
  133.             print("+"..monnames[a])
  134.         end
  135.         return
  136.     end
  137.     if peripheral.getType(monitorName) == "monitor" then
  138.         mon = peripheral.wrap(monitorName)
  139.         mon.setCursorPos(term.getCursorPos())
  140.     else
  141.         error("no such monitor as " .. monitorName)
  142.     end
  143.  
  144.     if tArg[2] == "-s" then
  145.         if type(tonumber(tArg[3])) == "number" then
  146.             if tonumber(tArg[3]) >= 0.5 and tonumber(tArg[3]) <= 5 then
  147.                 progpos = 4
  148.                 argpos = 5
  149.                 mon.setTextScale(tonumber(tArg[3]))
  150.             else
  151.                 error("invalid text scale, use 0.5-5")
  152.             end
  153.             if tArg[4] == nil then
  154.                 setMonFuncs()
  155.                 return
  156.             end
  157.         else
  158.             progpos = 2
  159.             argpos = 3
  160.             mon.setTextScale(textscale)
  161.         end
  162.     else
  163.         progpos = 2
  164.         argpos = 3
  165.         mon.setTextScale(textscale)
  166.     end
  167.    
  168.     if tArg[progpos] ~= nil then
  169.         local program = tArg[progpos]
  170.         local arguments
  171.         if tArg[progpos] == "exit" then
  172.             setRegFuncs()
  173.             return
  174.         else
  175.             if tArg[argpos] ~= nil then
  176.                 arguments = ""
  177.                 for a = argpos, #tArg do
  178.                     arguments = arguments .. " " .. tArg[a]
  179.                 end
  180.             end
  181.         end
  182.        
  183.         if arguments ~= nil and arguments ~= "" then
  184.             setMonFuncs()
  185.             shell.run(program .. " " .. arguments)
  186.             setRegFuncs()
  187.         else
  188.             setMonFuncs()
  189.             shell.run(program)
  190.             setRegFuncs()
  191.         end
  192.     else
  193.         setMonFuncs()
  194.     end
  195. else
  196.     displayHelp()
  197.     setRegFuncs()
  198. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement