summaryrefslogtreecommitdiff
path: root/lib/pry/commands/change_prompt.rb
blob: 37d81da23a9215be4c7e42c6acdf886c597334b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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