File: prompt.rb

package info (click to toggle)
ruby-pry-rails 0.3.9-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 320 kB
  • sloc: ruby: 844; makefile: 4
file content (51 lines) | stat: -rw-r--r-- 1,690 bytes parent folder | download
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
45
46
47
48
49
50
51
module PryRails
  class Prompt
    class << self
      def formatted_env
        if Rails.env.production?
          bold_env = Pry::Helpers::Text.bold(Rails.env)
          Pry::Helpers::Text.red(bold_env)
        elsif Rails.env.development?
          Pry::Helpers::Text.green(Rails.env)
        else
          Rails.env
        end
      end

      def project_name
        if Rails::VERSION::MAJOR >= 6
          Rails.application.class.module_parent_name.underscore
        else
          Rails.application.class.parent_name.underscore
        end
      end
    end
  end

  desc = "Includes the current Rails environment and project folder name.\n" \
          "[1] [project_name][Rails.env] pry(main)>"
  if Pry::Prompt.respond_to?(:add)
    Pry::Prompt.add 'rails', desc, %w(> *) do |target_self, nest_level, pry, sep|
      "[#{pry.input_ring.size}] " \
      "[#{Prompt.project_name}][#{Prompt.formatted_env}] " \
      "#{pry.config.prompt_name}(#{Pry.view_clip(target_self)})" \
      "#{":#{nest_level}" unless nest_level.zero?}#{sep} "
    end
  else
    draw_prompt = lambda do |target_self, nest_level, pry, sep|
      "[#{pry.input_array.size}] " \
      "[#{Prompt.project_name}][#{Prompt.formatted_env}] " \
      "#{pry.config.prompt_name}(#{Pry.view_clip(target_self)})" \
      "#{":#{nest_level}" unless nest_level.zero?}#{sep} "
    end
    prompts = [
      proc do |target_self, nest_level, pry|
        draw_prompt.call(target_self, nest_level, pry, '>')
      end,
      proc do |target_self, nest_level, pry|
        draw_prompt.call(target_self, nest_level, pry, '*')
      end
    ]
    Pry::Prompt::MAP["rails"] = {value: prompts, description: desc}
  end
end