class Mentor::TextToColor
Public Class Methods
new(full_text, text_to_color, output_type, match_pattern = nil)
click to toggle source
# File lib/helpers/text_to_color.rb, line 7 def initialize(full_text, text_to_color, output_type, match_pattern = nil) @full_text = full_text @text_to_color = text_to_color @output_type = output_type @match_pattern = match_pattern end
Public Instance Methods
color_pattern()
click to toggle source
# File lib/helpers/text_to_color.rb, line 14 def color_pattern @full_text.gsub(@match_pattern, colored) end
colored()
click to toggle source
# File lib/helpers/text_to_color.rb, line 18 def colored return @full_text unless text_to_color_exists? color_text end
Private Instance Methods
before_text_to_color(remaining_text)
click to toggle source
# File lib/helpers/text_to_color.rb, line 48 def before_text_to_color(remaining_text) index = remaining_text.index(@text_to_color) return '' if !index || index.zero? remaining_text[0..index - 1] end
color_regex()
click to toggle source
# File lib/helpers/text_to_color.rb, line 59 def color_regex /\e\[(\d|;)+m/ end
color_text()
click to toggle source
# File lib/helpers/text_to_color.rb, line 33 def color_text remaining_text = @match_pattern || @full_text text_processed = '' while remaining_text[@text_to_color] before_text = before_text_to_color(remaining_text) reset_color = get_reset_color(before_text).to_s text_processed += before_text + colorized_text index_after_text_processed = (before_text + @text_to_color).size remaining_text = reset_color + remaining_text[index_after_text_processed..-1] end text_processed + remaining_text end
colorized_text()
click to toggle source
# File lib/helpers/text_to_color.rb, line 25 def colorized_text rainbow(@text_to_color, @output_type) end
get_reset_color(before_text)
click to toggle source
# File lib/helpers/text_to_color.rb, line 54 def get_reset_color(before_text) index_of_color_to_reset_to = before_text.rindex(color_regex) before_text[index_of_color_to_reset_to..-1][color_regex] if index_of_color_to_reset_to end
text_to_color_exists?()
click to toggle source
# File lib/helpers/text_to_color.rb, line 29 def text_to_color_exists? @text_to_color && !@text_to_color.empty? end