Class: StringWrapper

Inherits:
Object show all
Defined in:
lib/hash_delegator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(width:, fill_margin: false, first_indent: '', indent_space: ' ', left_margin: 0, margin_char: ' ', rest_indent: '', right_margin: 0) ⇒ StringWrapper

Initializes the StringWrapper with the given options.

Parameters:

  • width (Integer)

    the maximum width of each line

  • left_margin (Integer) (defaults to: 0)

    the number of spaces for the left margin

  • right_margin (Integer) (defaults to: 0)

    the number of spaces for the right margin

  • indent (Integer)

    the number of spaces to indent all but the first line

  • fill_margin (Boolean) (defaults to: false)

    whether to fill the left margin with spaces



458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
# File 'lib/hash_delegator.rb', line 458

def initialize(
  width:,
  fill_margin: false,
  first_indent: '',
  indent_space: '  ',
  left_margin: 0,
  margin_char: ' ',
  rest_indent: '',
  right_margin: 0
)
  @fill_margin = fill_margin
  @first_indent = first_indent
  @indent = indent
  @indent_space = indent_space
  @rest_indent = rest_indent
  @right_margin = right_margin
  @width = width

  @margin_space = fill_margin ? (margin_char * left_margin) : ''
  @left_margin = @margin_space.length
end

Instance Attribute Details

#fill_marginObject (readonly)

Returns the value of attribute fill_margin.



447
448
449
# File 'lib/hash_delegator.rb', line 447

def fill_margin
  @fill_margin
end

#indentObject (readonly)

Returns the value of attribute indent.



447
448
449
# File 'lib/hash_delegator.rb', line 447

def indent
  @indent
end

#left_marginObject (readonly)

Returns the value of attribute left_margin.



447
448
449
# File 'lib/hash_delegator.rb', line 447

def left_margin
  @left_margin
end

#right_marginObject (readonly)

Returns the value of attribute right_margin.



447
448
449
# File 'lib/hash_delegator.rb', line 447

def right_margin
  @right_margin
end

#widthObject (readonly)

Returns the value of attribute width.



447
448
449
# File 'lib/hash_delegator.rb', line 447

def width
  @width
end

Instance Method Details

#wrap(text) ⇒ String

Wraps the given text according to the specified options.

Parameters:

  • text (String)

    the text to wrap

Returns:

  • (String)

    the wrapped text



484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
# File 'lib/hash_delegator.rb', line 484

def wrap(text)
  text = text.dup if text.frozen?
  max_line_length = width - left_margin - right_margin - @indent_space.length
  lines = []
  current_line = String.new

  words = text.split
  words.each.with_index do |word, index|
    trial_length = word.length
    trial_length += @first_indent.length if index.zero?
    if index != 0
      trial_length += current_line.length + 1 + @rest_indent.length
    end
    if trial_length > max_line_length && (words.count != 0)
      lines << current_line
      current_line = word
      current_line = current_line.dup if current_line.frozen?
    else
      current_line << ' ' unless current_line.empty?
      current_line << word
    end
  end
  lines << current_line unless current_line.empty?

  lines.map.with_index do |line, index|
    @margin_space + if index.zero?
                      @first_indent
                    else
                      @rest_indent
                    end + line
  end
end