class Text::Reform::BreakAt

Public Class Methods

new(hyphen) click to toggle source
# File lib/text/reform.rb, line 1470
def initialize hyphen
  @hyphen = hyphen
end

Public Instance Methods

break(str, initial_max_length, total_width) click to toggle source

Break by inserting a hyphen string.

initial_max_length

The maximum size of the first part of the word that will remain on the first line.

total_width

The total width that can be appended to this first line.

# File lib/text/reform.rb, line 1480
def break(str, initial_max_length, total_width)
  max = total_width - @hyphen.length
  if max <= 0
    ret = [str[0, 1], str[1, -1]]
  elsif str =~ /(.{1,#{max}}#@hyphen)(.*)/s
    ret = [ $1, $2 ]
  elsif str.length > total_width
    sep = initial_max_length-@hyphen.length
    ret = [
      str[0, sep]+@hyphen,
      str[sep..-1]
    ]
  else
    ret = [ '', str ]
  end

  return '', str if ret[0] =~ /\A\s*\Z/
    return ret
end