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
# 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?)

  results = []
  remaining_str = str.dup

  while remaining_str.length.positive?
    match_indices = substrings.map { |substring| remaining_str.index(substring) }.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 = substrings.find do |substring|
        remaining_str.index(substring) == earliest_match
      end

      if matching_substring
        matching_segment = remaining_str.slice!(0...matching_substring.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:



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

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.



109
110
111
112
# File 'lib/ansi_formatter.rb', line 109

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