Class: Troo::Wordwrap

Inherits:
Object
  • Object
show all
Defined in:
lib/troo/presentation/formatter.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value, options = {}) ⇒ Wordwrap

Returns a new instance of Wordwrap.



127
128
129
# File 'lib/troo/presentation/formatter.rb', line 127

def initialize(value, options = {})
  @value, @options = value, options
end

Class Method Details

.this(value, options = {}) ⇒ Object



122
123
124
# File 'lib/troo/presentation/formatter.rb', line 122

def this(value, options = {})
  new(value, options).reformat
end

Instance Method Details

#reformatObject



131
132
133
134
# File 'lib/troo/presentation/formatter.rb', line 131

def reformat
  return pruned if prune?
  wordwrapped
end

#wordwrappedObject



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/troo/presentation/formatter.rb', line 136

def wordwrapped
  processed = []
  value.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) >= maximum_width
        line_length = word_length
        processed   << reformatted
        reformatted = []
      end

      reformatted << word
    end

    processed << reformatted
  end

  output(processed)
end