Class List Method List File List

# Module: Cri::CoreExtensions::String

Included in:
  String
Defined in:
  lib/cri/core_ext/string.rb

## Instance Method Summary (collapse)

- - (String) formatted_as_command

  The string, formatted to be used as the name of a command in the help.

- - (String) formatted_as_option

  The string, formatted to be used as an option definition of a command in the help.

- - (String) formatted_as_title

  The string, formatted to be used as a title in a section in the help.

- - (Array<String>) to_paragraphs

  Extracts individual paragraphs (separated by two newlines).

- - (String) wrap_and_indent(width, indentation)

  Word-wraps and indents the string.

## Instance Method Details

### - (`String`) formatted_as_command

Returns The string, formatted to be used as the name of a command in the help

Returns:

- (`String`) —

  The string, formatted to be used as the name of a command in the help

    73
    74
    75

    # File 'lib/cri/core_ext/string.rb', line 73

    def formatted_as_command
      self.green
    end

### - (`String`) formatted_as_option

Returns The string, formatted to be used as an option definition of a command in the help

Returns:

- (`String`) —

  The string, formatted to be used as an option definition of a command in the help

    79
    80
    81

    # File 'lib/cri/core_ext/string.rb', line 79

    def formatted_as_option
      self.yellow
    end

### - (`String`) formatted_as_title

Returns The string, formatted to be used as a title in a section in the help

Returns:

- (`String`) —

  The string, formatted to be used as a title in a section in the help

    67
    68
    69

    # File 'lib/cri/core_ext/string.rb', line 67

    def formatted_as_title
      self.upcase.red.bold
    end

### - (`Array<String>`) to_paragraphs

Extracts individual paragraphs (separated by two newlines).

Returns:

- (`Array<String>`) —

  A list of paragraphs in the string

    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24

    # File 'lib/cri/core_ext/string.rb', line 11

    def to_paragraphs
      lines = self.scan(/([^\n]+\n|[^\n]*$)/).map { |s| s[0].strip }

      paragraphs = [ [] ]
      lines.each do |line|
        if line.empty?
          paragraphs << []
        else
          paragraphs.last << line
        end
      end

      paragraphs.reject { |p| p.empty? }.map { |p| p.join(' ') }
    end

### - (`String`) wrap_and_indent(width, indentation)

Word-wraps and indents the string.

Parameters:

- width (`Number`) —

  The maximal width of each line. This also includes indentation, i.e. the actual maximal width of the text is `width`-`indentation`.

- indentation (`Number`) —

  The number of spaces to indent each line.

Returns:

- (`String`) —

  The word-wrapped and indented string

    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

    # File 'lib/cri/core_ext/string.rb', line 35

    def wrap_and_indent(width, indentation)
      indented_width = width - indentation
      indent = ' ' * indentation
      # Split into paragraphs
      paragraphs = self.to_paragraphs

      # Wrap and indent each paragraph
      paragraphs.map do |paragraph|
        # Initialize
        lines = []
        line = ''

        # Split into words
        paragraph.split(/\s/).each do |word|
          # Begin new line if it's too long
          if (line + ' ' + word).length >= indented_width
            lines << line
            line = ''
          end

          # Add word to line
          line += (line == '' ? '' : ' ' ) + word
        end
        lines << line

        # Join lines
        lines.map { |l| indent + l }.join("\n")
      end.join("\n\n")
    end