Class: String
Overview
Colorize String class extension.
Constant Summary collapse
- COLORIZE_VERSION =
Version string
'0.5.6'
- 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) }
Class Method Summary collapse
-
.color_matrix(txt = "[X]") ⇒ Object
Display color matrix with color names.
-
.colors ⇒ Object
Return array of available colors used by colorize method.
-
.modes ⇒ Object
Return array of available modes used by colorize method.
Instance Method Summary collapse
-
#colorize(params) ⇒ Object
Change color of string.
-
#colorized? ⇒ Boolean
Return true if sting is colorized.
-
#uncolorize ⇒ Object
Return uncolorized string.
Class Method Details
.color_matrix(txt = "[X]") ⇒ Object
Display color matrix with color names.
185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/ext/colorize.rb', line 185 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 |
Instance Method Details
#colorize(params) ⇒ 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
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 111 112 113 114 115 116 |
# File 'lib/ext/colorize.rb', line 81 def colorize( params ) unless STDOUT.use_color return self unless STDOUT.isatty end return self if ENV['NO_COLOR'] begin require 'Win32/Console/ANSI' if RUBY_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
129 130 131 |
# File 'lib/ext/colorize.rb', line 129 def colorized? return !@uncolorized.nil? end |
#uncolorize ⇒ Object
Return uncolorized string
122 123 124 |
# File 'lib/ext/colorize.rb', line 122 def uncolorize return @uncolorized || self end |