Class: Loquacious::Configuration::Help::StringPresenter

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/loquacious/configuration/help/string_presenter.rb

Instance Method Summary collapse

Instance Method Details

#indent(leader) ⇒ Object

call-seq:

StringPresenter.new("foo").indent 2        #=> "  foo"
StringPresenter.new("foo").indent '#  '    #=> "# foo"

Indent the string by the given number of spaces. Alternately, if a leader string is given it will be used to indent with instead of spaces. Indentation is performed at the beginning of the string and after every newline character.

"foo\nbar".indent 2    #=> "  foo\n  bar"


44
45
46
47
48
49
50
# File 'lib/loquacious/configuration/help/string_presenter.rb', line 44

def indent(leader)
  leader =
      Numeric === leader ? ' ' * leader.to_i : leader.to_s
  str = self.gsub "\n", "\n"+leader
  str.insert 0, leader
  str
end

#reduce(width, ellipses = '...') ⇒ Object

call-seq:

reduce width, ellipses = '...'    #=> string

Reduce the size of the current string to the given width by removing characters from the middle of the string and replacing them with ellipses. If the width is greater than the length of the string, the string is returned unchanged. If the width is less than the length of the ellipses, then the ellipses are returned.

Raises:

  • (ArgumentError)


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/loquacious/configuration/help/string_presenter.rb', line 16

def reduce(width, ellipses = '...')
  raise ArgumentError, "width cannot be negative: #{width}" if width < 0

  return to_s if length <= width

  remove = length - width + ellipses.length
  return ellipses.dup if remove >= length

  left_end = (length + 1 - remove) / 2
  right_start = left_end + remove

  left = self[0,left_end]
  right = self[right_start,length-right_start]

  left << ellipses << right
end