class Pry::CommandProcessor
def command_matched(val)
-
(Array)
- The command data and arg string pair
Parameters:
-
val
(String
) -- The line of input.
def command_matched(val) _, cmd_data = commands.commands.find do |name, cmd_data| /^#{Regexp.escape(name)}(?!\S)(?:\s+(.+))?/ =~ val end [cmd_data, $1] end
def execute_command(target, action, options, *args)
-
args
(Array
) -- The command arguments. -
options
(Hash
) -- The options to set on the Commands object. -
action
(Proc
) -- The proc that implements the command. -
target
(Binding
) -- The target of the Pry session.
def execute_command(target, action, options, *args) # set some useful methods to be used by the action blocks commands.opts = options commands.target = target commands.output = output case action.arity <=> 0 when -1 # Use instance_exec() to make the `opts` method, etc available ret_val = commands.instance_exec(*args, &action) when 1, 0 # ensure that we get the right number of parameters # since 1.8.7 complains about incorrect arity (1.9.2 # doesn't care) args_with_corrected_arity = args.values_at *0..(action.arity - 1) ret_val = commands.instance_exec(*args_with_corrected_arity, &action) end # Tick, tock, im getting rid of this shit soon. options[:val].clear ret_val end
def execute_system_command(val, target)
-
target
(Binding
) -- The context in which to perform string interpolation. -
val
(String
) -- The system command to execute.
def execute_system_command(val, target) SYSTEM_COMMAND_REGEX =~ val cmd = interpolate_string($1, target) if cmd =~ /^cd\s+(.+)/i begin Dir.chdir(File.expand_path($1)) rescue Errno::ENOENT output.puts "No such directory: #{$1}" end else system(cmd) end # Tick, tock, im getting rid of this shit soon. val.replace("") end
def initialize(pry_instance)
def initialize(pry_instance) @pry_instance = pry_instance end
def interpolate_string(str, target)
-
(String)
- The reevaluated string with interpolations
Parameters:
-
target
(Binding
) -- The context where the string should be -
str
(String
) -- The string to reevaluate with interpolation.
def interpolate_string(str, target) dumped_str = str.dump dumped_str.gsub!(/\\\#\{/, '#{') target.eval(dumped_str) end
def process_commands(val, eval_string, target)
-
target
(Binding
) -- The receiver of the commands. -
eval_string
(String
) -- The cumulative lines of input for -
val
(String
) -- The current line of input.
def process_commands(val, eval_string, target) def val.clear() replace("") end def eval_string.clear() replace("") end if system_command?(val) execute_system_command(val, target) return end # no command was matched, so return to caller return if !pry_command?(val) val.replace interpolate_string(val, target) cmd_data, args_string = command_matched(val) args = args_string ? Shellwords.shellwords(args_string) : [] action = cmd_data[:action] keep_retval = cmd_data[:keep_retval] options = { :val => val, :eval_string => eval_string, :nesting => nesting, :commands => commands.commands } ret_value = execute_command(target, action, options, *args) # return value of block only if :keep_retval is true ret_value if keep_retval end
def pry_command?(val)
-
(Boolean)
- Whether the string is a valid Pry command.
Parameters:
-
val
(String
) -- The string passed in from the Pry prompt.
def pry_command?(val) !!command_matched(val).first end
def system_command?(val)
-
(Boolean)
- Whether the string is a valid system command.
Parameters:
-
val
(String
) -- The string passed in from the Pry prompt.
def system_command?(val) !!(SYSTEM_COMMAND_REGEX =~ val) end
def valid_command?(val)
-
(Boolean)
- Whether the string is a valid command.
Parameters:
-
val
(String
) -- The string passed in from the Pry prompt.
def valid_command?(val) system_command?(val) || pry_command?(val) end