diff options
author | Kyrylo Silin <silin@kyrylo.org> | 2018-11-04 05:16:32 +0800 |
---|---|---|
committer | Kyrylo Silin <silin@kyrylo.org> | 2018-11-04 05:16:32 +0800 |
commit | 60f20f8857caf2633e631cae274ad302daeee1d0 (patch) | |
tree | 21cd26449dbc25398e7760c84dee1075021f5a29 | |
parent | becba12b65403cbcaa577a8b4a4431ce390b8244 (diff) | |
download | pry-60f20f8857caf2633e631cae274ad302daeee1d0.tar.gz |
commands/change_prompt: incorporate 'list_prompts' functionality
Fixes #1829 (Merge `list-prompts` and `change-prompt` into one command)
-rw-r--r-- | lib/pry/commands/change_prompt.rb | 32 | ||||
-rw-r--r-- | lib/pry/commands/list_prompts.rb | 32 | ||||
-rw-r--r-- | lib/pry/prompt.rb | 15 |
3 files changed, 35 insertions, 44 deletions
diff --git a/lib/pry/commands/change_prompt.rb b/lib/pry/commands/change_prompt.rb index f20b741a..746cbbc3 100644 --- a/lib/pry/commands/change_prompt.rb +++ b/lib/pry/commands/change_prompt.rb @@ -4,17 +4,41 @@ class Pry::Command::ChangePrompt < Pry::ClassCommand description 'Change the current prompt.' command_options argument_required: true banner <<-BANNER - Usage: change-prompt NAME + Usage: change-prompt [OPTIONS] [NAME] - Change the current prompt. See list-prompts for a list of available - prompts. + Change the current prompt. BANNER + def options(opt) + opt.on(:l, :list, 'List the available prompts') + end + def process(prompt) + if opts.present?(:l) + list_prompts + else + change_prompt(prompt) + end + end + + private + + def list_prompts + prompts = Pry::Prompt.all.map do |name, prompt| + "#{bold(name)}#{red(' (selected)') if _pry_.prompt == prompt[:value]}\n" + + prompt[:description] + end + output.puts(prompts.join("\n")) + end + + def change_prompt(prompt) if Pry::Prompt.all.key?(prompt) _pry_.prompt = Pry::Prompt.all[prompt][:value] else - raise Pry::CommandError, "'#{prompt}' isn't a known prompt!" + raise(Pry::CommandError, <<MSG) +'#{prompt}' isn't a known prompt. Run `change-prompt --list` to see +the list of known prompts. +MSG end end diff --git a/lib/pry/commands/list_prompts.rb b/lib/pry/commands/list_prompts.rb deleted file mode 100644 index 9fe2cad3..00000000 --- a/lib/pry/commands/list_prompts.rb +++ /dev/null @@ -1,32 +0,0 @@ -class Pry::Command::ListPrompts < Pry::ClassCommand - match 'list-prompts' - group 'Input and Output' - description 'List the prompts available for use.' - banner <<-BANNER - Usage: list-prompts - - List the available prompts. You can use change-prompt to switch between - them. - BANNER - - def process - output.puts heading("Available prompts") + "\n" - Pry::Prompt.all.each do |name, prompt| - output.write "Name: #{bold(name)}" - output.puts selected_prompt?(prompt) ? selected_text : "" - output.puts prompt[:description] - output.puts - end - end - - private - - def selected_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 index 6f902300..d4a1c613 100644 --- a/lib/pry/prompt.rb +++ b/lib/pry/prompt.rb @@ -95,9 +95,9 @@ class Pry end add(:default, <<DESC) do |context, nesting, _pry_, sep| -The default Pry prompt. Includes information about the -current expression number, evaluation context, and nesting -level, plus a reminder that you're using Pry. +The default Pry prompt. Includes information about the current expression +number, evaluation context, and nesting level, plus a reminder that you're +using Pry. DESC format( "[%<in_count>s] %<name>s(%<context>s)%<nesting>s%<separator>s ", @@ -109,13 +109,13 @@ DESC ) end - add(:simple, "A simple '>>'.", ['>> ', ' | ']) do |_, _, _, sep| + add(:simple, "A simple `>>`.\n", ['>> ', ' | ']) do |_, _, _, sep| sep end add(:nav, <<DESC, %w[> *]) do |context, nesting, _pry_, sep| -A prompt that displays the binding stack as a path and -includes information about _in_ and _out_. +A prompt that displays the binding stack as a path and includes information +about #{Helpers::Text.bold('_in_')} and #{Helpers::Text.bold('_out_')}. DESC tree = _pry_.binding_stack.map { |b| Pry.view_clip(b.eval('self')) } format( @@ -129,8 +129,7 @@ DESC end add(:shell, <<DESC, %w[$ *]) do |context, nesting, _pry_, sep| -A prompt that displays the binding stack as a path and -includes information about _in_ and _out_. +A prompt that displays `$PWD` as you change it. DESC format( "%<name>s %<context>s:%<pwd>s %<separator>s ", |