Class: Pry::Command
- Extended by:
- Pry::CodeObject::Helpers, Helpers::DocumentationHelpers
- Includes:
- Helpers::BaseHelpers, Helpers::CommandHelpers, Helpers::Text
- Defined in:
- lib/pry/command.rb,
lib/pry/commands/cd.rb,
lib/pry/commands/ls.rb,
lib/pry/commands/ri.rb,
lib/pry/commands/cat.rb,
lib/pry/commands/wtf.rb,
lib/pry/commands/bang.rb,
lib/pry/commands/edit.rb,
lib/pry/commands/exit.rb,
lib/pry/commands/help.rb,
lib/pry/commands/hist.rb,
lib/pry/commands/play.rb,
lib/pry/commands/stat.rb,
lib/pry/commands/reset.rb,
lib/pry/commands/jump_to.rb,
lib/pry/commands/ls/grep.rb,
lib/pry/commands/nesting.rb,
lib/pry/commands/bang_pry.rb,
lib/pry/commands/exit_all.rb,
lib/pry/commands/raise_up.rb,
lib/pry/commands/show_doc.rb,
lib/pry/commands/whereami.rb,
lib/pry/commands/ls/config.rb,
lib/pry/commands/save_file.rb,
lib/pry/commands/show_info.rb,
lib/pry/commands/switch_to.rb,
lib/pry/commands/amend_line.rb,
lib/pry/commands/fix_indent.rb,
lib/pry/commands/import_set.rb,
lib/pry/commands/ls/globals.rb,
lib/pry/commands/ls/methods.rb,
lib/pry/commands/shell_mode.rb,
lib/pry/commands/show_input.rb,
lib/pry/commands/disable_pry.rb,
lib/pry/commands/find_method.rb,
lib/pry/commands/pry_version.rb,
lib/pry/commands/reload_code.rb,
lib/pry/commands/show_source.rb,
lib/pry/commands/clear_screen.rb,
lib/pry/commands/exit_program.rb,
lib/pry/commands/ls/constants.rb,
lib/pry/commands/ls/formatter.rb,
lib/pry/commands/ls/ls_entity.rb,
lib/pry/commands/toggle_color.rb,
lib/pry/commands/change_prompt.rb,
lib/pry/commands/ls/local_vars.rb,
lib/pry/commands/pry_backtrace.rb,
lib/pry/commands/shell_command.rb,
lib/pry/commands/code_collector.rb,
lib/pry/commands/ls/jruby_hacks.rb,
lib/pry/commands/ls/local_names.rb,
lib/pry/commands/list_inspectors.rb,
lib/pry/commands/ls/self_methods.rb,
lib/pry/commands/change_inspector.rb,
lib/pry/commands/ls/instance_vars.rb,
lib/pry/commands/watch_expression.rb,
lib/pry/commands/ls/interrogatable.rb,
lib/pry/commands/ls/methods_helper.rb,
lib/pry/commands/cat/file_formatter.rb,
lib/pry/commands/cat/abstract_formatter.rb,
lib/pry/commands/edit/exception_patcher.rb,
lib/pry/commands/cat/exception_formatter.rb,
lib/pry/commands/edit/file_and_line_locator.rb,
lib/pry/commands/watch_expression/expression.rb,
lib/pry/commands/cat/input_expression_formatter.rb
Overview
N.B. using a regular expression here so that “raise-up ‘foo’” does the right thing.
Direct Known Subclasses
Defined Under Namespace
Classes: AmendLine, Bang, BangPry, Cat, Cd, ChangeInspector, ChangePrompt, ClearScreen, CodeCollector, DisablePry, Edit, Exit, ExitAll, ExitProgram, FindMethod, FixIndent, Help, Hist, ImportSet, JumpTo, ListInspectors, Ls, Nesting, Play, PryBacktrace, RaiseUp, ReloadCode, Reset, Ri, SaveFile, ShellCommand, ShellMode, ShowDoc, ShowInfo, ShowInput, ShowSource, Stat, SwitchTo, ToggleColor, Version, WatchExpression, Whereami, Wtf
Constant Summary collapse
- VOID_VALUE =
represents a void return value for a command
Object.new
Constants included from Helpers::DocumentationHelpers
Helpers::DocumentationHelpers::YARD_TAGS
Constants included from Helpers::Text
Class Attribute Summary collapse
- .block ⇒ Object
-
.command_options(arg = nil) ⇒ Object
Define or get the command’s options.
-
.description(arg = nil) ⇒ Object
Define or get the command’s description.
- .match(arg = nil) ⇒ Object
Instance Attribute Summary collapse
-
#arg_string ⇒ Object
Returns the value of attribute arg_string.
-
#captures ⇒ Object
Returns the value of attribute captures.
-
#command_block ⇒ Object
The block we pass into a command so long as ‘:takes_block` is not equal to `false`.
-
#command_set ⇒ Object
Returns the value of attribute command_set.
-
#context ⇒ Object
Returns the value of attribute context.
-
#eval_string ⇒ Object
Returns the value of attribute eval_string.
-
#hooks ⇒ Object
Returns the value of attribute hooks.
-
#output ⇒ Object
Properties of one execution of a command (passed by #run_command as a hash of context and expanded in ‘#initialize`.
-
#pry_instance ⇒ Object
Returns the value of attribute pry_instance.
-
#target ⇒ Object
Returns the value of attribute target.
Class Method Summary collapse
-
.banner(arg = nil) ⇒ Object
Define or get the command’s banner.
- .command_name ⇒ Object
- .command_regex ⇒ Object
- .convert_to_regex(obj) ⇒ Object
- .default_options(match) ⇒ Object
- .doc ⇒ Object
-
.group(name = nil) ⇒ Object
The group in which the command should be displayed in “help” output.
- .inspect ⇒ Object
-
.match_score(val) ⇒ Fixnum
How well does this command match the given line?.
-
.matches?(val) ⇒ Boolean
Should this command be called for the given line?.
- .name ⇒ Object
-
.options ⇒ Object
Define or get the command’s options backward compatibility.
- .source ⇒ Object
- .source_file ⇒ Object (also: file)
- .source_line ⇒ Object (also: line)
- .state ⇒ Object
-
.subclass(match, description, options, helpers) { ... } ⇒ Class
Create a new command with the given properties.
Instance Method Summary collapse
- #_pry_ ⇒ Object
- #block ⇒ Object
-
#check_for_command_collision(command_match, arg_string) ⇒ Object
Display a warning if a command collides with a local/method in the current scope.
- #command_name ⇒ Object
- #command_options ⇒ Object
- #commands ⇒ Object
-
#complete(_search) ⇒ Array<String>
Generate completions for this command.
- #description ⇒ Object
-
#initialize(context = {}) ⇒ Command
constructor
Instantiate a command, in preparation for calling it.
-
#interpolate_string(str) ⇒ String
Revaluate the string (str) and perform interpolation.
- #match ⇒ Object
-
#name ⇒ Object
Make those properties accessible to instances.
-
#process_line(line) ⇒ Object, Command::VOID_VALUE
Process a line that Command.matches? this command.
-
#run(command_string, *args) ⇒ Object
Run a command from another command.
- #source ⇒ Object
-
#state ⇒ Hash
Pry commands can store arbitrary state here.
-
#target_self ⇒ Object
The value of ‘self` inside the `target` binding.
-
#tokenize(val) ⇒ Array
Extract necessary information from a line that Command.matches? this command.
- #void ⇒ Object
Methods included from Helpers::DocumentationHelpers
get_comment_content, process_comment_markup, process_rdoc, process_yardoc, process_yardoc_tag, strip_comments_from_c_code, strip_leading_whitespace
Methods included from Pry::CodeObject::Helpers
c_method?, c_module?, command?, module_with_yard_docs?, real_method_object?
Methods included from Helpers::Text
#bold, #default, #indent, #no_color, #no_pager, #strip_color, #with_line_numbers
Methods included from Helpers::CommandHelpers
#absolute_index_number, #absolute_index_range, #get_method_or_raise, #internal_binding?, #one_index_number, #one_index_range, #one_index_range_or_number, #restrict_to_lines, #set_file_and_dir_locals, #temp_file, #unindent
Methods included from Helpers::OptionsHelpers
Methods included from Helpers::BaseHelpers
#colorize_code, #find_command, #heading, #highlight, #not_a_real_file?, #safe_send, #silence_warnings, #stagger_output, #use_ansi_codes?
Constructor Details
#initialize(context = {}) ⇒ Command
Instantiate a command, in preparation for calling it.
230 231 232 233 234 235 236 237 238 |
# File 'lib/pry/command.rb', line 230 def initialize(context = {}) self.context = context self.target = context[:target] self.output = context[:output] self.eval_string = context[:eval_string] self.command_set = context[:command_set] self.hooks = context[:hooks] self.pry_instance = context[:pry_instance] end |
Class Attribute Details
.block ⇒ Object
66 67 68 |
# File 'lib/pry/command.rb', line 66 def block @block || instance_method(:process) end |
.command_options(arg = nil) ⇒ Object
Define or get the command’s options
51 52 53 54 55 |
# File 'lib/pry/command.rb', line 51 def (arg = nil) @command_options ||= (match) @command_options.merge!(arg) if arg @command_options end |
.description(arg = nil) ⇒ Object
Define or get the command’s description
45 46 47 48 |
# File 'lib/pry/command.rb', line 45 def description(arg = nil) @description = arg if arg @description ||= nil end |
.match(arg = nil) ⇒ Object
35 36 37 38 39 40 41 42 |
# File 'lib/pry/command.rb', line 35 def match(arg = nil) if arg @command_options ||= (arg) @command_options[:listing] = arg.is_a?(String) ? arg : arg.inspect @match = arg end @match ||= nil end |
Instance Attribute Details
#arg_string ⇒ Object
Returns the value of attribute arg_string.
213 214 215 |
# File 'lib/pry/command.rb', line 213 def arg_string @arg_string end |
#captures ⇒ Object
Returns the value of attribute captures.
211 212 213 |
# File 'lib/pry/command.rb', line 211 def captures @captures end |
#command_block ⇒ Object
The block we pass into a command so long as ‘:takes_block` is not equal to `false`
226 227 228 |
# File 'lib/pry/command.rb', line 226 def command_block @command_block end |
#command_set ⇒ Object
Returns the value of attribute command_set.
215 216 217 |
# File 'lib/pry/command.rb', line 215 def command_set @command_set end |
#context ⇒ Object
Returns the value of attribute context.
214 215 216 |
# File 'lib/pry/command.rb', line 214 def context @context end |
#eval_string ⇒ Object
Returns the value of attribute eval_string.
212 213 214 |
# File 'lib/pry/command.rb', line 212 def eval_string @eval_string end |
#hooks ⇒ Object
Returns the value of attribute hooks.
216 217 218 |
# File 'lib/pry/command.rb', line 216 def hooks @hooks end |
#output ⇒ Object
Properties of one execution of a command (passed by Pry#run_command as a hash of context and expanded in ‘#initialize`
209 210 211 |
# File 'lib/pry/command.rb', line 209 def output @output end |
#pry_instance ⇒ Object
Returns the value of attribute pry_instance.
217 218 219 |
# File 'lib/pry/command.rb', line 217 def pry_instance @pry_instance end |
#target ⇒ Object
Returns the value of attribute target.
210 211 212 |
# File 'lib/pry/command.rb', line 210 def target @target end |
Class Method Details
.banner(arg = nil) ⇒ Object
Define or get the command’s banner
61 62 63 64 |
# File 'lib/pry/command.rb', line 61 def (arg = nil) @banner = arg if arg @banner ||= description end |
.command_name ⇒ Object
109 110 111 |
# File 'lib/pry/command.rb', line 109 def command_name [:listing] end |
.command_regex ⇒ Object
165 166 167 168 169 170 |
# File 'lib/pry/command.rb', line 165 def command_regex prefix = convert_to_regex(Pry.config.command_prefix) prefix = "(?:#{prefix})?" unless [:use_prefix] /\A#{prefix}#{convert_to_regex(match)}(?!\S)/ end |
.convert_to_regex(obj) ⇒ Object
172 173 174 175 176 177 178 179 |
# File 'lib/pry/command.rb', line 172 def convert_to_regex(obj) case obj when String Regexp.escape(obj) else obj end end |
.default_options(match) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/pry/command.rb', line 89 def (match) { keep_retval: false, argument_required: false, interpolate: true, shellwords: true, listing: (match.is_a?(String) ? match : match.inspect), use_prefix: true, takes_block: false } end |
.doc ⇒ Object
75 76 77 |
# File 'lib/pry/command.rb', line 75 def doc new.help end |
.group(name = nil) ⇒ Object
The group in which the command should be displayed in “help” output. This is usually auto-generated from directory naming, but it can be manually overridden if necessary. Group should not be changed once it is initialized.
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/pry/command.rb', line 185 def group(name = nil) @group ||= begin name || case Pry::Method(block).source_file when %r{/pry/.*_commands/(.*).rb} Regexp.last_match(1).capitalize.tr('_', " ") when /(pry-\w+)-([\d\.]+([\w\.]+)?)/ name = Regexp.last_match(1) version = Regexp.last_match(2) "#{name} (v#{version})" when /pryrc/ "pryrc" else "(other)" end end end |
.inspect ⇒ Object
105 106 107 |
# File 'lib/pry/command.rb', line 105 def inspect name end |
.match_score(val) ⇒ Fixnum
How well does this command match the given line?
Higher scores are better because they imply that this command matches the line more closely.
The score is calculated by taking the number of characters at the start of the string that are used only to identify the command, not as part of the arguments.
153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/pry/command.rb', line 153 def match_score(val) if command_regex =~ val if Regexp.last_match.size > 1 Regexp.last_match.begin(1) else Regexp.last_match.end(0) end else -1 end end |
.matches?(val) ⇒ Boolean
Should this command be called for the given line?
133 134 135 |
# File 'lib/pry/command.rb', line 133 def matches?(val) command_regex =~ val end |
.name ⇒ Object
101 102 103 |
# File 'lib/pry/command.rb', line 101 def name super.to_s == "" ? "#<class(Pry::Command #{match.inspect})>" : super end |
.options ⇒ Object
Define or get the command’s options backward compatibility
57 58 59 60 61 |
# File 'lib/pry/command.rb', line 57 def (arg = nil) @command_options ||= (match) @command_options.merge!(arg) if arg @command_options end |
.source ⇒ Object
70 71 72 73 |
# File 'lib/pry/command.rb', line 70 def source file, line = block.source_location strip_leading_whitespace(Pry::Code.from_file(file).expression_at(line)) end |
.source_file ⇒ Object Also known as: file
79 80 81 |
# File 'lib/pry/command.rb', line 79 def source_file Array(block.source_location).first end |
.source_line ⇒ Object Also known as: line
84 85 86 |
# File 'lib/pry/command.rb', line 84 def source_line Array(block.source_location).last end |
.state ⇒ Object
202 203 204 |
# File 'lib/pry/command.rb', line 202 def state Pry::CommandState.default.state_for(self) end |
.subclass(match, description, options, helpers) { ... } ⇒ Class
Create a new command with the given properties.
120 121 122 123 124 125 126 127 128 |
# File 'lib/pry/command.rb', line 120 def subclass(match, description, , helpers, &block) klass = Class.new(self) klass.send(:include, helpers) klass.match = match klass.description = description klass. = klass.block = block klass end |
Instance Method Details
#_pry_ ⇒ Object
292 293 294 295 |
# File 'lib/pry/command.rb', line 292 def _pry_ Pry::Warning.warn('_pry_ is deprecated, use pry_instance instead') pry_instance end |
#block ⇒ Object
253 254 255 |
# File 'lib/pry/command.rb', line 253 def block self.class.block end |
#check_for_command_collision(command_match, arg_string) ⇒ Object
Display a warning if a command collides with a local/method in the current scope.
329 330 331 332 333 334 335 336 337 338 339 340 |
# File 'lib/pry/command.rb', line 329 def check_for_command_collision(command_match, arg_string) collision_type = target.eval("defined?(#{command_match})") collision_type ||= 'local-variable' if arg_string =~ %r{\A\s*[-+*/%&|^]*=} if collision_type output.puts( "#{Helpers::Text.bold('WARNING:')} Calling Pry command '#{command_match}', " \ "which conflicts with a #{collision_type}.\n\n" ) end rescue Pry::RescuableException # rubocop:disable Lint/HandleExceptions end |
#command_name ⇒ Object
261 262 263 |
# File 'lib/pry/command.rb', line 261 def command_name self.class.command_name end |
#command_options ⇒ Object
257 258 259 |
# File 'lib/pry/command.rb', line 257 def self.class. end |
#commands ⇒ Object
284 285 286 |
# File 'lib/pry/command.rb', line 284 def commands command_set.to_hash end |
#complete(_search) ⇒ Array<String>
Generate completions for this command
411 412 413 |
# File 'lib/pry/command.rb', line 411 def complete(_search) [] end |
#description ⇒ Object
249 250 251 |
# File 'lib/pry/command.rb', line 249 def description self.class.description end |
#interpolate_string(str) ⇒ String
Revaluate the string (str) and perform interpolation.
318 319 320 321 322 323 324 325 |
# File 'lib/pry/command.rb', line 318 def interpolate_string(str) dumped_str = str.dump if dumped_str.gsub!(/\\\#\{/, '#{') target.eval(dumped_str) else str end end |
#match ⇒ Object
245 246 247 |
# File 'lib/pry/command.rb', line 245 def match self.class.match end |
#name ⇒ Object
Make those properties accessible to instances
241 242 243 |
# File 'lib/pry/command.rb', line 241 def name self.class.name end |
#process_line(line) ⇒ Object, Command::VOID_VALUE
Process a line that Command.matches? this command.
394 395 396 397 398 399 400 401 402 403 404 405 |
# File 'lib/pry/command.rb', line 394 def process_line(line) command_match, arg_string, captures, args = tokenize(line) if Pry.config.collision_warning check_for_command_collision(command_match, arg_string) end self.arg_string = arg_string self.captures = captures call_safely(*(captures + args)) end |
#run(command_string, *args) ⇒ Object
Run a command from another command.
278 279 280 281 282 |
# File 'lib/pry/command.rb', line 278 def run(command_string, *args) command_string = pry_instance.config.command_prefix.to_s + command_string complete_string = "#{command_string} #{args.join(' ')}".rstrip command_set.process_line(complete_string, context) end |
#source ⇒ Object
265 266 267 |
# File 'lib/pry/command.rb', line 265 def source self.class.source end |
#state ⇒ Hash
Returns Pry commands can store arbitrary state here. This state persists between subsequent command invocations. All state saved here is unique to the command, it does not need to be namespaced.
309 310 311 |
# File 'lib/pry/command.rb', line 309 def state self.class.state end |
#target_self ⇒ Object
Returns The value of ‘self` inside the `target` binding.
298 299 300 |
# File 'lib/pry/command.rb', line 298 def target_self target.eval('self') end |
#tokenize(val) ⇒ Array
Extract necessary information from a line that Command.matches? this command.
Returns an array of four elements:
“‘
[String] the portion of the line that matched with the Command match
[String] a string of all the arguments (i.e. everything but the match)
[Array] the captures caught by the command_regex
[Array] the arguments obtained by splitting the arg_string
“‘
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 |
# File 'lib/pry/command.rb', line 356 def tokenize(val) val = interpolate_string(val) if [:interpolate] self.class.command_regex =~ val # please call Command.matches? before Command#call_safely unless Regexp.last_match raise CommandError, "fatal: called a command which didn't match?!" end captures = Regexp.last_match.captures pos = Regexp.last_match.end(0) arg_string = val[pos..-1] # remove the one leading space if it exists arg_string.slice!(0) if arg_string.start_with?(" ") # process and pass a block if one is found pass_block(arg_string) if [:takes_block] args = if arg_string if [:shellwords] Shellwords.shellwords(arg_string) else arg_string.split(" ") end else [] end [val[0..pos].rstrip, arg_string, captures, args] end |
#void ⇒ Object
288 289 290 |
# File 'lib/pry/command.rb', line 288 def void VOID_VALUE end |