Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- terminal redirect
- by BigShinyToys
- ]]--
- local function class(tInput)
- local tClass = tInput
- local mt = {
- __index = tClass
- }
- tClass.new = function(o)
- local tObj = o or {}
- setmetatable(tObj,mt)
- return tObj
- end
- return tClass
- end
- local screen = class {
- screen = {},
- realX = 30,
- realY = 10,
- posX = 1,
- posY = 1,
- isBlink = false,
- screenTX = colors.white,
- screenBC = colors.black,
- scp = term.native.setCursorPos,
- scb = term.native.setCursorBlink,
- stc = term.native.setTextColor,
- sbc = term.native.setBackgroundColor,
- wrt = term.native.write,
- drawTime,
- speed = 0.01,
- offX = 0,
- offY = 0,
- triger = function(s)
- if not s.drawTime then
- s.drawTime = os.startTimer(s.speed)
- end
- end,
- clearDrawTimer = function(s)
- s.drawTime = false
- end,
- getDrawTimer = function(s)
- return s.drawTime
- end,
- resize = function(s,width,hight) -- probble could be cleaner
- local difX = s.realX - width
- local difY = s.realY - hight
- if difY ~= 0 then
- if difY < 0 then
- for y = s.realY,hight do
- s.screen[y] = {}
- for x = 1,width do
- s.screen[y][x] = {s.screenBC,s.screenTX," "}
- end
- end
- else
- for y = s.realY + difY,s.realY do
- s.screen[y] = nil
- end
- end
- if difX ~= 0 then
- if difX < 0 then
- for y = 1,hight do
- for x = s.realY,width do
- if not s.screen[y][x] then
- s.screen[y][x] = {s.screenBC,s.screenTX," "}
- end
- end
- end
- else
- for y = 1,hight do
- for x = s.realY + difX,s.realY do
- s.screen[y][x] = nil
- end
- end
- end
- end
- end
- s.realX = width
- s.realY = hight
- end,
- setPos = function(s,x,y)
- s.offX = x
- s.offY = y
- end,
- newLine = function(s)
- local tab = {}
- for x = 1,s.realX do
- tab[x] = {s.screenBC,s.screenTX," "}
- end
- return tab
- end,
- slowDraw = function(s)
- s.drawTime = false
- s.scb(false)
- for y = 1,s.realY do
- s.scp(1 + s.offX,y + s.offY)
- for x = 1,s.realX do
- s.sbc(s.screen[y][x][1])
- s.stc(s.screen[y][x][2])
- s.wrt(s.screen[y][x][3])
- end
- end
- if s.isBlink then
- s.scp(s.posX + s.offX,s.posY + s.offY)
- s.scb(s.isBlink)
- else
- s.scb(s.isBlink)
- end
- end,
- draw = function(s) -- faster
- s.drawTime = false
- s.scb(false)
- for y = 1,s.realY do
- s.scp(1 + s.offX,y + s.offY)
- local lastTC
- local lastBC
- local TX = {}
- for x = 1,s.realX do
- local tempBC = s.screen[y][x][1]
- local tempTC = s.screen[y][x][2]
- if lastTC == tempTC and lastBC == tempBC then
- TX[#TX + 1] = s.screen[y][x][3]
- elseif lastBC then
- s.sbc(lastBC or tempBC)
- s.stc(lastTC or tempTC)
- s.wrt(table.concat(TX,""))
- TX = {}
- lastTC = tempTC
- lastBC = tempBC
- TX[#TX + 1] = s.screen[y][x][3]
- else
- lastTC = tempTC
- lastBC = tempBC
- TX[#TX + 1] = s.screen[y][x][3]
- end
- end
- s.sbc(lastBC)
- s.stc(lastTC)
- s.wrt(table.concat(TX,""))
- end
- if s.isBlink then
- s.scp(s.posX + s.offX,s.posY + s.offY)
- s.scb(s.isBlink)
- else
- s.scb(s.isBlink)
- end
- end,
- isColor = function(s)
- return true
- end,
- isColour = function(s)
- return true
- end,
- setTextColor = function(s,color)
- s.screenTX = color
- end,
- setTextColour = function(s,color)
- s.screenTX = color
- end,
- setBackgroundColor = function(s,color)
- s.screenBC = color
- end,
- setBackgroundColour = function(s,color)
- s.screenBC = color
- end,
- getSize = function(s)
- return s.realX,s.realY
- end,
- clearLine = function(s)
- s.screen[s.posY] = s:newLine()
- s:triger()
- end,
- setCursorBlink = function(s,bool)
- s.isBlink = bool
- s:triger()
- end,
- getCursorPos = function(s)
- return s.posX,s.posY
- end,
- clear = function(s)
- s.screen = {}
- for y = 1,s.realY do
- s.screen[y] = {}
- for x = 1,s.realX do
- s.screen[y][x] = {s.screenBC,s.screenTX," "}
- end
- end
- s:triger()
- end,
- scroll = function(s,n)
- for i = 1,n do
- table.remove(s.screen,1)
- table.insert(s.screen,s:newLine())
- end
- s:triger()
- end,
- setCursorPos = function(s,x,y)
- s.posX = math.floor(x)
- s.posY = math.floor(y)
- s:triger()
- end,
- write = function(s,line)
- if line and type(line) == "string" then
- for i = 0,#line - 1 do
- if s.screen[s.posY] and s.screen[s.posY][s.posX + i] then
- s.screen[s.posY][s.posX + i] = {s.screenBC,s.screenTX,string.sub(line,i + 1,i + 1)}
- end
- end
- s.posX = s.posX + #line
- end
- s:triger()
- end,
- getRedirect = function(s)
- local list = {
- "isColor","isColour","setTextColor","setTextColour","setBackgroundColor","setBackgroundColour","getSize",
- "clearLine","setCursorBlink","getCursorPos","clear","scroll","setCursorPos","write",
- }
- local fakeTerm = {}
- local screen = s
- for i = 1,#list do
- fakeTerm[list[i]] = function(...)
- return screen[list[i]](screen,...)
- end
- end
- return fakeTerm
- end,
- }
- --[[
- local myScreen = screen.new()
- myScreen:clear() -- required to inishilize grid
- myScreen:resize(40,18)
- myScreen:resize(30,10)
- myScreen:setPos(20,0)
- --[=[ test
- myScreen:write("Hello World!")
- myScreen:draw()
- ]=]--
- local fTerm = myScreen:getRedirect()
- term.redirect(fTerm)
- os.pullEventRaw = function(...)
- while true do
- local event = {coroutine.yield(...)}
- if event[1] == "timer" and event[2] == myScreen:getDrawTimer() then
- --if event[1] == "timer" then
- myScreen:draw()
- else
- return unpack(event)
- end
- end
- end
- ]]--
- local tTask = {}
- local newTask = function(path,x,y)
- if fs.exists(path) then
- local sc = screen.new()
- sc:clear()
- sc:resize(25,19)
- sc:setPos(x,y)
- local prog = coroutine.create(loadfile(path))
- table.insert(tTask,{co = prog,sc = sc,path = path,fTerm = sc:getRedirect()})
- end
- end
- --[[ nothing to see here move along
- newTask("rom/user/NEW/CIA login.lua",-10,-10)
- newTask("rom/user/NEW/CIA login.lua",15,-10)
- newTask("rom/user/NEW/CIA login.lua",-10,10)
- newTask("rom/user/NEW/CIA login.lua",15,10)
- newTask("rom/user/NEW/CIA login.lua",40,-10)
- newTask("rom/user/NEW/CIA login.lua",40,10)
- ]]--
- newTask("rom/programs/secret/alongtimeago",-10,-10)
- newTask("rom/programs/secret/alongtimeago",15,-10)
- newTask("rom/programs/secret/alongtimeago",-10,10)
- newTask("rom/programs/secret/alongtimeago",15,10)
- newTask("rom/programs/secret/alongtimeago",40,-10)
- newTask("rom/programs/secret/alongtimeago",40,10)
- while true do
- local event = {coroutine.yield()} -- add code to correctly divide up events like click
- if event[1] == "timer" then
- for i = 1,#tTask do
- if event[2] == tTask[i].sc:getDrawTimer() then
- tTask[i].sc:draw()
- break
- end
- end
- for i = 1,#tTask do
- term.redirect(tTask[i].fTerm)
- coroutine.resume(tTask[i].co,unpack(event))
- term.restore()
- end
- else
- for i = 1,#tTask do
- term.redirect(tTask[i].fTerm)
- coroutine.resume(tTask[i].co,unpack(event))
- term.restore()
- --[[
- yes i could have been smart and added a "term"
- to the envirmant but that would not affect print
- , write and other functions that affect the
- screen. so I'm using lyqyds method of redirection
- ]]--
- end
- end
- end
- --term.redirect(tTask[1].fTerm)
- --[[
- os.pullEventRaw = function(...)
- while true do
- local event = {coroutine.yield(...)}
- if event[1] == "timer" and event[2] == tTask[1].sc:getDrawTimer() then
- tTask[1].sc:draw()
- else
- return unpack(event)
- end
- end
- end
- ]]--
- --[[
- for key,value in pairs(tTask[1].tEnv.term) do
- print(key," ",value)
- end
- ]]--
- --[[
- while true do
- local event = {coroutine.yield(...)}
- if event[1] == "timer" and event[2] == tTask[1].sc:getDrawTimer() then
- term.redirect(tTask[1].fTerm)
- tTask[1].sc:draw()
- term.redirect()
- else
- coroutine.resume(tTask[1].co,unpack(event))
- end
- end
- ]]--
- --[[
- while true do
- local event = {coroutine.yield(...)}
- if event[1] == "timer" then
- for i = 1,#tTask do
- if tTask[i].sc:getDrawTimer() == event[2] then
- tTask[i].sc:draw()
- break
- end
- end
- else
- for i = 1,#tTask do
- coroutine.resume(tTask[i].co,unpack(event))
- end
- end
- end
- ]]--
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement