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

Terminal redirection questions

Started by 0099, 07 July 2014 - 05:55 PM
0099 #1
Posted 07 July 2014 - 07:55 PM
Please explain me, how is new term redirection system supposed to work?
1. I can't find any analogue for term.restore. term table itself "is not recommended redirect target", term.native is "do-not-use-unless-you-have-to" function, term.redirect(nil) is forbidden. What should I do?
2. I noticed that when a program that called term.redirect stops, it restores automatically (but the error message, if it exists, is sent to monitor).
2.1. Please, show me that piece of code that changes redirect targets back. I can't find it.
2.2. How i can prevent this thing from happening?
theoriginalbit #2
Posted 07 July 2014 - 08:09 PM
Please explain me, how is new term redirection system supposed to work?
1. I can't find any analogue for term.restore. term table itself "is not recommended redirect target", term.native is "do-not-use-unless-you-have-to" function, term.redirect(nil) is forbidden. What should I do?
2. I noticed that when a program that called term.redirect stops, it restores automatically (but the error message, if it exists, is sent to monitor).
2.1. Please, show me that piece of code that changes redirect targets back. I can't find it.
2.2. How i can prevent this thing from happening?
as stated here, term.redirect now returns the previous term object when invoked. meaning you just redirect back to that.


local monitor = peripheral.wrap("left")
local oldTerm = term.redirect(monitor)

--# code here

term.redirect(oldTerm)

it has been this way since 1.6x
Edited by
Lyqyd #3
Posted 07 July 2014 - 08:23 PM
You swapped redirect for restore in your response, theoriginalbit.

Edit: Also, this is compatible with both pre- and post-1.6:


local _old = term.redirect(target)

--# code here

if _old then
  term.redirect(_old)
else
  term.restore()
end
0099 #4
Posted 08 July 2014 - 04:46 PM
Thanks. But what about the automatical restoring - what if I don't like it?
I mean, I could write a small program to redirect output:

-- file name: "redir"
local tArgs = { ... }
assert(type(tArgs[1]) == "string", "Usage: redir (side)")
term.redirect(peripheral.wrap(tArgs[1]))
It won't work.

And I still can't find - where exactly does this happen?
theoriginalbit #5
Posted 08 July 2014 - 04:54 PM
There is no 'automatic restoring' using your code example you would do the following


local args = {...}
assert(type(args[1]) == "string", "Usage: redir (side)")

--# anything printed here will output to whatever the term object was before your program started (normally this is the computer)

local oldTerm = term.redirect(peripheral.wrap(args[1]))

--# everything printed here will output to the wrapped peripheral

term.redirect( oldTerm )

--# everything printed here will print back on the term object that was active when your program started
0099 #6
Posted 10 July 2014 - 03:37 PM
There is no 'automatic restoring' using your code example you would do the following
(code here)
The thing is, if I do NOT write term.redirect(oldTerm), when the program stops and returns to the shell, the output will be redirected automatically to the original screen. (if the program causes an error, its error report goes to the monitor, and THEN it redirects back)

P.S. My original question was in beta discussion, so I tested all this in 1.64pr2. 1.64 is where I need it, because term.restore is gone. I'll test it in an earlier version.

EDIT: Tested it in 1.63 and got the same result.
Edited on 10 July 2014 - 01:41 PM
Lyqyd #7
Posted 10 July 2014 - 03:53 PM
Are you testing on an advanced computer or a normal one? The multishell program may be part of the reason you're seeing redirection after a program exits.
0099 #8
Posted 10 July 2014 - 04:25 PM
Are you testing on an advanced computer or a normal one? The multishell program may be part of the reason you're seeing redirection after a program exits.

On a normal computer.