From b9cddb56094fff4e35dc40db722766b7f014201f Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Wed, 19 Mar 2014 17:13:20 +0100 Subject: [PATCH] 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 --- CHANGELOG.md | 4 ++++ lib/pry.rb | 1 + lib/pry/commands/change_inspector.rb | 27 ++++++++++++++++++++++ lib/pry/commands/list_inspectors.rb | 34 ++++++++++++++++++++++++++++ lib/pry/inspector.rb | 27 ++++++++++++++++++++++ 5 files changed, 93 insertions(+) create mode 100644 lib/pry/commands/change_inspector.rb create mode 100644 lib/pry/commands/list_inspectors.rb create mode 100644 lib/pry/inspector.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index fe825281..fba15027 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/lib/pry.rb b/lib/pry.rb index 589fe8b7..c417f3b2 100644 --- a/lib/pry.rb +++ b/lib/pry.rb @@ -168,3 +168,4 @@ require 'pry/rubygem' require "pry/indent" require "pry/last_exception" require "pry/prompt" +require "pry/inspector" diff --git a/lib/pry/commands/change_inspector.rb b/lib/pry/commands/change_inspector.rb new file mode 100644 index 00000000..a26e9f74 --- /dev/null +++ b/lib/pry/commands/change_inspector.rb @@ -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 diff --git a/lib/pry/commands/list_inspectors.rb b/lib/pry/commands/list_inspectors.rb new file mode 100644 index 00000000..facaf4ee --- /dev/null +++ b/lib/pry/commands/list_inspectors.rb @@ -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 diff --git a/lib/pry/inspector.rb b/lib/pry/inspector.rb new file mode 100644 index 00000000..2072c0fb --- /dev/null +++ b/lib/pry/inspector.rb @@ -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