Guest User

Screenshare program by MKlegoman357

a guest
Jun 17th, 2014
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.62 KB | None | 0 0
  1. --Screenshare program by MKlegoman357
  2.  
  3. local args = {...}
  4. local side = args[1] or "right"
  5. local textScale = tonumber(args[2]) or 1
  6. local program = {select(3, ...)}
  7. local name = fs.getName(shell.getRunningProgram())
  8.  
  9. if args[1] == "?" or args[1] == "help" then
  10.   print("Usage:")
  11.   print(name, " [side] [text scale] [program] [arg1] [arg2] ...")
  12.  
  13.   error("", 0)
  14. end
  15.  
  16. if peripheral.getType(side) ~= "monitor" then
  17.   error("No monitor on \"" .. side .. "\" side!", 0)
  18. end
  19.  
  20. local function clamp (n, min, max)
  21.   return n > max and max or n < min and min or n
  22. end
  23.  
  24. local function clear ()
  25.   term.setBackgroundColor(colors.black)
  26.   term.setTextColor(colors.white)
  27.   term.clear()
  28.   term.setCursorPos(1, 1)
  29. end
  30.  
  31. program[1] = program[1] or "shell"
  32.  
  33. local prevTerm = term.current()
  34. local mon = peripheral.wrap(side)
  35.  
  36. mon.setTextScale(textScale)
  37.  
  38. local termW, termH = term.getSize()
  39. local monW, monH = mon.getSize()
  40.  
  41. local width = termW < monW and termW or monW
  42. local height = termH < monH and termH or monH
  43.  
  44. local termWin = window.create(prevTerm, clamp((termW - width) / 2, 1, math.huge), clamp((termH - height) / 2, 1, math.huge), width, height, true)
  45. local monWin = window.create(mon, clamp((monW - width) / 2, 1, math.huge), clamp((monH - height) / 2, 1, math.huge), width, height, true)
  46.  
  47. local redirect = {}
  48.  
  49. for name in pairs(term.native()) do
  50.   redirect[name] = function (...)
  51.     termWin[name](...)
  52.        
  53.     return monWin[name](...)
  54.   end
  55. end
  56.  
  57. redirect.isColor = function ()
  58.   return termWin.isColor() and monWin.isColor()
  59. end
  60.  
  61. redirect.isColour = function ()
  62.   return termWin.isColour() and monWin.isColour()
  63. end
  64.  
  65. local pull = coroutine.yield
  66. local termX = (termW - width) / 2
  67. local termY = (termH - height) / 2
  68.  
  69. coroutine.yield = function (...)
  70.   while true do
  71.     local event = {pull(...)}
  72.    
  73.     term.setCursorPos(1, 1)
  74.    
  75.     print("!", table.concat(event, "!"), "!---")
  76.  
  77.     if event[1] == "mouse_click" or event[1] == "mouse_drag" or event[1] == "mouse_scroll" then
  78.       local x, y = event[3], event[4]
  79.      
  80.       if x >= termX and x <= termX + width - 1 and y >= termY and y <= termY + height - 1 then
  81.         event[3] = event[3] - termX + 1
  82.         event[4] = event[4] - termY + 1
  83.        
  84.         return unpack(event)
  85.       end
  86.     else
  87.       return unpack(event)
  88.     end
  89.   end
  90. end
  91.  
  92. term.setBackgroundColor(colors.white)
  93. term.clear()
  94.  
  95. mon.setBackgroundColor(colors.white)
  96. mon.clear()
  97.  
  98. term.redirect(redirect)
  99.  
  100. clear()
  101.  
  102. shell.run(unpack(program))
  103.  
  104. term.redirect(prevTerm)
  105. coroutine.yield = pull
  106.  
  107. clear()
  108.  
  109. term.redirect(mon)
  110. clear()
  111. term.redirect(prevTerm)
Advertisement
Add Comment
Please, Sign In to add comment