1
0
Fork 0
mirror of https://github.com/pry/pry.git synced 2022-11-09 12:35:05 -05:00
pry--pry/lib/pry/command_processor.rb

143 lines
4.6 KiB
Ruby
Raw Normal View History

require 'forwardable'
class Pry
class CommandProcessor
extend Forwardable
2011-04-18 17:31:39 -04:00
attr_accessor :pry_instance
2011-04-18 17:31:39 -04:00
def initialize(pry_instance)
@pry_instance = pry_instance
end
def_delegators :@pry_instance, :commands, :nesting, :output
2011-04-18 17:31:39 -04:00
# Is the string a valid command?
# @param [String] val The string passed in from the Pry prompt.
# @param [Binding] target The context where the string should be
# interpolated in.
# @return [Boolean] Whether the string is a valid command.
def valid_command?(val, target=binding)
!!(command_matched(val, target)[0])
end
# Convert the object to a form that can be interpolated into a
# Regexp cleanly.
# @return [String] The string to interpolate into a Regexp
def convert_to_regex(obj)
case obj
when String
Regexp.escape(obj)
else
obj
end
end
# Revaluate the string (str) and perform interpolation.
# @param [String] str The string to reevaluate with interpolation.
# @param [Binding] target The context where the string should be
# interpolated in.
# @return [String] The reevaluated string with interpolations
# applied (if any).
def interpolate_string(str, target)
dumped_str = str.dump
dumped_str.gsub!(/\\\#\{/, '#{')
target.eval(dumped_str)
end
# Determine whether a Pry command was matched and return command data
# and argument string.
# This method should not need to be invoked directly.
# @param [String] val The line of input.
# @param [Binding] target The binding to perform string
# interpolation against.
# @return [Array] The command data and arg string pair
def command_matched(val, target)
2011-06-05 18:48:15 -04:00
_, cmd_data = commands.commands.find do |name, data|
2011-07-26 03:34:54 -04:00
prefix = Regexp.escape(Pry.config.command_prefix)
prefix = "(?:#{prefix})?" unless data.options[:use_prefix]
2011-07-26 03:34:54 -04:00
command_regex = /^#{prefix}#{convert_to_regex(name)}(?!\S)/
2011-06-10 06:46:46 -04:00
if data.options[:interpolate]
# If interpolation fails then the command cannot be matched,
# so early exit.
begin
interp_val = interpolate_string(val, target)
rescue Exception
next
2011-06-10 06:46:46 -04:00
end
val.replace interp_val if command_regex =~ interp_val
else
command_regex =~ val
end
end
[cmd_data, (Regexp.last_match ? Regexp.last_match.captures : nil), (Regexp.last_match ? Regexp.last_match.end(0) : nil)]
end
# Process Pry commands. Pry commands are not Ruby methods and are evaluated
# prior to Ruby expressions.
# Commands can be modified/configured by the user: see `Pry::Commands`
# This method should not need to be invoked directly - it is called
# by `Pry#r`.
# @param [String] val The current line of input.
# @param [String] eval_string The cumulative lines of input for
# multi-line input.
# @param [Binding] target The receiver of the commands.
def process_commands(val, eval_string, target)
# no command was matched, so return to caller
command, captures, pos = command_matched(val, target)
return if !command
arg_string = val[pos..-1]
# remove the one leading space if it exists
arg_string.slice!(0) if arg_string.start_with?(" ")
args = arg_string ? Shellwords.shellwords(arg_string) : []
2011-04-18 17:31:39 -04:00
options = {
:val => val,
:arg_string => arg_string,
:eval_string => eval_string,
:nesting => nesting,
:commands => commands.commands,
:captures => captures
}
execute_command(target, command.name, options, *(captures + args))
end
# Execute a Pry command.
# This method should not need to be invoked directly.
# @param [Binding] target The target of the Pry session.
# @param [String] command The name of the command to be run.
# @param [Hash] options The options to set on the Commands object.
# @param [Array] args The command arguments.
def execute_command(target, command, options, *args)
context = CommandContext.new
# set some useful methods to be used by the action blocks
2011-04-25 17:13:36 -04:00
context.opts = options
context.target = target
context.output = output
context.captures = options[:captures]
context.eval_string = options[:eval_string]
context.arg_string = options[:arg_string]
2011-04-25 17:13:36 -04:00
context.command_set = commands
context.command_processor = self
ret = commands.run_command(context, command, *args)
added highlighting to disabled commands in GNU bash, version 4.1.5(1)-release (i486-pc-linux-gnu) These shell commands are defined internally. Type `help' to see this list. Type `help name' to find out more about the function `name'. Use `info bash' to find out more about the shell in general. Use `man -k' or `info' to find out more about commands not in this list. A star (*) next to a name means that the command is disabled. job_spec [&] history [-c] [-d offset] [n] or history -anrw [filename] o> (( expression )) if COMMANDS; then COMMANDS; [ elif COMMANDS; then COMMANDS> . filename [arguments] jobs [-lnprs] [jobspec ...] or jobs -x command [args] : kill [-s sigspec | -n signum | -sigspec] pid | jobspec ...> [ arg... ] let arg [arg ...] [[ expression ]] local [option] name[=value] ... alias [-p] [name[=value] ... ] logout [n] bg [job_spec ...] mapfile [-n count] [-O origin] [-s count] [-t] [-u fd] [-C> bind [-lpvsPVS] [-m keymap] [-f filename] [-q name] [-u nam> popd [-n] [+N | -N] break [n] printf [-v var] format [arguments] builtin [shell-builtin [arg ...]] pushd [-n] [+N | -N | dir] caller [expr] pwd [-LP] case WORD in [PATTERN [| PATTERN]...) COMMANDS ;;]... esac read [-ers] [-a array] [-d delim] [-i text] [-n nchars] [-> cd [-L|-P] [dir] readarray [-n count] [-O origin] [-s count] [-t] [-u fd] [> command [-pVv] command [arg ...] readonly [-af] [name[=value] ...] or readonly -p compgen [-abcdefgjksuv] [-o option] [-A action] [-G globpa> return [n] complete [-abcdefgjksuv] [-pr] [-DE] [-o option] [-A action> select NAME [in WORDS ... ;] do COMMANDS; done compopt [-o|+o option] [-DE] [name ...] set [--abefhkmnptuvxBCHP] [-o option-name] [arg ...] continue [n] shift [n] coproc [NAME] command [redirections] shopt [-pqsu] [-o] [optname ...] declare [-aAfFilrtux] [-p] [name[=value] ...] source filename [arguments] dirs [-clpv] [+N] [-N] suspend [-f] disown [-h] [-ar] [jobspec ...] test [expr] echo [-neE] [arg ...] time [-p] pipeline enable [-a] [-dnps] [-f filename] [name ...] times eval [arg ...] trap [-lp] [[arg] signal_spec ...] exec [-cl] [-a name] [command [arguments ...]] [redirection> true exit [n] type [-afptP] name [name ...] export [-fn] [name[=value] ...] or export -p typeset [-aAfFilrtux] [-p] name[=value] ... false ulimit [-SHacdefilmnpqrstuvx] [limit] fc [-e ename] [-lnr] [first] [last] or fc -s [pat=rep] [com> umask [-p] [-S] [mode] fg [job_spec] unalias [-a] name [name ...] for NAME [in WORDS ... ] ; do COMMANDS; done unset [-f] [-v] [name ...] for (( exp1; exp2; exp3 )); do COMMANDS; done until COMMANDS; do COMMANDS; done function name { COMMANDS ; } or name () { COMMANDS ; } variables - Names and meanings of some shell variables getopts optstring name [arg] wait [id] hash [-lr] [-p pathname] [-dt] [name ...] while COMMANDS; do COMMANDS; done help [-dms] [pattern ...] { COMMANDS ; }
2011-04-13 07:49:45 -04:00
# Tick, tock, im getting rid of this shit soon.
options[:val].replace("")
2011-04-18 17:31:39 -04:00
ret
end
end
end