Class: Formatter
Instance Method Summary collapse
- #header(txt, sep = "-") ⇒ Object
-
#initialize(width) ⇒ Formatter
constructor
A new instance of Formatter.
- #wrap(txt, opts) ⇒ Object
Constructor Details
#initialize(width) ⇒ Formatter
Returns a new instance of Formatter.
5 6 7 |
# File 'lib/puppet/application/describe.rb', line 5 def initialize(width) @width = width end |
Instance Method Details
#header(txt, sep = "-") ⇒ Object
31 32 33 |
# File 'lib/puppet/application/describe.rb', line 31 def header(txt, sep = "-") "\n#{txt}\n" + sep * txt.size end |
#wrap(txt, opts) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/puppet/application/describe.rb', line 9 def wrap(txt, opts) return "" unless txt && !txt.empty? work = (opts[:scrub] ? scrub(txt) : txt) indent = (opts[: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 << $1 work.slice!(0, $MATCH.length) else res << work.slice!(0, textLen) end end res << work if work.length.nonzero? prefix + res.join("\n#{prefix}") end |