class CTioga2::Commands::Documentation::WordWrapper

A small utility class to do word wrapping.

todo Maybe this belongs in Utils ?

Attributes

new_sep[RW]

What to replace the separator with

word_sep[RW]

A regex matching word separation.

Public Class Methods

new(ws = /\s+/, ns = " ") click to toggle source
# File lib/ctioga2/commands/doc/wordwrap.rb, line 36
def initialize(ws = /\s+/, ns = " ")
  @word_sep = ws
  @new_sep = ns
end
wrap(str, cols) click to toggle source

Calls wrap for default values of the parameters

# File lib/ctioga2/commands/doc/wordwrap.rb, line 57
def self.wrap(str, cols)
  return WordWrapper.new.wrap(str, cols)
end

Public Instance Methods

wrap(str, cols) click to toggle source

Split strings into an array of string whose length is each less than cols

# File lib/ctioga2/commands/doc/wordwrap.rb, line 43
def wrap(str, cols)
  words = str.split(@word_sep)
  lines = [words.shift]
  while w = words.shift
    if (lines.last.size + w.size + @new_sep.size) <= cols
      lines.last.concat("#{@new_sep}#{w}")
    else
      lines << w
    end
  end
  return lines
end