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 @options = defaults.merge!() end |
Instance Attribute Details
#options ⇒ Hash (readonly, protected)
91 92 93 |
# File 'lib/vedeu/output/wordwrap.rb', line 91 def @options end |
#text ⇒ String (readonly, protected)
87 88 89 |
# File 'lib/vedeu/output/wordwrap.rb', line 87 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.
168 169 170 171 172 173 174 |
# File 'lib/vedeu/output/wordwrap.rb', line 168 def defaults { ellipsis: '...', mode: :default, width: 70, } end |
#ellipsis ⇒ String (private)
For when using mode ‘:prune`, by default, provides ’…‘.
145 146 147 |
# File 'lib/vedeu/output/wordwrap.rb', line 145 def ellipsis .fetch(:ellipsis) end |
#ellipsis_string(string) ⇒ String (private)
117 118 119 120 121 122 123 124 125 |
# File 'lib/vedeu/output/wordwrap.rb', line 117 def ellipsis_string(string) if string.size < ellipsis.size prune_string(string) else "#{prune_string(string)}#{ellipsis}" end 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.
154 155 156 |
# File 'lib/vedeu/output/wordwrap.rb', line 154 def mode .fetch(:mode) end |
#prune ⇒ Array<String>|String
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/vedeu/output/wordwrap.rb', line 38 def prune return text if text.size <= pruned_width processed = [] if split_lines.size > 1 processed = split_lines.reduce([]) do |acc, line| acc << ellipsis_string(line) end else processed = ellipsis_string(text) end processed end |
#prune_string(string) ⇒ String (private)
Returns the string pruned.
131 132 133 |
# File 'lib/vedeu/output/wordwrap.rb', line 131 def prune_string(string) string.chomp.slice(0..pruned_width) end |
#pruned_width ⇒ Fixnum (private)
Returns the width of the string minus the ellipsis.
138 139 140 |
# File 'lib/vedeu/output/wordwrap.rb', line 138 def pruned_width width - ellipsis.size end |
#split_lines ⇒ Array<String> (private)
Returns the text as an array of lines, split on ‘n’.
111 112 113 |
# File 'lib/vedeu/output/wordwrap.rb', line 111 def split_lines text.split(/\n/) end |
#to_line_objects(text_as_lines) ⇒ Vedeu::Views::Lines (private)
97 98 99 100 101 102 103 104 105 106 |
# File 'lib/vedeu/output/wordwrap.rb', line 97 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.
161 162 163 |
# File 'lib/vedeu/output/wordwrap.rb', line 161 def width .fetch(:width) end |
#wrap ⇒ String
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/vedeu/output/wordwrap.rb', line 57 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([]) do |output, line| output << line.join(' ') end end |