Class: CTioga2::Commands::Documentation::WordWrapper
- Inherits:
-
Object
- Object
- CTioga2::Commands::Documentation::WordWrapper
- 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
-
#new_sep ⇒ Object
What to replace the separator with.
-
#word_sep ⇒ Object
A regex matching word separation.
Class Method Summary collapse
-
.wrap(str, cols) ⇒ Object
Calls #wrap for default values of the parameters.
Instance Method Summary collapse
-
#initialize(ws = /\s+/, ns = " ") ⇒ WordWrapper
constructor
A new instance of WordWrapper.
-
#wrap(str, cols) ⇒ Object
Split strings into an array of string whose length is each less than cols.
Constructor Details
#initialize(ws = /\s+/, ns = " ") ⇒ WordWrapper
Returns a new instance of WordWrapper.
38 39 40 41 |
# File 'lib/ctioga2/commands/doc/wordwrap.rb', line 38 def initialize(ws = /\s+/, ns = " ") @word_sep = ws @new_sep = ns end |
Instance Attribute Details
#new_sep ⇒ Object
What to replace the separator with
36 37 38 |
# File 'lib/ctioga2/commands/doc/wordwrap.rb', line 36 def new_sep @new_sep end |
#word_sep ⇒ Object
A regex matching word separation.
33 34 35 |
# File 'lib/ctioga2/commands/doc/wordwrap.rb', line 33 def word_sep @word_sep end |
Class Method Details
.wrap(str, cols) ⇒ Object
Calls #wrap for default values of the parameters
59 60 61 |
# File 'lib/ctioga2/commands/doc/wordwrap.rb', line 59 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
45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/ctioga2/commands/doc/wordwrap.rb', line 45 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 |