Class: CTioga2::Commands::Documentation::WordWrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/ctioga2/commands/doc/wordwrap.rb

Overview

A small utility class to do word wrapping.

todo Maybe this belongs in Utils ?

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ws = /\s+/, ns = " ") ⇒ WordWrapper

Returns a new instance of WordWrapper.



36
37
38
39
# File 'lib/ctioga2/commands/doc/wordwrap.rb', line 36

def initialize(ws = /\s+/, ns = " ")
  @word_sep = ws
  @new_sep = ns
end

Instance Attribute Details

#new_sepObject

What to replace the separator with



34
35
36
# File 'lib/ctioga2/commands/doc/wordwrap.rb', line 34

def new_sep
  @new_sep
end

#word_sepObject

A regex matching word separation.



31
32
33
# File 'lib/ctioga2/commands/doc/wordwrap.rb', line 31

def word_sep
  @word_sep
end

Class Method Details

.wrap(str, cols) ⇒ Object

Calls #wrap for default values of the parameters



57
58
59
# File 'lib/ctioga2/commands/doc/wordwrap.rb', line 57

def self.wrap(str, cols)
  return WordWrapper.new.wrap(str, cols)
end

Instance Method Details

#wrap(str, cols) ⇒ Object

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



43
44
45
46
47
48
49
50
51
52
53
54
# 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