Class: AnsiFormatter
Instance Method Summary collapse
- #format_and_highlight_array(data, highlight_color_sym: :exception_color_detail, plain_color_sym: :menu_chrome_color, label: 'Data:', highlight: [], line_prefix: ' ', line_postfix: '', detail_sep: '') ⇒ Object
-
#initialize(options = {}) ⇒ AnsiFormatter
constructor
A new instance of AnsiFormatter.
-
#scan_and_process_multiple_substrings(str, substrings, plain_sym, color_sym) ⇒ Array<String>
Function to scan a string and process its segments based on multiple substrings.
-
#scan_and_process_string(str, substring, plain_sym, color_sym) ⇒ Array<String>
Function to scan a string and process its segments.
-
#string_send_color(string, color_sym, default: 'plain') ⇒ String
Applies a color method to a string based on the provided color symbol.
Constructor Details
#initialize(options = {}) ⇒ AnsiFormatter
Returns a new instance of AnsiFormatter.
9 10 11 |
# File 'lib/ansi_formatter.rb', line 9 def initialize( = {}) @options = end |
Instance Method Details
#format_and_highlight_array(data, highlight_color_sym: :exception_color_detail, plain_color_sym: :menu_chrome_color, label: 'Data:', highlight: [], line_prefix: ' ', line_postfix: '', detail_sep: '') ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/ansi_formatter.rb', line 13 def format_and_highlight_array( data, highlight_color_sym: :exception_color_detail, plain_color_sym: :menu_chrome_color, label: 'Data:', highlight: [], line_prefix: ' ', line_postfix: '', detail_sep: '' ) data&.map do |item| scan_and_process_multiple_substrings(item, highlight, plain_color_sym, highlight_color_sym).join end || [] end |
#scan_and_process_multiple_substrings(str, substrings, plain_sym, color_sym) ⇒ Array<String>
Function to scan a string and process its segments based on multiple substrings
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/ansi_formatter.rb', line 35 def scan_and_process_multiple_substrings(str, substrings, plain_sym, color_sym) return string_send_color(str, plain_sym) if substrings.empty? || substrings.any?(&:empty?) substring_patterns = substrings.map do |value| [value, Regexp.new(value, Regexp::IGNORECASE)] end results = [] remaining_str = str.dup while remaining_str.length.positive? match_indices = substring_patterns.map do |_, pattern| remaining_str.index(pattern) end.compact earliest_match = match_indices.min if earliest_match # Process non-matching segment before the earliest match, if any unless earliest_match.zero? non_matching_segment = remaining_str.slice!(0...earliest_match) results << string_send_color(non_matching_segment, plain_sym) end # Find which substring has this earliest match matching_substring = substring_patterns.find do |_, pattern| remaining_str.index(pattern) == earliest_match end if matching_substring matching_segment = remaining_str.slice!(0...matching_substring[0].length) results << string_send_color(matching_segment, color_sym) end else # Process the remaining non-matching segment results << string_send_color(remaining_str, plain_sym) break end end results end |
#scan_and_process_string(str, substring, plain_sym, color_sym) ⇒ Array<String>
Function to scan a string and process its segments
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/ansi_formatter.rb', line 85 def scan_and_process_string(str, substring, plain_sym, color_sym) return string_send_color(str, plain_sym) unless substring.present? results = [] remaining_str = str.dup while remaining_str.length.positive? match_index = remaining_str.index(substring) if match_index # Process non-matching segment before the match, if any unless match_index.zero? non_matching_segment = remaining_str.slice!(0...match_index) results << string_send_color(non_matching_segment, plain_sym) end # Process the matching segment matching_segment = remaining_str.slice!(0...substring.length) results << string_send_color(matching_segment, color_sym) else # Process the remaining non-matching segment results << string_send_color(remaining_str, plain_sym) break end end results end |
#string_send_color(string, color_sym, default: 'plain') ⇒ String
Applies a color method to a string based on the provided color symbol. The color method is fetched from @options and applied to the string.
119 120 121 122 |
# File 'lib/ansi_formatter.rb', line 119 def string_send_color(string, color_sym, default: 'plain') color_method = @options.fetch(color_sym, default).to_sym AnsiString.new(string.to_s).send(color_method) end |