Class: Msf::Ui::Console::TablePrint::CustomColorStyler

Inherits:
Object
  • Object
show all
Defined in:
lib/msf/ui/console/table_print/custom_color_styler.rb

Constant Summary collapse

RESET_COLOR =
'%clr'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ CustomColorStyler

For example: opts = { 'abc' => '%grn' }

Parameters:

  • opts (Hash<String, String>) (defaults to: {})

    A hash of a String to find, and what color to use



11
12
13
# File 'lib/msf/ui/console/table_print/custom_color_styler.rb', line 11

def initialize(opts = {})
  @highlight_terms = opts.clone
end

Instance Method Details

#merge!(opts) ⇒ Object

Parameters:

  • opts (Hash<String, String>)

    A hash of a String to find, and what color to use



33
34
35
# File 'lib/msf/ui/console/table_print/custom_color_styler.rb', line 33

def merge!(opts)
  @highlight_terms.merge!(opts)
end

#style(value) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/msf/ui/console/table_print/custom_color_styler.rb', line 15

def style(value)
  if @highlight_terms.key?(value)
    return "#{@highlight_terms[value]}#{value}#{RESET_COLOR}"
  end

  colored_value = value

  # Maximal munch; consume the terms in order of length, from longest to shortest
  @highlight_terms.keys.sort_by { |key| -key.length }.each do |key|
    if value.include?(key)
      colored_value.gsub!(key, "#{@highlight_terms[key]}#{key}#{RESET_COLOR}")
    end
  end

  colored_value
end