Module: Dragoon::Color

Included in:
Bot
Defined in:
lib/dragoon/color.rb

Constant Summary collapse

COLORED_REGEXP =

borrowed from Term::ANSIColors

/\e\[(?:(?:[349]|10)[0-7]|[0-9])?m/
COLOR_LIST =
[:red, :green, :blue, :magenta, :cyan, :white]

Instance Method Summary collapse

Instance Method Details

#colorize(text) ⇒ Object

assign a permanent color to a text



12
13
14
15
16
# File 'lib/dragoon/color.rb', line 12

def colorize(text)
  @c_table ||= {}
  color = @c_table.fetch(text) rescue @c_table[text] = next_color
  text.send color
end

#highlight(word) ⇒ Object



36
37
38
# File 'lib/dragoon/color.rb', line 36

def highlight(word)
  word.black.on_yellow
end

#highlight_keywords(text, keywords) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/dragoon/color.rb', line 22

def highlight_keywords(text,keywords)
  keywords.sort_by(&:length).reverse.inject(text) do |result, keyword|
    result.gsub(keyword) { |word|
      to_left_of_word, to_right_of_word = $`, $'
      # check if inside exisitng highlighted word
      if to_left_of_word =~ /#{COLORED_REGEXP}\S+$/ && to_right_of_word =~ /^\S+#{COLORED_REGEXP}/
        word
      else
        highlight(word)
      end
    }
  end
end

#next_colorObject



18
19
20
# File 'lib/dragoon/color.rb', line 18

def next_color
  COLOR_LIST.push(COLOR_LIST.shift).first
end