Class: AnsiFormatter

Inherits:
Object show all
Defined in:
lib/ansi_formatter.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ AnsiFormatter

Returns a new instance of AnsiFormatter.



7
8
9
# File 'lib/ansi_formatter.rb', line 7

def initialize(options = {})
  @options = 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



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/ansi_formatter.rb', line 11

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

Parameters:

  • str (String)

    The string to scan.

  • substrings (Array<String>)

    The substrings to match in the string.

  • plain_sym (Symbol)

    The symbol for non-matching segments.

  • color_sym (Symbol)

    The symbol for matching segments.

Returns:



33
34
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
# File 'lib/ansi_formatter.rb', line 33

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 { |_, pattern| remaining_str.index(pattern) }.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

Parameters:

  • str (String)

    The string to scan.

  • substring (String)

    The substring to match in the string.

  • plain_sym (Symbol)

    The symbol for non-matching segments.

  • color_sym (Symbol)

    The symbol for matching segments.

Returns:



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/ansi_formatter.rb', line 79

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.

Parameters:

  • string (String)

    The string to which the color will be applied.

  • color_sym (Symbol)

    The symbol representing the color method.

  • default (String) (defaults to: 'plain')

    Default color method to use if color_sym is not found in @options.

Returns:

  • (String)

    The string with the applied color method.



113
114
115
116
# File 'lib/ansi_formatter.rb', line 113

def string_send_color(string, color_sym, default: 'plain')
  color_method = @options.fetch(color_sym, default).to_sym
  string.to_s.send(color_method)
end