From e617c7d42027455e1f49a9be3c18d84eaf7ba536 Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Wed, 19 Mar 2014 14:06:26 +0100 Subject: [PATCH] add 'list-prompts' and 'change-prompt' commands. the list-prompts command shows a list of available prompts as well as the active prompt in the list. prompts are drawn with a name as well as a short description. change-prompt can be used to change the prompt by the name found in the list-prompts command. --- CHANGELOG.md | 4 ++++ lib/pry.rb | 1 + lib/pry/commands/change_prompt.rb | 24 ++++++++++++++++++++++ lib/pry/commands/list_prompts.rb | 33 +++++++++++++++++++++++++++++++ lib/pry/prompt.rb | 23 +++++++++++++++++++++ 5 files changed, 85 insertions(+) create mode 100644 lib/pry/commands/change_prompt.rb create mode 100644 lib/pry/commands/list_prompts.rb create mode 100644 lib/pry/prompt.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index 6653b80c..27859167 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -27,6 +27,10 @@ * Add option to disable input completer through `_pry_.config.completer = nil` * Add `Pry.main`. returns a special instance of Object referenced by self of `TOPLEVEL_BINDING`: "main". * Add `Pry::LastException` (#1145) +* Add `list-prompts` command. (#1175) + - lists the available prompts available for use. +* Add `change-prompt` command. (#1175) + - switch the current prompt, 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 13e0cc43..4685b926 100644 --- a/lib/pry.rb +++ b/lib/pry.rb @@ -174,3 +174,4 @@ require 'pry/editor' require 'pry/rubygem' require "pry/indent" require "pry/last_exception" +require "pry/prompt" diff --git a/lib/pry/commands/change_prompt.rb b/lib/pry/commands/change_prompt.rb new file mode 100644 index 00000000..f14a2642 --- /dev/null +++ b/lib/pry/commands/change_prompt.rb @@ -0,0 +1,24 @@ +class Pry::Command::ChangePrompt < Pry::ClassCommand + match 'change-prompt' + group 'prompts' + description 'change the current pry prompt' + command_options argument_required: true + banner <<-BANNER + Usage: change-prompt + change the current prompt. see list-prompts for a list of available prompts. + BANNER + + def process(prompt) + if prompt_map.key?(prompt) + _pry_.prompt = prompt_map[prompt][:value] + else + raise Pry::CommandError, "'#{prompt}' isn't a known prompt!" + end + end + +private + def prompt_map + Pry::Prompt::MAP + end + Pry::Commands.add_command(self) +end diff --git a/lib/pry/commands/list_prompts.rb b/lib/pry/commands/list_prompts.rb new file mode 100644 index 00000000..8e03711a --- /dev/null +++ b/lib/pry/commands/list_prompts.rb @@ -0,0 +1,33 @@ +class Pry::Command::ListPrompts < Pry::ClassCommand + match 'list-prompts' + group 'prompts' + description 'list the prompts available for use in pry' + banner <<-BANNER + Usage: list-prompts + list the prompts available for use in pry + BANNER + + def process + output.puts heading("Available prompts") + "\n" + prompt_map.each do |name, prompt| + output.write "name: #{text.bold(name)}" + output.puts selected_prompt?(prompt) ? selected_text : "" + output.puts prompt[:description] + output.puts + end + end + +private + def prompt_map + Pry::Prompt::MAP + end + + def selected_text + text.red " (selected) " + end + + def selected_prompt?(prompt) + _pry_.prompt == prompt[:value] + end + Pry::Commands.add_command(self) +end diff --git a/lib/pry/prompt.rb b/lib/pry/prompt.rb new file mode 100644 index 00000000..591e0a55 --- /dev/null +++ b/lib/pry/prompt.rb @@ -0,0 +1,23 @@ +class Pry::Prompt + MAP = { + "default" => { + value: Pry::DEFAULT_PROMPT, + description: "the default pry prompt" + }, + + "simple" => { + value: Pry::SIMPLE_PROMPT, + description: "a simple prompt" + }, + + "nav" => { + value: Pry::NAV_PROMPT, + description: "a prompt that draws the binding stack as a path and includes information about _in_ and _out_" + }, + + "none" => { + value: Pry::NO_PROMPT, + description: "wave goodbye to the pry prompt" + } + } +end