Class: Cri::StringFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/cri/string_formatter.rb

Instance Method Summary (collapse)

Instance Method Details

- (String) format_as_command(s, io)

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

Parameters:

  • s (String)

    The string to format

Returns:

  • (String)

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



86
87
88
89
90
91
92
# File 'lib/cri/string_formatter.rb', line 86

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

- (String) format_as_option(s, io)

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

Parameters:

  • s (String)

    The string to format

Returns:

  • (String)

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



98
99
100
101
102
103
104
# File 'lib/cri/string_formatter.rb', line 98

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

- (String) format_as_title(s, io)

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

Parameters:

  • s (String)

    The string to format

Returns:

  • (String)

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



74
75
76
77
78
79
80
# File 'lib/cri/string_formatter.rb', line 74

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

- (Array<String>) to_paragraphs(s)

Extracts individual paragraphs (separated by two newlines).

Parameters:

  • s (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(s)
  lines = s.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(s, width, indentation)

Word-wraps and indents the string.

Parameters:

  • s (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.

Returns:

  • (String)

    The word-wrapped and indented string



40
41
42
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
# File 'lib/cri/string_formatter.rb', line 40

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

  # 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