Module: Qpixel

Defined in:
lib/console_extension.rb

Overview

Create module that can be included in the .irbrc:

Qpixel.irb! if defined?(Qpixel)

Class Method Summary collapse

Class Method Details

.irb!Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/console_extension.rb', line 62

def self.irb!
  IRB::Irb.class_eval do
    private

    def self.rails_environment
      case Rails.env
      when 'development'
        "\e[32mdev\e[0m"
      when 'production'
        "\e[31mprod\e[0m"
      when 'test'
        "\e[32mtest\e[0m"
      when 'staging'
        "\e[32mstag\e[0m"
      else
        "\e[31m#{Rails.env}\e[0m"
      end
    end

    def self.qpixel_prompt
      c = RequestContext.community
      "[#{rails_environment}] [\e[34m#{c&.name || '-'} @ #{c&.host || '-'}\e[0m]"
    end
  end

  IRB::Irb.class_eval do
    # Define an alternative string dup method which will redetermine the prompt part if community changes
    qpixel_block = proc do |s|
      def s.dup
        IRB::Irb.qpixel_prompt + self
      end
    end

    IRB.conf[:PROMPT][:QPIXEL] = {
      PROMPT_I: ':%03n> '.tap(&qpixel_block),
      PROMPT_N: ':%03n> '.tap(&qpixel_block),
      PROMPT_S: ':%03n%l '.tap(&qpixel_block),
      PROMPT_C: ':%03n* '.tap(&qpixel_block),
      RETURN: IRB.conf[:PROMPT][:DEFAULT][:RETURN]
    }

    IRB.conf[:PROMPT_MODE] = :QPIXEL
  end
end