Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- Screenshare program by @MKlegoman357
- This is a program you which mirrors computer's screen
- onto a monitor. This program automatically adapts to
- both, terminal and monitor for the best user experience.
- It also auto-adapts to monitor and screen size changes,
- as well as notices when a monitor gets disconnected.
- Usage:
- screenshare <side> [text scale] [program] [arguments]
- side - this is the side or network name the monitor is connected to
- text scale - this is the text scale of the monitor (default: 1)
- program, arguments - this is the program you want to run (default: shell or multishell, depends if advanced computer or not)
- Notes:
- * only works with CC 1.6 and above
- * all arguments, except side, are optional
- * uses default 'window' API, which is pretty laggy
- ]]
- local args = {...}
- local side = args[1]
- local textScale = tonumber(args[2]) or 1
- local program = {select(3, ...)}
- local name = shell and shell.getRunningProgram and fs.getName(shell.getRunningProgram()) or "screenshare"
- if #args == 0 or args[1] == "?" or args[1] == "help" then
- print("Usage:")
- print(name, " <side> [text scale] [program] [arguments] ...")
- error("", 0)
- end
- if peripheral.getType(side) ~= "monitor" then
- error("No monitor on \"" .. side .. "\" side!", 0)
- end
- local function clamp (n, min, max)
- return n > max and max or n < min and min or n
- end
- local function clear (t, bc)
- t = t or term
- t.setBackgroundColor(bc or colors.black)
- t.setTextColor(colors.white)
- t.clear()
- t.setCursorPos(1, 1)
- end
- local prevTerm = term.current()
- local mon = peripheral.wrap(side)
- textScale = clamp(textScale, 0.5, 5)
- mon.setTextScale(textScale)
- local termW, termH, monW, monH, termX, termY, monX, monY, width, height
- local function updateVariables ()
- termW, termH = prevTerm.getSize()
- monW, monH = mon.getSize()
- width = termW < monW and termW or monW
- height = termH < monH and termH or monH
- termX, termY = clamp(math.ceil((termW - width) / 2), 1, termW), clamp(math.ceil((termH - height) / 2), 1, termH)
- monX, monY = clamp(math.ceil((monW - width) / 2), 1, monW), clamp(math.ceil((monH - height) / 2), 1, monH)
- end
- updateVariables()
- local termWin = window.create(prevTerm, termX, termY, width, height, true)
- local monWin = window.create(mon, monX, monY, width, height, true)
- local redirect = {}
- for name in pairs(term.native()) do
- redirect[name] = function (...)
- termWin[name](...)
- return monWin[name](...)
- end
- end
- redirect.isColor = function ()
- return term.native().isColor() and mon.isColor()
- end
- redirect.isColour = function ()
- return term.native().isColour() and mon.isColour()
- end
- local function updateDisplays ()
- mon.setTextScale(textScale)
- updateVariables()
- termWin.reposition(termX, termY, width, height)
- monWin.reposition(monX, monY, width, height)
- clear(prevTerm, colors.white)
- clear(mon, colors.white)
- termWin.redraw()
- monWin.redraw()
- end
- clear(prevTerm, colors.white)
- clear(mon, colors.white)
- term.redirect(redirect)
- clear()
- if not program[1] then
- if term.isColor() then
- program[1] = "/rom/programs/advanced/multishell.lua"
- else
- program[1] = "/rom/programs/shell.lua"
- end
- end
- local env = setmetatable({shell = shell, multishell = multishell}, {__index = _G})
- local co
- do
- local func, err = loadfile(program[1])
- if not func then
- printError("Error loading file:")
- error(err, 0)
- end
- setfenv(func, env)
- co = coroutine.create(func)
- end
- local ok, filter = coroutine.resume(co, unpack(program, 2))
- while ok and coroutine.status(co) ~= "dead" do
- local event = {coroutine.yield(filter)}
- if event[1] == "mouse_click" or event[1] == "mouse_drag" or event[1] == "mouse_scroll" then
- event[3] = event[3] - termX + 1
- event[4] = event[4] - termY + 1
- elseif event[1] == "monitor_resize" and event[2] == side then
- updateDisplays()
- os.queueEvent("term_resize", "monitor")
- elseif event[1] == "term_resize" then
- updateDisplays()
- elseif event[1] == "peripheral_detach" and event[2] == side then
- term.redirect(prevTerm)
- clear()
- printError("Monitor was disconnected! Please attach the monitor on \"" .. side .. "\" side or press [E] to exit.")
- term.setCursorBlink(false)
- repeat
- local event, k = os.pullEvent()
- if event == "key" and k == keys.e then
- clear()
- print("Screenshare stopped!")
- error("", 0)
- end
- until peripheral.getType(side) == "monitor"
- updateDisplays()
- end
- ok, filter = coroutine.resume(co, unpack(event))
- end
- if not ok then
- error(filter, 0)
- end
- term.redirect(prevTerm)
- clear(prevTerm)
- clear(mon)
- print("Screenshare stopped!")
Advertisement
Add Comment
Please, Sign In to add comment