This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
bjornir90's profile picture

[Lua] [Question] Is there a way to get what is drawn on the screen ?

Started by bjornir90, 28 November 2012 - 06:53 AM
bjornir90 #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.
Orwell #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:

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 #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()!
bjornir90 #4
Posted 28 November 2012 - 09:24 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:

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.
Thanks you but as I have also some lines drawn i need something that can also get the background color at some points but it will be too complicated, and maybe impossible so i will use the thing i say in my first post.

@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 ?
Lyqyd #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.
GopherAtl #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.
Orwell #7
Posted 28 November 2012 - 02:08 PM
Please note that you'll need a complete table of term functions (sans redirect and restore) in order to redirect to that table.
Thanks for adding that. I should've said that in the first place. :P/>
bjornir90 #8
Posted 28 November 2012 - 07:05 PM
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.
Do you mean via term.redirect ? If so, sorry for my noob question (again ! XD) but what this do ? I thought that was only for rednet or monitor… :?