Advertisement
TheOddByte

[ComputerCraft] Monitor - Mirror

Jul 16th, 2015
484
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.65 KB | None | 0 0
  1. --[[
  2.     [Program] Mirror
  3.     @version 1.0, 2015-04-12
  4.     @author TheOddByte
  5. --]]
  6.  
  7.  
  8.  
  9. local version = "1.0"
  10. local w, h    = term.getSize()
  11.  
  12. local args = { ... }
  13. if #args < 1 then
  14.     error( "Usage: " .. shell.getRunningProgram() .. " <redirect/restore>", 0 )
  15. end
  16.  
  17.  
  18. --# Find and make sure there are any monitors attached
  19. local monitors = { peripheral.find("monitor") }
  20. if #monitors == 0 then
  21.     error( "No monitors attached", 0 )
  22. end
  23.  
  24.  
  25.  
  26. --# Create a backup of the term api
  27. native = native or {}
  28. if not native.term then
  29.     native.term = {}
  30.     for k, v in pairs( term ) do
  31.         native.term[k] = v
  32.     end
  33.     native.os = {}
  34.     for k, v in pairs( os ) do
  35.         native.os[k] = v
  36.     end
  37. end
  38.  
  39.  
  40. --# Some settings you can change
  41. local settings = {
  42.     clear        = true;
  43.     monitorClick = true;
  44. }
  45.  
  46.  
  47. local command = args[1]:lower()
  48. if command == "redirect" then
  49.     --# Scale the monitors to match the terminal
  50.     for i = 1, #monitors do
  51.         for scale = 1, 5, 0.5 do
  52.             monitors[i].setTextScale( scale )
  53.             local width, height = monitors[i].getSize()
  54.             if width < w or height < h then
  55.                 monitors[i].setTextScale( scale - 0.5 )
  56.                 break
  57.             end
  58.         end
  59.     end
  60.     --# Redirect the term api to do the
  61.     --# same stuff on the monitors
  62.     for name, _ in pairs( monitors[1] ) do
  63.         term[name] = function( ... )
  64.             for i = 1, #monitors do
  65.                 monitors[i][name]( unpack( arg ) )
  66.             end
  67.             if native.term[name] then
  68.                 return native.term[name]( unpack( arg ) )
  69.             end
  70.         end
  71.     end
  72.     if settings.monitorClick then
  73.         os.pullEvent = function( filter )
  74.             local e = { native.os.pullEvent( filter ) }
  75.             if e[1] == "monitor_touch" then
  76.                 e[1] = "mouse_click"
  77.                 e[2] = 1
  78.             end
  79.             return unpack( e )
  80.         end
  81.     end
  82.    
  83.     term.clear()
  84.     term.setCursorPos( 1, 1 )
  85.     print( "Mirror " .. version .. " - " .. #monitors .. " monitors wrapped" )
  86.    
  87. elseif command == "restore" then
  88.  
  89.     --# Restore apis to normal
  90.     for k, v in pairs( native.term ) do
  91.         term[k] = v
  92.     end
  93.     for k, v in pairs( native.os ) do
  94.         os[k] = v
  95.     end
  96.     if settings.clear then
  97.         for i = 1, #monitors do
  98.             monitors[i].setBackgroundColor( colors.black )
  99.             monitors[i].setCursorPos( 1, 1 )
  100.             monitors[i].clear()
  101.         end
  102.     end
  103.    
  104. else
  105.     printError( "Invalid command: " .. command .. "\nUsage: " .. shell.getRunningProgram() .. " <redirect/restore>" )
  106. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement