Class: Formatter

Inherits:
Object show all
Defined in:
lib/puppet/application/describe.rb

Instance Method Summary collapse

Constructor Details

#initialize(width) ⇒ Formatter

Returns a new instance of Formatter.



6
7
8
# File 'lib/puppet/application/describe.rb', line 6

def initialize(width)
  @width = width
end

Instance Method Details

#header(txt, sep = "-") ⇒ Object



33
34
35
# File 'lib/puppet/application/describe.rb', line 33

def header(txt, sep = "-")
  "\n#{txt}\n" + sep * txt.size
end

#wrap(txt, opts) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/puppet/application/describe.rb', line 10

def wrap(txt, opts)
  return "" unless txt && !txt.empty?

  work = (opts[:scrub] ? scrub(txt) : txt)
  indent = opts[:indent] || 0
  textLen = @width - indent
  patt = Regexp.new("\\A(.{0,#{textLen}})[ \n]")
  prefix = " " * indent

  res = []

  while work.length > textLen
    if work =~ patt
      res << ::Regexp.last_match(1)
      work.slice!(0, ::Regexp.last_match(0).length)
    else
      res << work.slice!(0, textLen)
    end
  end
  res << work if work.length.nonzero?
  prefix + res.join("\n#{prefix}")
end