Difference between revisions of "Term.current"

From ComputerCraft Wiki
Jump to: navigation, search
(Created term.current page)
 
(Elaboration on how term.current() serves to replace term.restore().)
Line 5: Line 5:
 
|api=term
 
|api=term
 
|addon=ComputerCraft
 
|addon=ComputerCraft
|desc=Returns the current terminal object that is being used to write to.
+
|desc=Added by ComputerCraft 1.6, this function returns the current terminal object that is being used to write to; if you intend to [[term.redirect|redirect]] elsewhere (eg to an attached [[Monitor]]), you may find need to record the identity of the previous object and redirect back to it when done.<br><br>
 +
 
 +
Previously, [[term.restore|term.restore()]] could be used to automatically revert back to the previously used output object - ComputerCraft 1.6 also removes that command.
 
|examples=
 
|examples=
 
{{Example
 
{{Example
|desc=Restores a program to the computer.  
+
|desc=Wraps a monitor at the top of the computer, then restores back to the previous display. Uses term.current() ''if available'', or [[term.restore|term.restore()]] if not.
This will only work if the computer was running a program on a monitor
+
|code= local mon = peripheral.wrap("top")
|code=term.redirect(peripheral.wrap('left')) -- switch to a monitor<br/>print('Hello!') -- write on the monitor<br/>print(term.current()) --returns the same as peripheral.wrap('left')
+
 +
-- If using a build of ComputerCraft with term.current, record the current terminal output object:
 +
if term.current then mon.restoreTo = term.current() end
 +
 +
-- Redirect the terminal to the attached monitor:
 +
term.redirect(mon)
 +
 +
-- Restore back:
 +
if term.current then
 +
term.redirect(mon.restoreTo)
 +
else
 +
term.restore()
 +
end
 
}}
 
}}
 
|notes=
 
|notes=
* [[term.current]]() requires ComputerCraft version 1.6 or newer.
+
* term.current() requires ComputerCraft version 1.6 or newer.
 
}}
 
}}
  
 
[[Category:API_Functions]]
 
[[Category:API_Functions]]

Revision as of 13:14, 31 March 2014


Grid Redstone.png  Function term.current
Added by ComputerCraft 1.6, this function returns the current terminal object that is being used to write to; if you intend to redirect elsewhere (eg to an attached Monitor), you may find need to record the identity of the previous object and redirect back to it when done.

Previously, term.restore() could be used to automatically revert back to the previously used output object - ComputerCraft 1.6 also removes that command.
Syntax term.current()
Returns table terminal object
Part of ComputerCraft
API term

Examples

Grid paper.png  Example
Wraps a monitor at the top of the computer, then restores back to the previous display. Uses term.current() if available, or term.restore() if not.
Code
local mon = peripheral.wrap("top")

-- If using a build of ComputerCraft with term.current, record the current terminal output object:
if term.current then mon.restoreTo = term.current() end

-- Redirect the terminal to the attached monitor:
term.redirect(mon)

-- Restore back:
if term.current then
	term.redirect(mon.restoreTo)
else
	term.restore()
end

Additional Notes

  • term.current() requires ComputerCraft version 1.6 or newer.