Class: StringWrapper
Instance Attribute Summary collapse
-
#fill_margin ⇒ Object
readonly
Returns the value of attribute fill_margin.
-
#indent ⇒ Object
readonly
Returns the value of attribute indent.
-
#left_margin ⇒ Object
readonly
Returns the value of attribute left_margin.
-
#right_margin ⇒ Object
readonly
Returns the value of attribute right_margin.
-
#width ⇒ Object
readonly
Returns the value of attribute width.
Instance Method Summary collapse
-
#initialize(width:, fill_margin: false, first_indent: '', indent_space: ' ', left_margin: 0, margin_char: ' ', rest_indent: '', right_margin: 0) ⇒ StringWrapper
constructor
Initializes the StringWrapper with the given options.
-
#wrap(text) ⇒ String
Wraps the given text according to the specified options.
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.
562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 |
# File 'lib/hash_delegator.rb', line 562 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_margin ⇒ Object (readonly)
Returns the value of attribute fill_margin.
551 552 553 |
# File 'lib/hash_delegator.rb', line 551 def fill_margin @fill_margin end |
#indent ⇒ Object (readonly)
Returns the value of attribute indent.
551 552 553 |
# File 'lib/hash_delegator.rb', line 551 def indent @indent end |
#left_margin ⇒ Object (readonly)
Returns the value of attribute left_margin.
551 552 553 |
# File 'lib/hash_delegator.rb', line 551 def left_margin @left_margin end |
#right_margin ⇒ Object (readonly)
Returns the value of attribute right_margin.
551 552 553 |
# File 'lib/hash_delegator.rb', line 551 def right_margin @right_margin end |
#width ⇒ Object (readonly)
Returns the value of attribute width.
551 552 553 |
# File 'lib/hash_delegator.rb', line 551 def width @width end |
Instance Method Details
#wrap(text) ⇒ String
Wraps the given text according to the specified options.
588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 |
# File 'lib/hash_delegator.rb', line 588 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 |