Module: Participle::Colors

Included in:
Bot, LogQueue
Defined in:
lib/colors.rb

Overview

Uses ANSI escape sequences for coloring on command line. All of this stuff performs properly in Windows, which is to say it just returns whatever you throw at it

Defined Under Namespace

Classes: FormatError

Constant Summary

ANSI_COLORS_HEX =

A map of RGB values to ANSI escape sequence colors

{"000000" => 30, "FF0000" => 31, "00FF00" => 32, "FFFF00" => 33, "0000FF" => 34, "FF00FF" => 35, "00FFFF" => 36, "FFFFFF" => 37}
ANSI_COLORS_NAMED =

A map of color names to ANSI escape sequence colors

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

A map of style names to ANSI escape sequence stylings

{:normal => "21m\e[22m\e[23", :bold => 1, :faint => 2, :italic => 3, :underline => 4}

Instance Method Summary (collapse)

Instance Method Details

- (String) ansi_colorize(str, color)

Colorize a string; should be used in conjunction with closest_to.



118
119
120
121
# File 'lib/colors.rb', line 118

def ansi_colorize str, color
  return str if color.nil? || !(RUBY_PLATFORM =~ /linux|darwin/)
  "\e[#{color}m#{str}\e[0m"
end

- (String) ansi_stylize(str, style)

Stylize a string.



127
128
129
130
# File 'lib/colors.rb', line 127

def ansi_stylize str, style
  return str if style.nil? || !(RUBY_PLATFORM =~ /linux|darwin/)
  "\e[#{ANSI_STYLES[style]}m#{str}\e[#{ANSI_STYLES[:normal]}m"
end

- (Array?) chromaticied(line)

Determines if a line has been styled by the Chromacity dAMN extension.



202
203
204
# File 'lib/colors.rb', line 202

def chromaticied line
  line.match(/<abbr title="colors:([0-9A-F]{6}):([0-9A-F]{6})"><\/abbr>/).to_a[1..-1]
end

- (Fixnum) closest_to(color)

Finds the closest 16-bit ANSI color to color.



89
90
91
# File 'lib/colors.rb', line 89

def closest_to(color)
  ANSI_COLORS_HEX[ANSI_COLORS_HEX.keys.sort{|a,b|color_difference(a, color) <=> color_difference(b, color)}.first]
end

- (Fixnum) color_difference(color1, color2)

Finds the "difference" between two colors (cumulative difference between RGB values).



97
98
99
100
101
102
103
104
# File 'lib/colors.rb', line 97

def color_difference color1, color2
  sum = 0
  bits, pieces = to_hex(color1), to_hex(color2)
  bits.each_index{|i|
    sum -= [bits[i], pieces[i]].sort.inject(:-)
  }
  sum
end

- (String) colorize(str, color)

Colorize a string; standalone method.



110
111
112
# File 'lib/colors.rb', line 110

def colorize str, color
  ansi_colorize(str, ANSI_COLORS_NAMED[color])
end

- (String) embolden(str)

Bold a string.



145
146
147
# File 'lib/colors.rb', line 145

def embolden str
  ansi_colorize(str, 1)
end

- (String) format_html(str)

Makes the logs all pretty for the terminal.



216
217
218
219
220
221
222
# File 'lib/colors.rb', line 216

def format_html str
  return str if RUBY_PLATFORM =~ /mswin|mingw|bccwin|wince|emx/
  #{}"\e[#{ANSI_COLORS_NAMED[@@logformat["line"]["color"].to_sym]}m" + 
  str.gsub(/<b>(.*?)<\/b>/){"\e[1m#$1\e[22m"}.
  gsub(/<i>(.*?)<\/i>/){"\e[7m#$1\e[27m"}.
  gsub(/<u>(.*?)<\/u>/){"\e[4m#$1\e[24m"}
end

- (String) format_user(uname)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



183
184
185
# File 'lib/colors.rb', line 183

def format_user uname
  ansi_stylize(uname, @@logformat["username"]["style"].to_sym)
end

- (Fixnum) get_term_size

Get the current prompt width.



134
135
136
137
138
139
140
# File 'lib/colors.rb', line 134

def get_term_size
  if RUBY_PLATFORM =~ /mswin|mingw|bccwin|wince|emx/
    `mode`.split(/\n/).grep(/Columns/).first.split(/ +/).last.to_i
  else
    `stty size`.split(" ")[1].to_i
  end
end

- (String) italicize(str)

Italicize a string.



152
153
154
# File 'lib/colors.rb', line 152

def italicize str
  ansi_colorize(str, 3)
end

- (String) loggerify(category, str, type = "system", username = nil, colors = false)

Converts a string with a room name to pretty logger format.



169
170
171
172
173
174
175
176
177
178
179
# File 'lib/colors.rb', line 169

def loggerify category, str, type = "system", username = nil, colors = false
  type = "##{type}" unless type == "system"
  if category == "system"
    @@logformat["formats"]["system"].colorformat.gsub("%timestamp", timestamp).gsub("%line", format_html(str))
  else
    @@logformat["formats"][category].colorformat.gsub("%timestamp", timestamp).
    gsub("%room", room_name(type)).
    gsub("%username", format_user(username || "")).
    gsub("%line", format_html(str.decode_entities))
  end
end

- (String) room_name(room)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



189
190
191
# File 'lib/colors.rb', line 189

def room_name room
  ansi_colorize(ansi_stylize(room.truncate(15), @@logformat["room"]["style"].to_sym), ANSI_COLORS_NAMED[@@logformat["room"]["color"].to_sym])
end

- (String) timestamp

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



195
196
197
# File 'lib/colors.rb', line 195

def timestamp
  ansi_colorize(ansi_stylize(Time.now.strftime("%H:%M:%S"), @@logformat["timestamp"]["style"].to_sym), ANSI_COLORS_NAMED[@@logformat["timestamp"]["color"].to_sym])
end

- (Array<Fixnum>) to_hex(str)

Converts a color string in hex format to an array of RGB values.



82
83
84
# File 'lib/colors.rb', line 82

def to_hex(str)
  str.scan(/../).map{|x|x.to_i(16)}
end

- (String) unchrome(line)

Removes Chromacity styling from a line.



209
210
211
# File 'lib/colors.rb', line 209

def unchrome line
  line.gsub(/ *<abbr title="colors:[0-9A-F]{6}:[0-9A-F]{6}"><\/abbr>/, "")
end

- (String) underscore(str)

Underscore a string.



159
160
161
# File 'lib/colors.rb', line 159

def underscore str
  ansi_colorize(str, 4)
end