Class: Vedeu::Output::Wordwrap
- Inherits:
-
Object
- Object
- Vedeu::Output::Wordwrap
- Defined in:
- lib/vedeu/output/wordwrap.rb
Overview
Wrap or prune text.
Instance Attribute Summary collapse
- #options ⇒ Hash readonly protected
- #text ⇒ String readonly protected
Class Method Summary collapse
Instance Method Summary collapse
- #content ⇒ Vedeu::Views::Lines
-
#defaults ⇒ Hash<Symbol => Fixnum, String, Symbol>
private
Returns the default options/attributes for this class.
-
#ellipsis ⇒ String
private
For when using mode ‘:prune`, by default, provides ’…‘.
- #ellipsis_string(string) ⇒ String private
-
#initialize(text, options = {}) ⇒ Vedeu::Output::Wordwrap
constructor
Returns a new instance of Vedeu::Output::Wordwrap.
-
#mode ⇒ Symbol
private
Returns the word wrapping mode.
- #prune ⇒ Array<String>|String
-
#prune_string(string) ⇒ String
private
Returns the string pruned.
-
#pruned_width ⇒ Fixnum
private
Returns the width of the string minus the ellipsis.
-
#split_lines ⇒ Array<String>
private
Returns the text as an array of lines, split on ‘n’.
- #to_line_objects(text_as_lines) ⇒ Vedeu::Views::Lines private
-
#width ⇒ Fixnum
private
Returns the width to prune or wrap to.
- #wrap ⇒ String
Constructor Details
#initialize(text, options = {}) ⇒ Vedeu::Output::Wordwrap
Returns a new instance of Vedeu::Output::Wordwrap.
22 23 24 25 |
# File 'lib/vedeu/output/wordwrap.rb', line 22 def initialize(text, = {}) @text = text = defaults.merge!() end |
Instance Attribute Details
#options ⇒ Hash (readonly, protected)
83 84 85 |
# File 'lib/vedeu/output/wordwrap.rb', line 83 def end |
#text ⇒ String (readonly, protected)
79 80 81 |
# File 'lib/vedeu/output/wordwrap.rb', line 79 def text @text end |
Class Method Details
.for(text, options = {}) ⇒ Object
10 11 12 |
# File 'lib/vedeu/output/wordwrap.rb', line 10 def self.for(text, = {}) new(text, ).content end |
Instance Method Details
#content ⇒ Vedeu::Views::Lines
28 29 30 31 32 33 34 35 |
# File 'lib/vedeu/output/wordwrap.rb', line 28 def content case mode when :prune then to_line_objects(prune) when :wrap then to_line_objects(wrap) else to_line_objects(split_lines) end end |
#defaults ⇒ Hash<Symbol => Fixnum, String, Symbol> (private)
Returns the default options/attributes for this class.
159 160 161 162 163 164 165 |
# File 'lib/vedeu/output/wordwrap.rb', line 159 def defaults { ellipsis: '...', mode: :default, width: 70, } end |
#ellipsis ⇒ String (private)
For when using mode ‘:prune`, by default, provides ’…‘.
133 134 135 |
# File 'lib/vedeu/output/wordwrap.rb', line 133 def ellipsis [:ellipsis] end |
#ellipsis_string(string) ⇒ String (private)
109 110 111 112 113 |
# File 'lib/vedeu/output/wordwrap.rb', line 109 def ellipsis_string(string) return prune_string(string) if string.size < ellipsis.size "#{prune_string(string)}#{ellipsis}".freeze end |
#mode ⇒ Symbol (private)
Returns the word wrapping mode. One of :default, :prune or
:wrap;
:default = Renders the content as is.
:prune = Discards the remainder of the content line
after width.
:wrap = Forces the content on to a new line after
width.
145 146 147 |
# File 'lib/vedeu/output/wordwrap.rb', line 145 def mode [:mode] end |
#prune ⇒ Array<String>|String
38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/vedeu/output/wordwrap.rb', line 38 def prune return text if text.size <= pruned_width if split_lines.size > 1 split_lines.reduce([]) { |a, e| a << ellipsis_string(e) } else ellipsis_string(text) end end |
#prune_string(string) ⇒ String (private)
Returns the string pruned.
119 120 121 |
# File 'lib/vedeu/output/wordwrap.rb', line 119 def prune_string(string) string.chomp.slice(0..pruned_width) end |
#pruned_width ⇒ Fixnum (private)
Returns the width of the string minus the ellipsis.
126 127 128 |
# File 'lib/vedeu/output/wordwrap.rb', line 126 def pruned_width width - ellipsis.size end |
#split_lines ⇒ Array<String> (private)
Returns the text as an array of lines, split on ‘n’.
103 104 105 |
# File 'lib/vedeu/output/wordwrap.rb', line 103 def split_lines text.split(/\n/) end |
#to_line_objects(text_as_lines) ⇒ Vedeu::Views::Lines (private)
89 90 91 92 93 94 95 96 97 98 |
# File 'lib/vedeu/output/wordwrap.rb', line 89 def to_line_objects(text_as_lines) line_objects = Array(text_as_lines).map do |text_line| stream = Vedeu::Views::Stream.new(value: text_line) line = Vedeu::Views::Line.new stream.parent = line line.add(stream) line end Vedeu::Views::Lines.new(line_objects) end |
#width ⇒ Fixnum (private)
Returns the width to prune or wrap to.
152 153 154 |
# File 'lib/vedeu/output/wordwrap.rb', line 152 def width [:width] end |
#wrap ⇒ String
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/vedeu/output/wordwrap.rb', line 51 def wrap processed = [] split_lines.map do |unprocessed| line_length = 0 reformatted = [] unprocessed.split(/\s/).map do |word| word_length = word.length + 1 if (line_length += word_length) >= width line_length = word_length processed << reformatted reformatted = [] end reformatted << word end processed << reformatted end processed.reduce([]) { |a, e| a << e.join(' ') } end |