# Class: Cri::StringFormatter Private

Inherits:
  Object
  - Object
  - Cri::StringFormatter
  show all

Defined in:
  lib/cri/string_formatter.rb

## Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Used for formatting strings (e.g. converting to paragraphs, wrapping, formatting as title)

## Instance Method Summary collapse

- #bold(str) ⇒ Object private
- #format_as_command(str, io) ⇒ String private

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

- #format_as_option(str, io) ⇒ String private

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

- #format_as_title(str, io) ⇒ String private

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

- #green(str) ⇒ Object private
- #red(str) ⇒ Object private
- #to_paragraphs(str) ⇒ Array<String> private

  Extracts individual paragraphs (separated by two newlines).

- #wrap_and_indent(str, width, indentation, first_line_already_indented = false) ⇒ String private

  Word-wraps and indents the string.

- #yellow(str) ⇒ Object private

## Instance Method Details

### #bold(str) ⇒ `Object`

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

    127
    128
    129

    # File 'lib/cri/string_formatter.rb', line 127

    def bold(str)
      "\e[1m#{str}\e[0m"
    end

### #format_as_command(str, io) ⇒ `String`

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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

Parameters:

- str (`String`) —

  The string to format

Returns:

- (`String`) —

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

    95
    96
    97
    98
    99
    100
    101

    # File 'lib/cri/string_formatter.rb', line 95

    def format_as_command(str, io)
      if Cri::Platform.color?(io)
        green(str)
      else
        str
      end
    end

### #format_as_option(str, io) ⇒ `String`

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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

Parameters:

- str (`String`) —

  The string to format

Returns:

- (`String`) —

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

    107
    108
    109
    110
    111
    112
    113

    # File 'lib/cri/string_formatter.rb', line 107

    def format_as_option(str, io)
      if Cri::Platform.color?(io)
        yellow(str)
      else
        str
      end
    end

### #format_as_title(str, io) ⇒ `String`

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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

Parameters:

- str (`String`) —

  The string to format

Returns:

- (`String`) —

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

    83
    84
    85
    86
    87
    88
    89

    # File 'lib/cri/string_formatter.rb', line 83

    def format_as_title(str, io)
      if Cri::Platform.color?(io)
        bold(red(str.upcase))
      else
        str.upcase
      end
    end

### #green(str) ⇒ `Object`

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

    119
    120
    121

    # File 'lib/cri/string_formatter.rb', line 119

    def green(str)
      "\e[32m#{str}\e[0m"
    end

### #red(str) ⇒ `Object`

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

    115
    116
    117

    # File 'lib/cri/string_formatter.rb', line 115

    def red(str)
      "\e[31m#{str}\e[0m"
    end

### #to_paragraphs(str) ⇒ `Array<String>`

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Extracts individual paragraphs (separated by two newlines).

Parameters:

- str (`String`) —

  The string to format

Returns:

- (`Array<String>`) —

  A list of paragraphs in the string

    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27

    # File 'lib/cri/string_formatter.rb', line 14

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

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

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

### #wrap_and_indent(str, width, indentation, first_line_already_indented = false) ⇒ `String`

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Word-wraps and indents the string.

Parameters:

- str (`String`) —

  The string to format

- 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.

- first_line_already_indented (`Boolean`) (defaults to: `false`) —

  Whether or not the first line is already indented

Returns:

- (`String`) —

  The word-wrapped and indented string

    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
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77

    # File 'lib/cri/string_formatter.rb', line 43

    def wrap_and_indent(str, width, indentation, first_line_already_indented = false)
      indented_width = width - indentation
      indent = ' ' * indentation
      # Split into paragraphs
      paragraphs = to_paragraphs(str)

      # Wrap and indent each paragraph
      text = 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")

      if first_line_already_indented
        text[indentation..-1]
      else
        text
      end
    end

### #yellow(str) ⇒ `Object`

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

    123
    124
    125

    # File 'lib/cri/string_formatter.rb', line 123

    def yellow(str)
      "\e[33m#{str}\e[0m"
    end