class Troo::Wordwrap

Attributes

options[R]
value[R]

Public Class Methods

new(value, options = {}) click to toggle source
# File lib/troo/presentation/formatter.rb, line 127
def initialize(value, options = {})
  @value, @options = value, options
end
this(value, options = {}) click to toggle source
# File lib/troo/presentation/formatter.rb, line 122
def this(value, options = {})
  new(value, options).reformat
end

Public Instance Methods

reformat() click to toggle source
# File lib/troo/presentation/formatter.rb, line 131
def reformat
  return pruned if prune?
  wordwrapped
end
wordwrapped() click to toggle source
# 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

Private Instance Methods

defaults() click to toggle source
# File lib/troo/presentation/formatter.rb, line 195
def defaults
  {
    width: 70,
    prune: false
  }
end
maximum_width() click to toggle source
# File lib/troo/presentation/formatter.rb, line 187
def maximum_width
  options.fetch(:width)
end
output(paragraph) click to toggle source
# File lib/troo/presentation/formatter.rb, line 164
def output(paragraph)
  paragraph.reduce([]) do |output, line|
    output << line.join(' ')
  end.join("\n")
end
prune?() click to toggle source
# File lib/troo/presentation/formatter.rb, line 183
def prune?
  options.fetch(:prune)
end
pruned() click to toggle source
# File lib/troo/presentation/formatter.rb, line 170
def pruned
  return value if value.size <= pruned_width
  [
    value.chomp.slice(0..pruned_width),
    '...',
    Esc.reset
  ].join
end
pruned_width() click to toggle source
# File lib/troo/presentation/formatter.rb, line 179
def pruned_width
  maximum_width - 3
end