Method: CLI::UI::Formatter#format

Defined in:
lib/cli/ui/formatter.rb

#format(sgr_map = SGR_MAP, enable_color: CLI::UI.enable_color?) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/cli/ui/formatter.rb', line 104

def format(sgr_map = SGR_MAP, enable_color: CLI::UI.enable_color?)
  @nodes.replace([])
  stack = parse_body(StringScanner.new(@text))
  prev_fmt = T.let(nil, T.nilable(Stack))
  content = @nodes.each_with_object(+'') do |(text, fmt), str|
    if prev_fmt != fmt && enable_color
      text = apply_format(text, fmt, sgr_map)
    end
    str << text
    prev_fmt = fmt
  end

  stack.reject! { |e| e.is_a?(LITERAL_BRACES) }

  return content unless enable_color
  return content if stack == prev_fmt

  unless stack.empty? && (@nodes.empty? || T.must(@nodes.last)[1].empty?)
    content << apply_format('', stack, sgr_map)
  end
  content
end