Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/colorb.rb

Constant Summary collapse

Colors =
{
  :default => 9,

  :black   => 0,
  :red     => 1,
  :green   => 2,
  :yellow  => 3,
  :blue    => 4,
  :magenta => 5,
  :cyan    => 6,
  :white   => 7
}
Extra =
{
  :clean => 0,

  :bold      => 1,
  :underline => 4,
  :blink     => 5,
  :standout  => 7
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#backgroundObject

Returns the value of attribute background.



44
45
46
# File 'lib/colorb.rb', line 44

def background
  @background
end

#flagsObject

Returns the value of attribute flags.



44
45
46
# File 'lib/colorb.rb', line 44

def flags
  @flags
end

#foregroundObject

Returns the value of attribute foreground.



44
45
46
# File 'lib/colorb.rb', line 44

def foreground
  @foreground
end

Class Method Details

.color!(what, bg = false) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/colorb.rb', line 143

def self.color! (what, bg=false)
  if what.is_a?(Symbol) || what.is_a?(String)
    if Colors[what.to_sym]
      "\e[#{Colors[what.to_sym] + (bg ? 40 : 30)}m"
    else
      color = Color::RGB.from_html(what.to_s)

      "\e[#{bg ? 48 : 38};2;#{color.red.to_i};#{color.green.to_i};#{color.blue.to_i}m"
    end
  elsif what.is_a?(Numeric)
    if what < 8
      "\e[#{what + (bg ? 40 : 30)}m"
    else
      "\e[#{bg ? 48 : 38};5;#{what}m"
    end
  end
end

.colorify(string, foreground, background, flags) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/colorb.rb', line 125

def self.colorify (string, foreground, background, flags)
  return string if ENV['NO_COLORS'] && !ENV['NO_COLORS'].empty?

  string = string.dup

  string.sub!(/^/, [
    String.color!(foreground),
    String.color!(background, true),
    [flags].flatten.compact.uniq.map {|f|
      String.extra!(f)
    }
  ].flatten.compact.join(''))

  string.sub!(/$/, String.extra!(:clean))

  string
end

.extra!(what) ⇒ Object



161
162
163
# File 'lib/colorb.rb', line 161

def self.extra! (what)
  Extra[what] ? "\e[#{Extra[what]}m" : "\e[#{what}m"
end

Instance Method Details

#color(code, second = nil) ⇒ Object



66
67
68
# File 'lib/colorb.rb', line 66

def color (code, second=nil)
  self.dup.color!(code, second)
end

#color!(code, second = nil) ⇒ Object



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

def color! (code, second=nil)
  string = (self.frozen?) ? self.dup : self

  if code.is_a?(Symbol) || code.is_a?(String)
    if !string.foreground
      string.foreground = code
    else
      string.background = code
    end

    string.lazy? ? string : string.colorify!
  else
    if code < 16
      if code > 7
        (string.flags ||= []) << (!string.foreground ? :bold : :blink)
      end

      if !string.foreground
        string.foreground = code - (code > 7 ? 7 : 0)
      else
        string.background = code - (code > 7 ? 7 : 0)
      end
    else
      if !string.foreground
        string.foreground = code
      else
        string.background = code
      end
    end
  end

  string.color!(second) if second

  string.lazy? ? string : string.colorify!
end

#colorify!Object



121
122
123
# File 'lib/colorb.rb', line 121

def colorify!
  String.colorify(self, @foreground, @background, @flags)
end

#extra(name) ⇒ Object



106
107
108
# File 'lib/colorb.rb', line 106

def extra (name)
  self.dup.extra!(name)
end

#extra!(name) ⇒ Object



110
111
112
113
114
115
116
# File 'lib/colorb.rb', line 110

def extra! (name)
  string = (self.frozen?) ? self.dup : self

  (string.flags ||= []) << name

  string.lazy? ? string : string.colorify!
end

#lazyObject



118
# File 'lib/colorb.rb', line 118

def lazy;  @lazy = true; self end

#lazy?Boolean

Returns:

  • (Boolean)


119
# File 'lib/colorb.rb', line 119

def lazy?; @lazy              end