lib/cri/string_formatter.rb
# Word-wraps and indents the string.
#
# @param [String] str The string to format
#
# @param [Number] width The maximal width of each line. This also includes
#   indentation, i.e. the actual maximal width of the text is
#   `width`-`indentation`.
#
# @param [Number] indentation The number of spaces to indent each line.
#
# @param [Boolean] first_line_already_indented Whether or not the first
#   line is already indented
#
# @return [String] The word-wrapped and indented string
Word-wraps and indents the string.
Word - wraps and indents the string .
def wrap_and_indent(str, width, indentation, first_line_already_indented = false)
      indented_width = width - indentation
      indent = ' ' * indentation
      paragraphs = to_paragraphs(str)
      text = paragraphs.map do |paragraph|
        lines = []
        line = ''
        paragraph.split(/\s/).each do |word|
          if (line + ' ' + word).length >= indented_width
            lines << line
            line = ''
          end
          line += (line == '' ? '' : ' ') + word
        end
        lines << line
        lines.map { |l| indent + l }.join("\n")
      end.join("\n\n")
      if first_line_already_indented
        text[indentation..-1]
      else
        text
      end
    end
def wrap_and_indent ( str , width , indentation , first_line_already_indented = false ) indented_width = width - indentation indent = ' ' * indentation paragraphs = to_paragraphs ( str ) text = paragraphs . map do | paragraph | lines = [ ] line = '' paragraph . split ( / \s / ) . each do | word | if ( line + ' ' + word ) . length >= indented_width lines << line line = '' end line += ( line == '' ? '' : ' ' ) + word end lines << line lines . map { | l | indent + l } . join ( "\n" ) end . join ( "\n\n" ) if first_line_already_indented text [ indentation .. - 1 ] else text end end
Word-wraps and indents the string.
Word - wraps and indents the string .
# Split into paragraphs # Wrap and indent each paragraph # Initialize # Split into words # Begin new line if it's too long # Add word to line # Join lines
The word-wrapped and indented string
The word - wrapped and indented string
String
str
<not_specific>
width
<not_specific>
indentation
<not_specific>
first_line_already_indented
<not_specific>
<not_specific>
first_line_already_indented Whether or not the first line is already indented
first_line_already_indented Whether or not the first line is already indented