add 'list-inspectors' & 'change-inspectors' commands.

the list-inspectors command shows a list of available inspectors as well
as the active inspectors in the list. inspectors are drawn with a name as
well as a short description.

change-inspector can be used to change the inspector by the name found in
the list-inspectors command.

an inspector in pry is something that prints a return value in a repl session.
i'm not crazy about the name `inspector` but the traditional name used in pry(`print`)
didn't fit well. some discussion led us to `inspector` for lack of a better word,
see #1176 on github for discussion
This commit is contained in:
Robert Gleeson 2014-03-19 17:13:20 +01:00
parent f8dc3ed53e
commit b9cddb5609
5 changed files with 93 additions and 0 deletions

View File

@ -32,6 +32,10 @@
- lists the available prompts available for use.
* Add `change-prompt` command. (#1175)
- switch the current prompt, by name.
* Add `list-inspectors` command. (#1176)
- list the inspectors available to print ruby return values in a repl.
* Add `change-inspector` command. (#1176)
- switch the current inspector, by name.
#### Bug fixes, etc.
* Move `Pry::BondCompleter` and bond integration to the [pry-bond](https://github.com/pry/pry-bond) rubygem. (#1166)

View File

@ -168,3 +168,4 @@ require 'pry/rubygem'
require "pry/indent"
require "pry/last_exception"
require "pry/prompt"
require "pry/inspector"

View File

@ -0,0 +1,27 @@
class Pry::Command::ChangeInspector < Pry::ClassCommand
match 'change-inspector'
group 'inspect'
description 'change the current pry inspector'
command_options argument_required: true
banner <<-BANNER
Usage: change-inspector
change the Proc to used to print return values in a repl session.
see list-inspectors for a list of available Proc's and a short description
of what they do.
BANNER
def process(inspector)
if inspector_map.key?(inspector)
_pry_.print = inspector_map[inspector][:value]
output.puts "switched to the '#{inspector}' inspector!"
else
raise Pry::CommandError, "'#{inspector}' isn't a known inspector!"
end
end
private
def inspector_map
Pry::Inspector::MAP
end
Pry::Commands.add_command(self)
end

View File

@ -0,0 +1,34 @@
class Pry::Command::ListInspectors < Pry::ClassCommand
match 'list-inspectors'
group 'inspect'
description 'list the inspector Procs available to use in pry'
banner <<-BANNER
Usage: list-inspectors
list the inspector Proc's available to print ruby objects(e.g: return values) in
a repl session.
BANNER
def process
output.puts heading("Available inspectors") + "\n"
inspector_map.each do |name, inspector|
output.write "name: #{text.bold(name)}"
output.puts selected_inspector?(inspector) ? selected_text : ""
output.puts inspector[:description]
output.puts
end
end
private
def inspector_map
Pry::Inspector::MAP
end
def selected_text
text.red " (selected) "
end
def selected_inspector?(inspector)
_pry_.print == inspector[:value]
end
Pry::Commands.add_command(self)
end

27
lib/pry/inspector.rb Normal file
View File

@ -0,0 +1,27 @@
class Pry::Inspector
MAP = {
"default" => {
value: Pry::DEFAULT_PRINT,
description: <<-DESCRIPTION.each_line.map(&:lstrip!)
the default pry inspector. it has paging and color support, and uses pretty_inspect
when printing an object.
DESCRIPTION
},
"simple" => {
value: Pry::SIMPLE_PRINT,
description: <<-DESCRIPTION.each_line.map(&:lstrip)
a simple inspector that uses #puts and #inspect when printing an object.
it has no pager, color, or pretty_inspect support.
DESCRIPTION
},
"clipped" => {
value: Pry::CLIPPED_PRINT,
description: <<-DESCRIPTION.each_line.map(&:lstrip)
the clipped inspector has the same features as the 'simple' inspector but prints
large objects as a smaller string.
DESCRIPTION
}
}
end