diff options
author | 0xAB <0xAB@protonmail.com> | 2017-08-28 05:10:41 +0100 |
---|---|---|
committer | 0xAB <0xAB@protonmail.com> | 2017-08-28 05:10:41 +0100 |
commit | 5f83fe196074b9b81b37133ae575cee5ff131a0a (patch) | |
tree | c110cfc71ff1a442be276dca4392a0349366a282 | |
parent | be61f06561e4578fbeba43d2c05e1bbd2be2c067 (diff) | |
download | pry-5f83fe196074b9b81b37133ae575cee5ff131a0a.tar.gz |
in case of multiple aliases referencing different prompts, provide
multiple choice from 'change-prompt' command.
-rw-r--r-- | lib/pry/commands/change_prompt.rb | 20 | ||||
-rw-r--r-- | lib/pry/prompt.rb | 12 |
2 files changed, 30 insertions, 2 deletions
diff --git a/lib/pry/commands/change_prompt.rb b/lib/pry/commands/change_prompt.rb index c6e72a20..36382d09 100644 --- a/lib/pry/commands/change_prompt.rb +++ b/lib/pry/commands/change_prompt.rb @@ -11,13 +11,29 @@ class Pry::Command::ChangePrompt < Pry::ClassCommand BANNER def process(prompt) - if new_prompt = Pry::Prompt[prompt] - _pry_.prompt = new_prompt.proc_array + prompts = Pry::Prompt.all(prompt) + if prompts.size == 1 + _pry_.prompt = prompts[0].proc_array + elsif prompts.size > 1 + multiple_choice(prompts) else raise Pry::CommandError, "'#{prompt}' isn't a known prompt!" end end private + def multiple_choice(prompts) + _pry_.pager.page "Multiple aliases found, please choose:\n" + prompts.each.with_index(1) do |prompt, i| + _pry_.pager.page "#{i}) #{prompt.name} => #{prompt.alias_for}" + end + output.write "Choice: " + reply = _pry_.input.respond_to?(:gets) ? _pry_.input.gets : _pry_.input.readline + if reply =~ /^\d+$/ and reply.to_i <= prompts.size + _pry_.prompt = prompts[reply.to_i-1].proc_array + else + multiple_choice(prompts) + end + end Pry::Commands.add_command(self) end diff --git a/lib/pry/prompt.rb b/lib/pry/prompt.rb index dcd2405a..7c3672c4 100644 --- a/lib/pry/prompt.rb +++ b/lib/pry/prompt.rb @@ -85,6 +85,18 @@ module Pry::Prompt end # + # @param [String] name + # The name of a prompt. + # + # @return [Array<PromptInfo>] + # An array of {PromptInfo} objects. + # + def all(name) + name = name.to_s + all_prompts.select{|prompt| prompt.name == name} + end + + # # Remove a prompt from Pry. # It will no longer be visible in the output of "list-prompts" or usable with the # "change-prompt" command. |