Class: ReeText::WordWrap

Inherits:
Object
  • Object
show all
Includes:
Ree::FnDSL
Defined in:
lib/ree_lib/packages/ree_text/package/ree_text/functions/word_wrap.rb

Constant Summary collapse

DEFAULTS =
{
  line_width: 80,
  break_sequence: "\n"
}

Instance Method Summary collapse

Instance Method Details

#call(text, **opts) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/ree_lib/packages/ree_text/package/ree_text/functions/word_wrap.rb', line 43

def call(text, **opts)
  options = DEFAULTS.merge(opts)
  formated_text = text.dup

  formated_text.split("\n").collect do |line|
    if line.length > options[:line_width]
      line
        .gsub(/(.{1,#{options[:line_width]}})(\s+|$)/, "\\1#{options[:break_sequence]}")
        .rstrip
    else
      line
    end
  end * options[:break_sequence]
end