1
0
Fork 0
mirror of https://github.com/pry/pry.git synced 2022-11-09 12:35:05 -05:00

Merge pull request #1849 from pry/prompt-commands

commands/change_prompt: incorporate 'list_prompts' functionality
This commit is contained in:
Kyrylo Silin 2018-11-04 13:28:07 +08:00 committed by GitHub
commit 0f452a5c02
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 35 additions and 68 deletions

View file

@ -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

View file

@ -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

View file

@ -1,24 +0,0 @@
class Pry
class Command::SimplePrompt < Pry::ClassCommand
match 'simple-prompt'
group 'prompts'
description 'Toggle the simple prompt.'
banner <<-'BANNER'
Toggle the simple prompt.
BANNER
def process
state.disabled ^= true
if state.disabled
state.prev_prompt = _pry_.prompt
_pry_.prompt = Pry::Prompt[:simple][:value]
else
_pry_.prompt = state.prev_prompt
end
end
end
Pry::Commands.add_command(Pry::Command::SimplePrompt)
end

View file

@ -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 ",