Class: Vedeu::Wordwrap
- Inherits:
-
Object
- Object
- Vedeu::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::Lines
- #defaults ⇒ Hash<Symbol => Fixnum, String, Symbol> private
- #ellipsis ⇒ String private
- #ellipsis_string(string) ⇒ String private
-
#initialize(text, options = {}) ⇒ Vedeu::Wordwrap
constructor
Returns a new instance of Vedeu::Wordwrap.
- #mode ⇒ Symbol private
- #prune ⇒ Array<String>|String
- #prune_string(string) ⇒ String private
- #pruned_width ⇒ Fixnum private
- #split_lines ⇒ Array<String> private
- #to_line_objects(text_as_lines) ⇒ Vedeu::Lines private
- #width ⇒ Fixnum private
- #wrap ⇒ String
Constructor Details
#initialize(text, options = {}) ⇒ Vedeu::Wordwrap
Returns a new instance of Vedeu::Wordwrap.
19 20 21 22 |
# File 'lib/vedeu/output/wordwrap.rb', line 19 def initialize(text, = {}) @text = text = defaults.merge!() end |
Instance Attribute Details
#options ⇒ Hash (readonly, protected)
88 89 90 |
# File 'lib/vedeu/output/wordwrap.rb', line 88 def end |
#text ⇒ String (readonly, protected)
84 85 86 |
# File 'lib/vedeu/output/wordwrap.rb', line 84 def text @text end |
Class Method Details
.for(text, options = {}) ⇒ Object
7 8 9 |
# File 'lib/vedeu/output/wordwrap.rb', line 7 def self.for(text, = {}) new(text, ).content end |
Instance Method Details
#content ⇒ Vedeu::Lines
25 26 27 28 29 30 31 32 |
# File 'lib/vedeu/output/wordwrap.rb', line 25 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)
149 150 151 152 153 154 155 |
# File 'lib/vedeu/output/wordwrap.rb', line 149 def defaults { ellipsis: '...', mode: :default, width: 70, } end |
#ellipsis ⇒ String (private)
134 135 136 |
# File 'lib/vedeu/output/wordwrap.rb', line 134 def ellipsis .fetch(:ellipsis) end |
#ellipsis_string(string) ⇒ String (private)
112 113 114 115 116 117 118 119 120 |
# File 'lib/vedeu/output/wordwrap.rb', line 112 def ellipsis_string(string) if string.size < ellipsis.size prune_string(string) else [prune_string(string), ellipsis].join end end |
#mode ⇒ Symbol (private)
139 140 141 |
# File 'lib/vedeu/output/wordwrap.rb', line 139 def mode .fetch(:mode) end |
#prune ⇒ Array<String>|String
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/vedeu/output/wordwrap.rb', line 35 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)
124 125 126 |
# File 'lib/vedeu/output/wordwrap.rb', line 124 def prune_string(string) string.chomp.slice(0..pruned_width) end |
#pruned_width ⇒ Fixnum (private)
129 130 131 |
# File 'lib/vedeu/output/wordwrap.rb', line 129 def pruned_width width - ellipsis.size end |
#split_lines ⇒ Array<String> (private)
106 107 108 |
# File 'lib/vedeu/output/wordwrap.rb', line 106 def split_lines text.split(/\n/) end |
#to_line_objects(text_as_lines) ⇒ Vedeu::Lines (private)
94 95 96 97 98 99 100 101 102 103 |
# File 'lib/vedeu/output/wordwrap.rb', line 94 def to_line_objects(text_as_lines) line_objects = Array(text_as_lines).map do |text_line| stream = Vedeu::Stream.new(value: text_line) line = Vedeu::Line.new stream.parent = line line.add(stream) line end Vedeu::Lines.new(line_objects) end |
#width ⇒ Fixnum (private)
144 145 146 |
# File 'lib/vedeu/output/wordwrap.rb', line 144 def width .fetch(:width) end |
#wrap ⇒ String
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/vedeu/output/wordwrap.rb', line 54 def wrap processed = [] text.split(/\n/).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 |