class Bosh::Cli::LineWrap
Attributes
left_margin[R]
width[R]
Public Class Methods
new(width, left_margin = 0)
click to toggle source
# File lib/cli/line_wrap.rb, line 3 def initialize(width, left_margin = 0) @width = width @left_margin = left_margin end
Public Instance Methods
wrap(string)
click to toggle source
# File lib/cli/line_wrap.rb, line 8 def wrap(string) paragraphs = string.split("\n") wrapped_paragraphs = paragraphs.map do |paragraph| lines = wrapped_lines(paragraph) lines = indent_lines(lines) paragraph_indentation(paragraph) + lines.join("\n") end wrapped_paragraphs.join("\n") end
Private Instance Methods
indent_lines(lines)
click to toggle source
# File lib/cli/line_wrap.rb, line 50 def indent_lines(lines) lines.map { |line| (' ' * left_margin) + line } end
new_line_needed?(buffer, word)
click to toggle source
# File lib/cli/line_wrap.rb, line 46 def new_line_needed?(buffer, word) buffer.size + word.size > width end
paragraph_indentation(paragraph)
click to toggle source
# File lib/cli/line_wrap.rb, line 26 def paragraph_indentation(paragraph) paragraph.start_with?(' ') ? ' ' : '' end
wrapped_lines(string)
click to toggle source
# File lib/cli/line_wrap.rb, line 30 def wrapped_lines(string) result = [] buffer = '' string.split(' ').each do |word| if new_line_needed?(buffer, word) result << buffer buffer = word else buffer << ' ' unless buffer.empty? buffer << word end end result << buffer end