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

Saving function output

Started by CCGrimHaxor, 20 January 2015 - 03:34 PM
CCGrimHaxor #1
Posted 20 January 2015 - 04:34 PM
Hey guys I was wondering how I would execute a function and then save it's outputs.
Not the return but the actual prints and writes. The drawing and clearing of terminal is not needed.
Thanks
InDieTasten #2
Posted 20 January 2015 - 04:53 PM
well, override print and write to save their passed arguments in a string you can later access, run the function which outputs to "capture", restore the functions and you are good to go. write for code examples
civilwargeeky #3
Posted 20 January 2015 - 04:53 PM
As far as I know, you can't just read what is on a specific part of the screen without re-writing the "write" or "print" function.

Getting what you tell print to output should be really easy though

local oldPrint = print
function print(...)
  local tab = {...}
  for i=1, #tab do
	tab[i] = tostring(tab[i])
  end
  toReturn = table.concat(tab)
  oldPrint(toReturn)
  return toReturn
end 
This would do what I *think* you want, but I can't exactly tell what you want to get.
Edited on 20 January 2015 - 03:54 PM
InDieTasten #4
Posted 20 January 2015 - 05:05 PM
As far as I know, you can't just read what is on a specific part of the screen without re-writing the "write" or "print" function.



  oldPrint(toReturn)
  return toReturn

this would change the behaviour of the program, as print has it's own return values (number of linebreaks made)

I think he/she is looking for something like this:

local output = ""
--override
local oldPrint = print
print = function(...)
local args = {...}
for k,v in ipairs(args) do
  local e,m = pcall(tostring, v)
  if(not e) then
   error(m, 2)
  end
  output = output..m
end
output = output.."\n"
return oldPrint(...)
end
--call
yourFunc()
--restore
print = oldPrint
--analyze
-- here you can use output, which contains the
-- text printed by yourFunc, without cursor position changes though
CCGrimHaxor #5
Posted 20 January 2015 - 05:07 PM
As far as I know, you can't just read what is on a specific part of the screen without re-writing the "write" or "print" function.

Getting what you tell print to output should be really easy though

local oldPrint = print
function print(...)
  local tab = {...}
  for i=1, #tab do
	tab[i] = tostring(tab[i])
  end
  toReturn = table.concat(tab)
  oldPrint(toReturn)
  return toReturn
end 
This would do what I *think* you want, but I can't exactly tell what you want to get.

Yep that's it thanks

As far as I know, you can't just read what is on a specific part of the screen without re-writing the "write" or "print" function.



  oldPrint(toReturn)
  return toReturn

this would change the behaviour of the program, as print has it's own return values (number of linebreaks made)

I think he/she is looking for something like this:

local output = ""
--override
local oldPrint = print
print = function(...)
local args = {...}
for k,v in ipairs(args) do
  local e,m = pcall(tostring, v)
  if(not e) then
   error(m, 2)
  end
  output = output..m
end
output = output.."\n"
return oldPrint(...)
end
--call
yourFunc()
--restore
print = oldPrint
--analyze
-- here you can use output, which contains the
-- text printed by yourFunc, without cursor position changes though

Nice thanks
Bomb Bloke #6
Posted 21 January 2015 - 01:31 AM
If you're on 1.6 or later, a "simple" way of doing it would be to draw everything you want into a window, then if you want to redraw it later, simply call that window's redraw function.