1
0
Fork 0
mirror of https://github.com/pry/pry.git synced 2022-11-09 12:35:05 -05:00
pry--pry/lib/pry/commands/change_prompt.rb
Kyrylo Silin bc26e405ea Simplify Prompt API
Before this change when you set a prompt, you have to do the following:

```rb
Pry.config.prompt = Pry::Prompt[:simple][:value]
```

The `[:value]` part was leaking implementation details and it proved to be an
unnecessary step.

With this change we can do the following:

```rb
Pry.config.prompt = Pry::Prompt[:simple]
```

`[:value]` is omitted.

I have also refactored some tests and removed irrelevant ones.

The Array API for prompt is deprecated:
`Pry.config.prompt = [proc {}, proc {}]` emits a warning now.
2018-11-17 05:26:25 +08:00

44 lines
1 KiB
Ruby

class Pry::Command::ChangePrompt < Pry::ClassCommand
match 'change-prompt'
group 'Input and Output'
description 'Change the current prompt.'
command_options argument_required: true
banner <<-BANNER
Usage: change-prompt [OPTIONS] [NAME]
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}\n" +
prompt.description
end
output.puts(prompts.join("\n" * 2))
end
def change_prompt(prompt)
if Pry::Prompt[prompt]
_pry_.prompt = Pry::Prompt[prompt]
else
raise Pry::CommandError, "'#{prompt}' isn't a known prompt. " \
"Run `change-prompt --list` to see the list of known prompts."
end
end
Pry::Commands.add_command(self)
end