def format_paragraph(paragraph, wrap_at, ansi_stack)
      cleared_para = Sanitizer.replace(paragraph)
      lines = []
      line = ''
      word = ''
      word_length = 0
      line_length = 0
      char_length = 0 # visible char length
      text_length = display_width(cleared_para)
      total_length = 0
      ansi = ''
      matched = nil
      to_chars(cleared_para) do |char|
        if char == ANSI # found ansi
          ansi << char && next
        end

        if ansi.length > 0
          ansi << char
          if Sanitizer.ansi?(ansi) # we found ansi let's consume
            matched = ansi
          elsif matched
            ansi_stack << [matched[0...-1], line_length + word_length]
            matched = nil
            ansi    = ''
          end
          next if ansi.length > 0
        end

        char_length = display_width(char)
        total_length += char_length
        if line_length + word_length + char_length <= wrap_at
          if char == SPACE || total_length == text_length
            line << word + char
            line_length += word_length + char_length
            word = ''
            word_length = 0
          else
            word << char
            word_length += char_length
          end
          next
        end

        if char == SPACE # ends with space
          lines << insert_ansi(ansi_stack, line)
          line = ''
          line_length = 0
          word += char
          word_length += char_length
        elsif word_length + char_length <= wrap_at
          lines << insert_ansi(ansi_stack, line)
          line = word + char
          line_length = word_length + char_length
          word = ''
          word_length = 0
        else # hyphenate word - too long to fit a line
          lines << insert_ansi(ansi_stack, word)
          line_length = 0
          word = char
          word_length = char_length
        end
      end
      lines << insert_ansi(ansi_stack, line) unless line.empty?
      lines << insert_ansi(ansi_stack, word) unless word.empty?
      lines
    end
def format_paragraph(paragraph, wrap_at, ansi_stack)
      cleared_para = Sanitizer.replace(paragraph)
      lines = []
      line = ''
      word = ''
      word_length = 0
      line_length = 0
      char_length = 0 # visible char length
      text_length = display_width(cleared_para)
      total_length = 0
      ansi = ''
      matched = nil
      to_chars(cleared_para) do |char|
        if char == ANSI # found ansi
          ansi << char && next
        end

        if ansi.length > 0
          ansi << char
          if Sanitizer.ansi?(ansi) # we found ansi let's consume
            matched = ansi
          elsif matched
            ansi_stack << [matched[0...-1], line_length + word_length]
            matched = nil
            ansi    = ''
          end
          next if ansi.length > 0
        end

        char_length = display_width(char)
        total_length += char_length
        if line_length + word_length + char_length <= wrap_at
          if char == SPACE || total_length == text_length
            line << word + char
            line_length += word_length + char_length
            word = ''
            word_length = 0
          else
            word << char
            word_length += char_length
          end
          next
        end

        if char == SPACE # ends with space
          lines << insert_ansi(ansi_stack, line)
          line = ''
          line_length = 0
          word += char
          word_length += char_length
        elsif word_length + char_length <= wrap_at
          lines << insert_ansi(ansi_stack, line)
          line = word + char
          line_length = word_length + char_length
          word = ''
          word_length = 0
        else # hyphenate word - too long to fit a line
          lines << insert_ansi(ansi_stack, word)
          line_length = 0
          word = char
          word_length = char_length
        end
      end
      lines << insert_ansi(ansi_stack, line) unless line.empty?
      lines << insert_ansi(ansi_stack, word) unless word.empty?
      lines
    end
Format paragraph to be maximum of wrap_at length

 @param [String] paragraph
   the paragraph to format
 @param [Integer] wrap_at
   the maximum length to wrap the paragraph

 @return [Array[String]]
   the wrapped lines

 @api private