Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Wojbie Edit of monitor program into a mirror program
- function printUsage()
- print( "Usage: mirror [flag] <side> <program> <arguments>" )
- print( "flag is optional and can be as follows: " )
- print( "\"-b\" - Removes border on monitor" )
- print( "\"-a\" - Uses alternative charset for border on monitor" )
- print( "\"-t\" - Turns on monitor_touch event generation" )
- print( "\"-c\" - Turns off mouse_click 1 event generation" )
- return
- end
- local tArgs = { ... }
- if #tArgs < 2 then
- printUsage()
- return
- end
- local bor={"+","-","|"}
- local monitor_touch = false
- local mouse_click = true
- local notflag=false
- repeat
- if tArgs[1]=="-b" then bor={" "," "," "} table.remove(tArgs,1)
- elseif tArgs[1]=="-a" then bor={"#","=","I"} table.remove(tArgs,1)
- elseif tArgs[1]=="-t" then monitor_touch = true table.remove(tArgs,1)
- elseif tArgs[1]=="-c" then mouse_click = false table.remove(tArgs,1)
- else notflag=true end
- until notflag
- local sSide = tArgs[1]
- if peripheral.getType( sSide ) ~= "monitor" then
- print( "No monitor on "..sSide.." side" )
- return
- end
- local sProgram = tArgs[2]
- local sPath = shell.resolveProgram( sProgram )
- if sPath == nil then
- print( "No such program: "..sProgram )
- return
- end
- print( "Running "..sProgram.." on "..sSide.." monitor" )
- local monitorside = peripheral.wrap( sSide )
- --[[
- --metatable magic
- local monitor={}
- 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}
- setmetatable( monitor, monitormeta )
- --]]
- local monitor=monitorside
- if term.isColor() and not monitor.isColor() then print( "Monitor needs to be Advanced one if terminal is Advanced." ) return end
- --coorutine setup
- local co = coroutine.create( function()
- shell.run( sProgram, unpack( tArgs, 3 ) )
- end )
- local function resume( ... )
- local ok, param = coroutine.resume( co, ... )
- if not ok then
- printError( param )
- end
- return param
- end
- --test function
- local function sleepEvent(A)
- local tempT={}
- local timer=os.startTimer(A)
- while true do
- local temp={os.pullEvent()}
- if temp[1]=="timer" and temp[2]==timer then break end
- table.insert(tempT,temp)--print(unpack(tempT[#tempT]))
- end
- while #tempT >0 do
- os.queueEvent(unpack(table.remove(tempT)))
- end
- return true
- end
- --monitor messing here
- local x,y -- monitor size()
- local j,l --term size()
- local js,ls -- saved js and ls
- local changed=true --scale changed
- local i,k -- offsets
- local saveterm=term -- copy of current term
- local oldwrap=peripheral.wrap -- copy of current peripheral.wrap
- local fixmon=false --when you need to fix that monitor
- local redSides={saveterm}
- --Colors
- local moncolor=colours.white
- local monbgcolor=colours.black
- local monincolor=colours.white
- local moninbgcolor=colours.black
- --Monitor Functions
- local function monline(A)
- local _,lb=monitor.getCursorPos()
- monitor.setCursorPos(1,lb)
- if lb<k or lb>l+k+1 then
- monitor.setBackgroundColor(moninbgcolor)
- monitor.write(string.rep (" ",math.max(x,0)))
- monitor.setBackgroundColor(monbgcolor)
- elseif lb==k or lb==l+k+1 then
- monitor.setTextColor(monincolor)
- monitor.setBackgroundColor(moninbgcolor)
- if i>0 then
- monitor.write(string.rep(" ",math.max(i-1,0)))
- monitor.write(bor[1])
- end
- monitor.write(string.rep(bor[2],math.max(j,0)))
- monitor.write(bor[1])
- monitor.write(string.rep(" ",math.max(i,0)))
- monitor.setBackgroundColor(monbgcolor)
- monitor.setTextColor(moncolor)
- else
- monitor.setTextColor(monincolor)
- monitor.setBackgroundColor(moninbgcolor)
- if i>0 then
- monitor.write(string.rep(" ",math.max(i-1,0)))
- monitor.write(bor[3])
- end
- monitor.setBackgroundColor((A and moninbgcolor) or monbgcolor)
- monitor.write(string.rep(" ",math.max(j,0)))
- monitor.setBackgroundColor(moninbgcolor)
- monitor.write(bor[3])
- monitor.write(string.rep(" ",math.max(i,0)))
- monitor.setBackgroundColor(monbgcolor)
- monitor.setTextColor(moncolor)
- end
- monitor.setCursorPos(i+1,lb)
- end
- local function monclear(A)
- local _,lb=monitor.getCursorPos()
- for cy=1,y do
- monitor.setCursorPos(1,cy)
- monline(A)
- end
- monitor.setCursorPos(i+1,lb)
- end
- local function monscrool(A)
- local la,lb=monitor.getCursorPos()
- for i=1,A do
- monitor.scroll(1)
- monitor.setCursorPos(1,k-1) monline()
- monitor.setCursorPos(1,k) monline()
- monitor.setCursorPos(1,k+l) monline()
- monitor.setCursorPos(1,k+l+1) monline()
- monitor.setCursorPos(1,y) monline()
- end
- monitor.setCursorPos(la,lb)
- end
- --Prepare Monitor
- local function monsetup()
- j,l=saveterm.getSize()
- if js~=j or ls~=l then
- changed=true
- monitor.setTextScale(0.5)
- x,y=monitor.getSize()
- j,l=saveterm.getSize()
- js,ls=saveterm.getSize()
- if x<j or y<l then print("Monitor is too small to mirror",0)
- else
- --Scalling screen to max size showing whole terminal
- for m=1,5,0.5 do
- monitor.setTextScale(m)
- x,y=monitor.getSize()
- if x<j or y<l then monitor.setTextScale(m-0.5) break end
- end
- end
- --Centering Monitor Display
- x,y=monitor.getSize()
- i=math.floor((x-j)/2)
- k=math.floor((y-l)/2)
- end
- end
- local function montest(A) --already made local at line 57
- if fixmon then moncolor=colours.white monbgcolor=colours.black end
- if fixmon or( A and ( A[1]=="monitor_resize" and (A[2]==redSides[#redSides].side or A[2]==sSide) )) then
- monsetup()
- if changed then
- changed=false
- local tempx,tempy = saveterm.getCursorPos()
- monitor.setCursorPos(tempx+i,tempy+k)
- monitor.setTextColor(moncolor)
- monitor.setBackgroundColor(monbgcolor)
- monclear(true)
- monitor.setTextColor(moncolor)
- monitor.setBackgroundColor(monbgcolor)
- end
- end
- if fixmon then fixmon=false end -- resume() end --coroutine.yield()
- end
- monsetup()
- monclear(true)
- monitor.setTextColor(moncolor)
- monitor.setBackgroundColor(monbgcolor)
- local tempx,tempy = saveterm.getCursorPos()
- monitor.setCursorPos(tempx+i,tempy+k)
- --overwrite term and peripheral.wrap
- _G["peripheral"]["wrap"]= function(side) local o = oldwrap(side) if o then o.side = side end return o end
- _G["term"]={
- ["mirrored"] = true,
- ["native"] = saveterm.native,
- ["write"] = function(A) saveterm.write(A) local la,lb=monitor.getCursorPos() monitor.write(string.sub(A,1,j+i-la+1)) end,
- ["clear"] = function() saveterm.clear() monclear() end,-- monitor.clear() end,
- ["clearLine"] = function() saveterm.clearLine() monline() end,-- monitor.clearLine() end,
- ["getCursorPos"] = saveterm["getCursorPos"],
- ["setCursorPos"] = function(A,B) saveterm.setCursorPos(A,B) monitor.setCursorPos(A+i,B+k) end,
- ["setCursorBlink"] = function(A) saveterm.setCursorBlink(A) monitor.setCursorBlink(A) end,
- ["isColor"]= saveterm["isColor"],
- ["isColour"]= saveterm["isColor"],
- ["getSize"]= saveterm["getSize"],
- ["scroll"] = function(A) saveterm.scroll(A) monscrool(A) end,
- ["redirect"] = function(A)
- if A.side~=sSide then
- redSides[#redSides+1]=A
- saveterm.redirect(A)
- A.oldsetTextScale=A.oldsetTextScale or {}
- A.oldsetTextScale[sSide]=A.setTextScale
- A.setTextScale=function(B) A.oldsetTextScale[sSide](B) fixmon=true montest() end
- end
- fixmon=true
- montest()
- end,
- ["restore"] = function()
- if #redSides>1 then
- local part=redSides[#redSides]
- if part.oldsetTextScale[sSide] then part.setTextScale=part.oldsetTextScale[sSide] part.oldsetTextScale[sSide]=nil end
- redSides[#redSides]=nil
- saveterm.restore()
- end
- fixmon=true
- montest()
- end,
- ["setTextColor"] = function(A) saveterm.setTextColor(A) monitor.setTextColor(A) moncolor=A end,
- ["setBackgroundColor"] = function(A) saveterm.setBackgroundColor(A) monitor.setBackgroundColor(A) monbgcolor=A end,
- ["setTextColour"] = function(A) saveterm.setTextColor(A) monitor.setTextColor(A) moncolor=A end,
- ["setBackgroundColour"] = function(A) saveterm.setBackgroundColor(A) monitor.setBackgroundColor(A) monbgcolor=A end,
- }
- --Coorutines code from Monitor part 2
- local ok, param = pcall( function()
- local sFilter = resume()
- while coroutine.status( co ) ~= "dead" do
- local tEvent = { os.pullEventRaw() }
- montest(tEvent)
- if tEvent[1]~="monitor_touch" or (tEvent[2] ~= sSide or monitor_touch) then
- if sFilter == nil or tEvent[1] == sFilter or tEvent[1] == "terminate" then
- sFilter = resume( unpack( tEvent ) )
- end
- end
- montest()
- if coroutine.status( co ) ~= "dead"and mouse_click and (sFilter == nil or sFilter == "mouse_click" ) then
- if tEvent[1] == "monitor_touch" and tEvent[2] == sSide then
- local clix,cliy=tEvent[3]-i,tEvent[4]-k
- if clix>0 and cliy>0 and clix<=j and cliy<=l then
- sFilter = resume( "mouse_click", 1, clix,cliy , unpack( tEvent, 5 ) )
- end
- end
- end
- montest()
- end
- end )
- --Restore all .setTExtScale() that i modyfied if pepole didin't term.restore properly ( no i wont fix that for them its not my job )
- while #redSides>1 do
- local part=redSides[#redSides]
- if part.oldsetTextScale[sSide] then part.setTextScale=part.oldsetTextScale[sSide] end
- redSides[#redSides]=nil
- end
- --Restore term and peripheral.warp
- _G["term"]=saveterm
- _G["peripheral"]["wrap"]=oldwrap
- if not ok then
- printError( param )
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement