The title say all, I want to know if something like term.getPrintedOnScreen() already exists or an idea to do it
[Lua] [Question] Is there a way to get what is drawn on the screen ?
Started by bjornir90, Nov 28 2012 07:53 AM
7 replies to this topic
#1
Posted 28 November 2012 - 07:53 AM
Hello !
The title say all, I want to know if something like term.getPrintedOnScreen() already exists or an idea to do it
I have an idea : as the functions are custom functions, I think I could use SPD to know what function has runned, and then re-execute it after cleared the screen.
The title say all, I want to know if something like term.getPrintedOnScreen() already exists or an idea to do it
#2
Posted 28 November 2012 - 08:04 AM
This have been asked before. The typical way to do this is to make an api that 'inherits' the term api and stores everything in a table. So you can fetch the characters at each position on the screen.
For example:
You could redirect the term to that api to make things seemless.
For example:
local screen = {}
local w,h = term.getSize()
local x,y = 1,1
local function clearTable()
for i=1,h do
screen[i]={}
end
end
clearTable()
function clear()
term.clear()
clearTable()
end
function setCursorPos(nx, ny)
term.setCursorPos(nx,ny)
x=nx
y=ny
end
function write(str)
term.write(str)
for i=x,math.min(w,x+#str) do
screen[y][i] = str:sub(x-i+1)
end
end
function getChar(nx,ny)
return screen[ny][nx] or ''
end
You could redirect the term to that api to make things seemless.
#3
Posted 28 November 2012 - 08:38 AM
Please note that you'll need a complete table of term functions (sans redirect and restore) in order to redirect to that table. Perhaps a metatable could be used to __index the unchanged functions. And don't forget about scroll()!
#4
Posted 28 November 2012 - 09:24 AM
Orwell, on 28 November 2012 - 08:04 AM, said:
This have been asked before. The typical way to do this is to make an api that 'inherits' the term api and stores everything in a table. So you can fetch the characters at each position on the screen.
For example:
You could redirect the term to that api to make things seemless.
For example:
local screen = {}
local w,h = term.getSize()
local x,y = 1,1
local function clearTable()
for i=1,h do
screen[i]={}
end
end
clearTable()
function clear()
term.clear()
clearTable()
end
function setCursorPos(nx, ny)
term.setCursorPos(nx,ny)
x=nx
y=ny
end
function write(str)
term.write(str)
for i=x,math.min(w,x+#str) do
screen[y][i] = str:sub(x-i+1)
end
end
function getChar(nx,ny)
return screen[ny][nx] or ''
end
You could redirect the term to that api to make things seemless.
@lyqyd thanks you, but what do you mean by don't forget about scroll() ? I don't see the use of that in this case ?
#5
Posted 28 November 2012 - 11:10 AM
If a program were to term.scroll() the screen and your buffer layer didn't notice, you'd have garbage data in your buffer instead of the actual screen contents.
#6
Posted 28 November 2012 - 11:31 AM
also, the print function calls scroll if necessary to scroll the screen. So while your own code might call it rarely, it is called quite frequently by many programs, so must be included in a redirect.
#8
Posted 28 November 2012 - 07:05 PM
GopherAtl, on 28 November 2012 - 11:31 AM, said:
also, the print function calls scroll if necessary to scroll the screen. So while your own code might call it rarely, it is called quite frequently by many programs, so must be included in a redirect.
2 user(s) are reading this topic
0 members, 2 guests, 0 anonymous users











