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

Get window contents

Started by KaoS, 07 September 2012 - 11:37 AM
KaoS #1
Posted 07 September 2012 - 01:37 PM
Hey all, is there a way to get the current contents of the console? I'm sure it is stored in a table somewhere to be reprinted if the window is full. does anyone know where it is?
Lyqyd #2
Posted 07 September 2012 - 03:50 PM
You can look through the write() function, but I don't believe that it is stored anywhere. You could create a terminal redirection layer that wrote to both the screen and a table, though.
KaoS #3
Posted 07 September 2012 - 04:00 PM
I don't know how term.redirect works, could you possibly explain?
OmegaVest #4
Posted 07 September 2012 - 04:56 PM
I think he meant make your own print or redirect function that stores what is printed to the screen into a table to be read at a later date.

But, for the record, term.redirect() just pushes whatever is supposed to be typed to another source, usually a monitor (ie. term.redirect(mon)).



Also, this.

hiddenTab = {}
function myWrite(whatRite)
   local hiddenStr = ""
   x, y = term.getCursorPos()
   for i = 1, y do
	  hiddenStr = hiddenStr .. "n"
   end
   for i = 1, x do
	  hiddenStr = hiddenStr .. " "
   end
   hiddenStr = hiddenStr .. whatRite
   hiddenTab[#hiddenTab+1] = hiddenStr
   term.write(whatRite)
end



EDIT: Simple program is simplistic. :D/>/>
KaoS #5
Posted 07 September 2012 - 05:25 PM
I think I have figured it out, what you do is make a table of functions like so:


funcs={}
record={}
for k,v in pairs(term.native) do
funcs[k]=v
end

then you define your custom functions


record={}
funcs.write=function(...) table.insert(record,...) return turtle.native.write(...) end

which replaces the ones already on the function list. then finally


term.redirect(funcs)

problem is making functions to accurately store the information because read() writes out the line again every time you press a key so you end up with an enormous table. any ideas on how to manage it?
Cloudy #6
Posted 07 September 2012 - 05:31 PM
Yes, store each line in a table - and edit the line when the write command is called.
KaoS #7
Posted 07 September 2012 - 05:52 PM
an interesting idea, so far I have


funcs={}
record={}
for k,v in pairs(term.native) do
funcs[k]=function(...) table.insert(record,{v,[2]={...}}) return v(...) end
funcs[k..'2']=v
end
term.redirect(funcs)

and then to restore that window you call


for k,v in pairs(record) do v[1](unpack(v[2])) sleep(0) end

and it rewrites everything, its quite cool to execute

term.clear() --you can store an alternate clear function in the func table so it won't call again if you like... up to you
for k,v in pairs(record) do v[1](unpack(v[2])) sleep(0.1) end

but include something to only sleep if the write command is executed for better effect

EDIT: or even better just make it not sleep for any clear functions so a moving cursor is also animated
MysticT #8
Posted 07 September 2012 - 06:02 PM
You can check the screen api I made for MysticOS, it lets you get a character or line from the console. It works by saving the text on a table (like you did) and the cursor position so you know where to write. It also supports writing to multiple outputs (terminal and monitors).