Class: RainbowHighlighter

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/glark/util/highlight.rb

Constant Summary collapse

COLORS =
%w{ black red green yellow blue magenta cyan white [\dA-Fa-f]{6} }
DECORATIONS =
%w{ none reset bold underscore underline blink reverse inverse negative concealed }
BACKGROUND_COLORS =
COLORS.collect { |color| "on_#{color}" }
FOREGROUND_COLORS =
COLORS
COLORS_RE =
Regexp.new('(?: ' + 
# background will be in capture 0
'on(?:\s+|_) ( ' + COLORS.join(' | ') + ' ) | ' +
# foreground will be in capture 1
'( ' + (COLORS + DECORATIONS).join(' | ') + ' ) ' +
')', Regexp::EXTENDED)

Instance Method Summary collapse

Instance Method Details

#get_code(color, type) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/glark/util/highlight.rb', line 49

def get_code color, type
  case color
  when 'bold', 'bright'
    Sickill::Rainbow::TERM_EFFECTS[:bright]
  when 'reverse', 'negative', 'inverse'
    Sickill::Rainbow::TERM_EFFECTS[:inverse]
  when 'underline'
    Sickill::Rainbow::TERM_EFFECTS[:underline]
  when 'blink'
    Sickill::Rainbow::TERM_EFFECTS[:blink]
  when %r{^[\dA-Fa-f]{6}$}
    ac = Sickill::Rainbow::AnsiColor.new type, color
    ac.code
  else
    ac = Sickill::Rainbow::AnsiColor.new type, color.to_sym
    ac.code
  end
end

#to_codes(color) ⇒ Object



68
69
70
71
72
73
74
75
76
# File 'lib/glark/util/highlight.rb', line 68

def to_codes color
  codes = ""
  return codes unless Sickill::Rainbow.enabled
  color.scan(COLORS_RE).collect do |md|
    color, type = md[0] ? [ md[0], :background ] : [ md[1], :foreground ]
    code = get_code color, type
    "\e[#{code}m"
  end.join ''
end