Class: Hexdump::Theme::Rule Private

Inherits:
Object
  • Object
show all
Defined in:
lib/hexdump/theme/rule.rb

Overview

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

Represents a color highlighting rule.

Since:

  • 1.0.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(style: nil, highlights: nil) ⇒ Rule

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.

Initializes the color.

Parameters:

  • style (Symbol, Array<Symbol>) (defaults to: nil)

    The default style name(s). See ANSI::PARAMETERS.

  • highlights (Hash{String,Regexp => Symbol,Array<Symbol>}, nil) (defaults to: nil)

    Optional highlighting rules.

Since:

  • 1.0.0



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/hexdump/theme/rule.rb', line 40

def initialize(style: nil, highlights: nil)
  @reset = ANSI::RESET
  
  @style = if style
             ANSI.new(style)
           end

  @highlight_strings = {}
  @highlight_regexps = {}

  if highlights
    highlights.each do |pattern,style|
      highlight(pattern,style)
    end
  end
end

Instance Attribute Details

#highlight_regexpsHash{String => ANSI} (readonly)

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.

Highlighting rules for matching substrings.

Returns:

Since:

  • 1.0.0



29
30
31
# File 'lib/hexdump/theme/rule.rb', line 29

def highlight_regexps
  @highlight_regexps
end

#highlight_stringsHash{String => ANSI} (readonly)

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.

Highlighting rules for exact strings.

Returns:

Since:

  • 1.0.0



24
25
26
# File 'lib/hexdump/theme/rule.rb', line 24

def highlight_strings
  @highlight_strings
end

#styleANSI? (readonly)

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.

The default style to apply to strings.

Returns:

Since:

  • 1.0.0



19
20
21
# File 'lib/hexdump/theme/rule.rb', line 19

def style
  @style
end

Instance Method Details

#apply(string) ⇒ String

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.

Applies coloring/highlighting to a string.

Parameters:

  • string (String)

    The string to color/highlight.

Returns:

  • (String)

    The colored the string.

Since:

  • 1.0.0



109
110
111
112
113
114
115
116
117
# File 'lib/hexdump/theme/rule.rb', line 109

def apply(string)
  if (!@highlight_strings.empty? || !@highlight_regexps.empty?)
    apply_highlight(string)
  elsif @style
    "#{@style}#{string}#{@reset}"
  else
    string
  end
end

#highlight(pattern, style) ⇒ Object

Adds a highlighting rule.

Examples:

hexdump.style.numeric.highlight('00', :faint)
hexdump.style.index.highlight(/00$/, [:white, :bold])

Parameters:

  • pattern (String, Regexp)

    The exact String to highlight or regular expression to highlight.

  • style (Symbol, Array<Symbol>)

    The style name(s). See ANSI::PARAMETERS.

Raises:

  • (ArgumentError)

    The given pattern was not a String or Regexp.

Since:

  • 1.0.0



87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/hexdump/theme/rule.rb', line 87

def highlight(pattern,style)
  ansi = ANSI.new(style)

  case pattern
  when String
    @highlight_strings[pattern] = ansi
  when Regexp
    @highlight_regexps[pattern] = ansi
  else
    raise(ArgumentError,"pattern must be a String or Regexp: #{pattern.inspect}")
  end
end

#highlightsHash{String,Regexp => ANSI}

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.

The highlighting rules.

Returns:

Since:

  • 1.0.0



63
64
65
# File 'lib/hexdump/theme/rule.rb', line 63

def highlights
  @highlight_strings.merge(@highlight_regexps)
end