class MindControl::REPL

Pry based REPL.

Public Class Methods

new( target, options = {} ) click to toggle source

@param [Object, Proc] target The receiver of the Pry session. @param [Hash] options The optional configuration parameters for Pry.

# File lib/mind_control/repl.rb, line 16
def initialize( target, options = {} )
  @target  = target
  @options = options
end

Public Instance Methods

start( stdin, stdout ) click to toggle source

Start REPL session.

@param [IO] stdin The object to use for input. @param [IO] stdout The object to use for output.

# File lib/mind_control/repl.rb, line 27
def start( stdin, stdout )
  # NB: We cannot use Readline, because it always uses STDOUT / STDIN.
  input = CoollineAdapter.new( stdin, stdout )

  # Default command set
  commands = @options[ :commands ] || ::Pry::CommandSet.new.import( ::Pry::Commands )

  # Import our MindControl commands
  commands.import MindControl::PryCommands

  # Target can be callable
  target = @target.respond_to?( :call ) ? @target.call : @target

  # NB: input/input can't be changed via pry options!
  pry = ::Pry.new @options.merge( :commands => commands, :input => input, :output => stdout )

  # Store pry instance in thread-local context so that we can later determine
  # whether we are running inside MindControl session or not.
  ::Pry.current[ :mind_control_pry_instance ] = pry

  # Start session
  pry.repl target
end