Class: Shellout::Paragraph

Inherits:
Object
  • Object
show all
Defined in:
lib/shellout/paragraph.rb

Instance Method Summary collapse

Constructor Details

#initialize(str) ⇒ Paragraph

Returns a new instance of Paragraph.



4
5
6
7
8
# File 'lib/shellout/paragraph.rb', line 4

def initialize(str)
  @str = str
  @padding = 0
  @width   = 80
end

Instance Method Details

#padding(width) ⇒ Object



15
16
17
18
# File 'lib/shellout/paragraph.rb', line 15

def padding(width)
  @padding = width
  self
end


20
21
22
23
24
25
26
27
28
# File 'lib/shellout/paragraph.rb', line 20

def print(out=$stdout)
  text_width = @width - @padding
  text = ' '*@padding + @str
  # Stolen from ActionView::Helpers::TextHelper#word_wrap
  p = text.split("\n").collect do |line|
    line.length > text_width ? line.gsub(/(.{1,#{text_width}})(\s+|$)/, "\\1\n" + ' '*@padding).rstrip : line
  end * "\n"
  out.puts p
end

#width(width) ⇒ Object



10
11
12
13
# File 'lib/shellout/paragraph.rb', line 10

def width(width)
  @width = width
  self
end