Advertisement
Wojbie

Monitor Mirror

Nov 8th, 2013
951
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.16 KB | None | 0 0
  1. --Wojbie Edit of monitor program into a mirror program
  2.  
  3. function printUsage()
  4.     print( "Usage: mirror [flag] <side> <program> <arguments>" )
  5.     print( "flag is optional and can be as follows: " )
  6.     print( "\"-b\" - Removes border on monitor" )
  7.     print( "\"-a\" - Uses alternative charset for border on monitor" )
  8.     print( "\"-t\" - Turns on monitor_touch event generation" )
  9.     print( "\"-c\" - Turns off mouse_click 1 event generation" )
  10.     return
  11. end
  12.  
  13. local tArgs = { ... }
  14. if #tArgs < 2 then
  15.     printUsage()
  16.     return
  17. end
  18.  
  19. local bor={"+","-","|"}
  20. local monitor_touch = false
  21. local mouse_click = true
  22.  
  23. local notflag=false
  24.  
  25. repeat
  26.  
  27. if tArgs[1]=="-b" then bor={" "," "," "} table.remove(tArgs,1)
  28. elseif tArgs[1]=="-a" then bor={"#","=","I"} table.remove(tArgs,1)
  29. elseif tArgs[1]=="-t" then monitor_touch = true table.remove(tArgs,1)
  30. elseif tArgs[1]=="-c" then mouse_click = false table.remove(tArgs,1)
  31. else notflag=true end
  32.  
  33. until notflag
  34.  
  35. local sSide = tArgs[1]
  36. if peripheral.getType( sSide ) ~= "monitor" then
  37.     print( "No monitor on "..sSide.." side" )
  38.     return
  39. end
  40.  
  41. local sProgram = tArgs[2]
  42. local sPath = shell.resolveProgram( sProgram )
  43. if sPath == nil then
  44.     print( "No such program: "..sProgram )
  45.     return
  46. end
  47.  
  48. print( "Running "..sProgram.." on "..sSide.." monitor" )
  49.  
  50. local monitorside = peripheral.wrap( sSide )
  51. --[[
  52. --metatable magic
  53. local monitor={}
  54. local monitormeta={__index = function(A,B) while not peripheral.isPresent(sSide) or not monitorside do monitorside = peripheral.wrap( sSide ) sleep(1) end if monitorside then return monitorside[B] end return nil end}
  55. setmetatable( monitor, monitormeta )
  56. --]]
  57. local monitor=monitorside
  58. if term.isColor() and not monitor.isColor() then print( "Monitor needs to be Advanced one if terminal is Advanced." ) return end
  59.  
  60. --coorutine setup
  61.  
  62. local co = coroutine.create( function()
  63.     shell.run( sProgram, unpack( tArgs, 3 ) )
  64. end )
  65.  
  66. local function resume( ... )
  67.     local ok, param = coroutine.resume( co, ... )
  68.     if not ok then
  69.         printError( param )
  70.     end
  71.     return param
  72. end
  73.  
  74. --test function
  75.  
  76. local function sleepEvent(A)
  77.     local tempT={}
  78.     local timer=os.startTimer(A)
  79.    
  80.     while true do
  81.         local temp={os.pullEvent()}
  82.         if temp[1]=="timer" and temp[2]==timer then break end
  83.         table.insert(tempT,temp)--print(unpack(tempT[#tempT]))
  84.     end
  85.  
  86.     while #tempT >0 do
  87.         os.queueEvent(unpack(table.remove(tempT)))
  88.     end
  89.  
  90.     return true
  91. end
  92.  
  93. --monitor messing here
  94.  
  95. local x,y -- monitor size()
  96. local j,l --term size()
  97. local js,ls -- saved js and ls
  98. local changed=true --scale changed
  99. local i,k -- offsets
  100. local saveterm=term -- copy of current term
  101. local oldwrap=peripheral.wrap -- copy of current peripheral.wrap
  102. local fixmon=false --when you need to fix that monitor
  103. local redSides={saveterm}
  104.  
  105. --Colors
  106. local moncolor=colours.white
  107. local monbgcolor=colours.black
  108. local monincolor=colours.white
  109. local moninbgcolor=colours.black
  110.  
  111. --Monitor Functions
  112.  
  113. local function monline(A)
  114. local _,lb=monitor.getCursorPos()
  115. monitor.setCursorPos(1,lb)
  116. if lb<k or lb>l+k+1 then
  117.     monitor.setBackgroundColor(moninbgcolor)
  118.     monitor.write(string.rep (" ",math.max(x,0)))
  119.     monitor.setBackgroundColor(monbgcolor)
  120. elseif lb==k or lb==l+k+1 then
  121.     monitor.setTextColor(monincolor)
  122.     monitor.setBackgroundColor(moninbgcolor)
  123.     if i>0 then
  124.         monitor.write(string.rep(" ",math.max(i-1,0)))
  125.         monitor.write(bor[1])
  126.     end
  127.     monitor.write(string.rep(bor[2],math.max(j,0)))
  128.     monitor.write(bor[1])
  129.     monitor.write(string.rep(" ",math.max(i,0)))
  130.     monitor.setBackgroundColor(monbgcolor)
  131.     monitor.setTextColor(moncolor)
  132. else
  133.     monitor.setTextColor(monincolor)
  134.     monitor.setBackgroundColor(moninbgcolor)
  135.     if i>0 then    
  136.         monitor.write(string.rep(" ",math.max(i-1,0)))
  137.         monitor.write(bor[3])
  138.     end
  139.    
  140.     monitor.setBackgroundColor((A and moninbgcolor) or monbgcolor)
  141.     monitor.write(string.rep(" ",math.max(j,0)))
  142.     monitor.setBackgroundColor(moninbgcolor)
  143.    
  144.     monitor.write(bor[3])
  145.     monitor.write(string.rep(" ",math.max(i,0)))
  146.     monitor.setBackgroundColor(monbgcolor)
  147.     monitor.setTextColor(moncolor)
  148. end
  149. monitor.setCursorPos(i+1,lb)
  150. end
  151.  
  152. local function monclear(A)
  153. local _,lb=monitor.getCursorPos()
  154.     for cy=1,y do
  155.         monitor.setCursorPos(1,cy)
  156.         monline(A)
  157.     end
  158. monitor.setCursorPos(i+1,lb)
  159. end
  160.  
  161. local function monscrool(A)
  162.     local la,lb=monitor.getCursorPos()
  163.     for i=1,A do
  164.         monitor.scroll(1)
  165.         monitor.setCursorPos(1,k-1) monline()
  166.         monitor.setCursorPos(1,k) monline()
  167.         monitor.setCursorPos(1,k+l) monline()
  168.         monitor.setCursorPos(1,k+l+1) monline()
  169.         monitor.setCursorPos(1,y) monline()
  170.     end
  171.     monitor.setCursorPos(la,lb)
  172. end
  173.  
  174. --Prepare Monitor
  175. local function monsetup()
  176.     j,l=saveterm.getSize()
  177.     if js~=j or ls~=l then
  178.         changed=true
  179.         monitor.setTextScale(0.5)
  180.         x,y=monitor.getSize()
  181.         j,l=saveterm.getSize()
  182.         js,ls=saveterm.getSize()
  183.         if x<j or y<l then print("Monitor is too small to mirror",0)
  184.         else
  185.             --Scalling screen to max size showing whole terminal
  186.             for m=1,5,0.5 do
  187.                 monitor.setTextScale(m)
  188.                 x,y=monitor.getSize()
  189.                 if x<j or y<l then monitor.setTextScale(m-0.5) break end               
  190.             end
  191.         end
  192.         --Centering Monitor Display
  193.         x,y=monitor.getSize()
  194.         i=math.floor((x-j)/2)
  195.         k=math.floor((y-l)/2)
  196.     end
  197. end
  198.  
  199. local function montest(A) --already made local at line 57
  200.     if fixmon then moncolor=colours.white monbgcolor=colours.black end
  201.     if fixmon or( A and ( A[1]=="monitor_resize" and (A[2]==redSides[#redSides].side or A[2]==sSide) )) then
  202.         monsetup()
  203.         if changed then
  204.         changed=false
  205.         local tempx,tempy = saveterm.getCursorPos()
  206.         monitor.setCursorPos(tempx+i,tempy+k)
  207.         monitor.setTextColor(moncolor)
  208.         monitor.setBackgroundColor(monbgcolor)
  209.         monclear(true)
  210.         monitor.setTextColor(moncolor)
  211.         monitor.setBackgroundColor(monbgcolor)
  212.         end
  213.     end
  214.     if fixmon then fixmon=false end -- resume() end --coroutine.yield()
  215. end
  216.  
  217. monsetup()
  218. monclear(true)
  219. monitor.setTextColor(moncolor)
  220. monitor.setBackgroundColor(monbgcolor)
  221. local tempx,tempy = saveterm.getCursorPos()
  222. monitor.setCursorPos(tempx+i,tempy+k)
  223.  
  224. --overwrite term and peripheral.wrap
  225.  
  226. _G["peripheral"]["wrap"]= function(side) local o = oldwrap(side) if o then o.side = side end return o end
  227. _G["term"]={
  228. ["mirrored"] = true,
  229. ["native"] = saveterm.native,
  230. ["write"] = function(A) saveterm.write(A) local la,lb=monitor.getCursorPos() monitor.write(string.sub(A,1,j+i-la+1)) end,
  231. ["clear"] = function() saveterm.clear() monclear() end,-- monitor.clear() end,
  232. ["clearLine"] = function() saveterm.clearLine() monline() end,-- monitor.clearLine() end,
  233. ["getCursorPos"] = saveterm["getCursorPos"],
  234. ["setCursorPos"] = function(A,B) saveterm.setCursorPos(A,B) monitor.setCursorPos(A+i,B+k) end,
  235. ["setCursorBlink"] = function(A) saveterm.setCursorBlink(A) monitor.setCursorBlink(A) end,
  236. ["isColor"]= saveterm["isColor"],
  237. ["isColour"]= saveterm["isColor"],
  238. ["getSize"]= saveterm["getSize"],
  239. ["scroll"] = function(A) saveterm.scroll(A) monscrool(A) end,
  240. ["redirect"] = function(A)
  241.         if A.side~=sSide then
  242.         redSides[#redSides+1]=A
  243.         saveterm.redirect(A)
  244.         A.oldsetTextScale=A.oldsetTextScale or {}
  245.         A.oldsetTextScale[sSide]=A.setTextScale
  246.         A.setTextScale=function(B) A.oldsetTextScale[sSide](B) fixmon=true montest() end
  247.         end
  248.         fixmon=true
  249.         montest()
  250.     end,
  251. ["restore"] = function()
  252.         if #redSides>1 then
  253.         local part=redSides[#redSides]
  254.         if part.oldsetTextScale[sSide] then part.setTextScale=part.oldsetTextScale[sSide] part.oldsetTextScale[sSide]=nil end
  255.         redSides[#redSides]=nil
  256.         saveterm.restore()
  257.         end
  258.         fixmon=true
  259.         montest()
  260.     end,
  261. ["setTextColor"] = function(A) saveterm.setTextColor(A) monitor.setTextColor(A) moncolor=A end,
  262. ["setBackgroundColor"] = function(A) saveterm.setBackgroundColor(A) monitor.setBackgroundColor(A) monbgcolor=A end,
  263. ["setTextColour"] = function(A) saveterm.setTextColor(A) monitor.setTextColor(A) moncolor=A end,
  264. ["setBackgroundColour"] = function(A) saveterm.setBackgroundColor(A) monitor.setBackgroundColor(A) monbgcolor=A end,
  265. }
  266.  
  267. --Coorutines code from Monitor part 2
  268. local ok, param = pcall( function()
  269.     local sFilter = resume()
  270.     while coroutine.status( co ) ~= "dead" do
  271.         local tEvent = { os.pullEventRaw() }
  272.         montest(tEvent)
  273.         if tEvent[1]~="monitor_touch" or (tEvent[2] ~= sSide or monitor_touch) then
  274.             if sFilter == nil or tEvent[1] == sFilter or tEvent[1] == "terminate" then
  275.                 sFilter = resume( unpack( tEvent ) )
  276.             end
  277.         end
  278.         montest()
  279.         if coroutine.status( co ) ~= "dead"and mouse_click and (sFilter == nil or sFilter == "mouse_click" )  then
  280.             if tEvent[1] == "monitor_touch" and tEvent[2] == sSide then
  281.                 local clix,cliy=tEvent[3]-i,tEvent[4]-k
  282.                 if clix>0 and cliy>0 and clix<=j and cliy<=l then
  283.                     sFilter = resume( "mouse_click", 1, clix,cliy , unpack( tEvent, 5 ) )
  284.                 end
  285.             end
  286.         end
  287.         montest()
  288.     end
  289. end )
  290.  
  291. --Restore all .setTExtScale() that i modyfied if pepole didin't term.restore properly ( no i wont fix that for them its not my job )
  292. while #redSides>1 do
  293.     local part=redSides[#redSides]
  294.     if part.oldsetTextScale[sSide] then part.setTextScale=part.oldsetTextScale[sSide] end
  295.     redSides[#redSides]=nil
  296. end
  297.  
  298. --Restore term and peripheral.warp
  299. _G["term"]=saveterm
  300. _G["peripheral"]["wrap"]=oldwrap
  301.  
  302. if not ok then
  303.     printError( param )
  304. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement