Class: String

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

Overview

Colorize String class extension.

Constant Summary collapse

COLORS =

Colors Hash

{
  :black          => 0,
  :red            => 1,
  :green          => 2,
  :yellow         => 3,
  :blue           => 4,
  :magenta        => 5,
  :cyan           => 6,
  :white          => 7,
  :default        => 9,
  
  :light_black    => 10,
  :light_red      => 11,
  :light_green    => 12,
  :light_yellow   => 13,
  :light_blue     => 14,
  :light_magenta  => 15,
  :light_cyan     => 16,
  :light_white    => 17
}
MODES =

Modes Hash

{
  :default        => 0, # Turn off all attributes
  #:bright        => 1, # Set bright mode
  :underline      => 4, # Set underline mode
  :blink          => 5, # Set blink mode
  :swap           => 7, # Exchange foreground and background colors
  :hide           => 8  # Hide text (foreground color would be the same as background)
}
BBS_COLOR_TABLE =

BBS-style numeric color codes.

{
  0   => :black,
  1   => :blue,
  2   => :green,
  3   => :cyan,
  4   => :red,
  5   => :magenta,
  6   => :yellow,
  7   => :white,
  8   => :light_black,
  9   => :light_blue,
  10  => :light_green,
  11  => :light_cyan,
  12  => :light_red,
  13  => :light_magenta,
  14  => :light_yellow,
  15  => :light_white,
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.color_matrix(txt = "[X]") ⇒ Object

Display color matrix with color names.



179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/colorize.rb', line 179

def color_matrix( txt = "[X]" )
  size = String.colors.length
  String.colors.each do | color |
    String.colors.each do | back |
     print txt.colorize( :color => color, :background => back )
    end
    puts " < #{color}"
  end
  String.colors.reverse.each_with_index do | back, index |
    puts "#{"|".rjust(txt.length)*(size-index)} < #{back}"
  end 
end

.colorsObject

Return array of available colors used by colorize method



168
169
170
171
172
173
174
# File 'lib/colorize.rb', line 168

def colors
  keys = []
  COLORS.each_key do | key |
    keys << key
  end
  keys
end

.modesObject

Return array of available modes used by colorize method



157
158
159
160
161
162
163
# File 'lib/colorize.rb', line 157

def modes
  keys = []
  MODES.each_key do | key |
    keys << key
  end
  keys
end

Instance Method Details

#colorize(params = nil) ⇒ Object

Change color of string

Examples:

puts "This is blue".colorize( :blue )
puts "This is light blue".colorize( :light_blue )
puts "This is also blue".colorize( :color => :blue )
puts "This is blue with red background".colorize( :color => :light_blue, :background => :red )
puts "This is blue with red background".colorize( :light_blue ).colorize( :background => :red )
puts "This is blue text on red".blue.on_red
puts "This is red on blue".colorize( :red ).on_blue
puts "This is red on blue and underline".colorize( :red ).on_blue.underline
puts "This is blue text on red".blue.on_red.blink


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
106
107
108
109
110
# File 'lib/colorize.rb', line 76

def colorize( params=nil )
  
  return self.tagged_colors unless params
  return self unless STDOUT.isatty

  begin
      require 'Win32/Console/ANSI' if PLATFORM =~ /win32/
  rescue LoadError
      raise 'You must gem install win32console to use color on Windows'
  end
  
  
  color_parameters = {}

  if (params.instance_of?(Hash))
    color_parameters[:color] = COLORS[params[:color]]
    color_parameters[:background] = COLORS[params[:background]]
    color_parameters[:mode] = MODES[params[:mode]]
  elsif (params.instance_of?(Symbol))
    color_parameters[:color] = COLORS[params]
  end
  
  color_parameters[:color] ||= @color || 9
  color_parameters[:background] ||= @background || 9
  color_parameters[:mode] ||= @mode || 0

  color_parameters[:uncolorized] ||= @uncolorized || self.dup
 
  # calculate bright mode
  color_parameters[:color] += 50 if color_parameters[:color] > 10

  color_parameters[:background] += 50 if color_parameters[:background] > 10

  return "\033[#{color_parameters[:mode]};#{color_parameters[:color]+30};#{color_parameters[:background]+40}m#{color_parameters[:uncolorized]}\033[0m".set_color_parameters( color_parameters )
end

#colorized?Boolean

Return true if sting is colorized

Returns:



123
124
125
# File 'lib/colorize.rb', line 123

def colorized?
  return !@uncolorized.nil?
end

#tagged_colorsObject

Colorize a string that has “color tags”.

Examples:

Colors as words:

puts "<light_yellow><light_white>*</light_white> Hey mom! I am <light_green>SO</light_green> colourized right now.</light_yellow>".colorize

Numeric ANSI colors (from the BBS days):

puts "<10><5>*</5> Hey mom! I am <9>SO</9> colourized right now.</10>".colorize


232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/colorize.rb', line 232

def tagged_colors
  stack = []

  # matchers for just the color part of the tag
  open_tag_re     = /<([\w\d_]+)>/
  close_tag_re    = /<\/([\w\d_]+)>/

  # split the string into tags and texts
  tokens          = self.split(/(<\/?[\w\d_]+>)/)
  tokens.delete_if { |token| token.size == 0 }
  
  result        = ""

  tokens.each do |token|
    
    if open_tag_re =~ token and valid_color?($1)

      stack.push $1

    elsif close_tag_re =~ token and valid_color?($1)

  # if this color is on the stack somwehere...
      if pos = stack.rindex($1)
        # close the tag by removing it from the stack
        stack.delete_at pos
      else
        raise "Error: tried to close an unopened color tag -- #{token}"
      end

    else

      color = (stack.last || "white")
      color = BBS_COLOR_TABLE[color.to_i] if color =~ /^\d+$/
      result << token.colorize(color.to_sym)
      
    end
    
  end
  
  result
end

#uncolorizeObject

Return uncolorized string



116
117
118
# File 'lib/colorize.rb', line 116

def uncolorize
  return @uncolorized || self
end

#valid_color?(string) ⇒ Boolean

Returns:



217
218
219
# File 'lib/colorize.rb', line 217

def valid_color?(string)
  COLORS.include?(string.to_sym) or (string =~ /^\d+$/ and BBS_COLOR_TABLE.include?(string.to_i))
end