Module: Colorer

Extended by:
Colorer
Included in:
Colorer
Defined in:
lib/colorer.rb

Defined Under Namespace

Classes: AlreadyDefinedMethod, UnknownSgrCode

Constant Summary collapse

VERSION =
File.read(File.expand_path('../../VERSION', __FILE__)).strip
BASIC_SGR =

Select Graphic Rendition

{
  :clear     => 0,
  :bold      => 1,
  :underline => 4,
  :blinking  => 5,
  :reversed  => 7,

  :black     => 30,
  :red       => 31,
  :green     => 32,
  :yellow    => 33,
  :blue      => 34,
  :magenta   => 35,
  :cyan      => 36,
  :white     => 37,

  :onblack   => 40,
  :onred     => 41,
  :ongreen   => 42,
  :onyellow  => 43,
  :onblue    => 44,
  :onmagenta => 45,
  :oncyan    => 46,
  :onwhite   => 47
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#colorObject Also known as: color?

Returns the value of attribute color.



47
48
49
# File 'lib/colorer.rb', line 47

def color
  @color
end

#strict_ansiObject

Returns the value of attribute strict_ansi.



51
52
53
# File 'lib/colorer.rb', line 51

def strict_ansi
  @strict_ansi
end

Instance Method Details

#def_basic_styles(basic = true, force = false) ⇒ Object



53
54
55
# File 'lib/colorer.rb', line 53

def def_basic_styles(basic=true, force=false)
  define_styles basic_styles(basic), force
end

#def_custom_styles(styles, force = false) ⇒ Object



57
58
59
# File 'lib/colorer.rb', line 57

def def_custom_styles(styles, force=false)
  define_styles styles, force
end

#def_strip_ansiObject

strips all ansi SGR codes from self



102
103
104
105
106
107
108
109
110
# File 'lib/colorer.rb', line 102

def def_strip_ansi
  unless 'string'.respond_to?(:strip_ansi)
    String.class_eval do
      define_method(:strip_ansi) do
        gsub(/\e\[[\d;]+m/, '')
      end
    end
  end
end

#define_styles(styles, force = false) ⇒ Object



61
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
# File 'lib/colorer.rb', line 61

def define_styles(styles, force=false)
  unless caller[0].match(/def_basic_styles|def_custom_styles/)
    puts 'DEPRECATION WARNING: :define_styles has been deprecated. Use def_basic_styles and def_custom_styles instead'
    if styles.keys.include?(:basic)
      puts 'DEPRECATION WARNING: :basic is not a reserved name anymore: please use def_basic_styles to define the basic methods'
      styles = basic_styles(styles.delete(:basic)).merge(styles)
    end
    puts "    #{caller[0]}"
  end
  check_or_define_or
  styles.each_pair do |meth, style|
    # allows dummy methods (useful for style overriding)
    if style.nil?
      String.class_eval { define_method(meth) { self } }
      next
    end
    style = [style] unless style.is_a?(Array)
    codes = style.map do |s|
              code = s.is_a?(Symbol) ? BASIC_SGR[s] : s
              raise UnknownSgrCode.new(s) unless code.is_a?(Integer) && (0..109).include?(code)
              code
            end
    String.class_eval do
      raise AlreadyDefinedMethod.new(meth, self) if !force && method_defined?(meth)
      define_method(meth) do
        return self unless Colorer.color?
        if Colorer.strict_ansi
          match(/^\e\[[\d;]+m.*\e\[0m$/m) ?
            sub(/^(\e\[[\d;]+)/, '\1;' + codes.join(';')) :
            sprintf("\e[0;%sm%s\e[0m", codes.join(';'), self)
        else
          match(/^(?:\e\[\d+m)+.*\e\[0m$/m) ?
            sub(/^((?:\e\[\d+m)+)/, '\1' + codes.map{|c| "\e[#{c}m" }.join) :
            sprintf("\e[0m%s%s\e[0m", codes.map{|c| "\e[#{c}m" }.join, self)
        end
      end
    end
  end
end