Class: Hexdump::Theme::Rule Private
- Inherits:
-
Object
- Object
- Hexdump::Theme::Rule
- 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.
Instance Attribute Summary collapse
-
#highlight_regexps ⇒ Hash{String => ANSI}
readonly
private
Highlighting rules for matching substrings.
-
#highlight_strings ⇒ Hash{String => ANSI}
readonly
private
Highlighting rules for exact strings.
-
#style ⇒ ANSI?
readonly
private
The default style to apply to strings.
Instance Method Summary collapse
-
#apply(string) ⇒ String
private
Applies coloring/highlighting to a string.
-
#highlight(pattern, style) ⇒ Object
Adds a highlighting rule.
-
#highlights ⇒ Hash{String,Regexp => ANSI}
private
The highlighting rules.
-
#initialize(style: nil, highlights: nil) ⇒ Rule
constructor
private
Initializes the color.
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.
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_regexps ⇒ Hash{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.
29 30 31 |
# File 'lib/hexdump/theme/rule.rb', line 29 def highlight_regexps @highlight_regexps end |
#highlight_strings ⇒ Hash{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.
24 25 26 |
# File 'lib/hexdump/theme/rule.rb', line 24 def highlight_strings @highlight_strings end |
#style ⇒ 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.
The default style to apply to strings.
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.
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.
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 |
#highlights ⇒ Hash{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.
63 64 65 |
# File 'lib/hexdump/theme/rule.rb', line 63 def highlights @highlight_strings.merge(@highlight_regexps) end |