Advertisement
CloudCubed

Untitled

Jun 26th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local native = (term.native and term.native()) or term
  2. local redirectTarget = native
  3.  
  4. local function wrap( _sFunction )
  5.     return function( ... )
  6.         return redirectTarget[ _sFunction ]( ... )
  7.     end
  8. end
  9.  
  10. local term = {}
  11.  
  12. term.redirect = function( target )
  13.     if target == nil or type( target ) ~= "table" then
  14.         error( "Invalid redirect target", 2 )
  15.     end
  16.     if target == term then
  17.         error( "term is not a recommended redirect target, try term.current() instead", 2 )
  18.     end
  19.     for k,v in pairs( native ) do
  20.         if type( k ) == "string" and type( v ) == "function" then
  21.             if type( target[k] ) ~= "function" then
  22.                 target[k] = function()
  23.                     error( "Redirect object is missing method "..k..".", 2 )
  24.                 end
  25.             end
  26.         end
  27.     end
  28.     local oldRedirectTarget = redirectTarget
  29.     redirectTarget = target
  30.     return oldRedirectTarget
  31. end
  32.  
  33. term.current = function()
  34.     return redirectTarget
  35. end
  36.  
  37. term.native = function()
  38.     -- NOTE: please don't use this function unless you have to.
  39.     -- If you're running in a redirected or multitasked enviorment, term.native() will NOT be
  40.     -- the current terminal when your program starts up. It is far better to use term.current()
  41.     return native
  42. end
  43.  
  44. for k,v in pairs( native ) do
  45.     if type( k ) == "string" and type( v ) == "function" then
  46.         if term[k] == nil then
  47.             term[k] = wrap( k )
  48.         end
  49.     end
  50. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement