class Formatter
Public Class Methods
new(width)
click to toggle source
# File lib/puppet/application/describe.rb 6 def initialize(width) 7 @width = width 8 end
Public Instance Methods
header(txt, sep = "-")
click to toggle source
# File lib/puppet/application/describe.rb 32 def header(txt, sep = "-") 33 "\n#{txt}\n" + sep * txt.size 34 end
wrap(txt, opts)
click to toggle source
# File lib/puppet/application/describe.rb 10 def wrap(txt, opts) 11 return "" unless txt && !txt.empty? 12 work = (opts[:scrub] ? scrub(txt) : txt) 13 indent = (opts[:indent] ? opts[:indent] : 0) 14 textLen = @width - indent 15 patt = Regexp.new("\\A(.{0,#{textLen}})[ \n]") 16 prefix = " " * indent 17 18 res = [] 19 20 while work.length > textLen 21 if work =~ patt 22 res << $1 23 work.slice!(0, $MATCH.length) 24 else 25 res << work.slice!(0, textLen) 26 end 27 end 28 res << work if work.length.nonzero? 29 prefix + res.join("\n#{prefix}") 30 end
Private Instance Methods
scrub(text)
click to toggle source
# File lib/puppet/application/describe.rb 38 def scrub(text) 39 # For text with no carriage returns, there's nothing to do. 40 return text if text !~ /\n/ 41 42 # If we can match an indentation, then just remove that same level of 43 # indent from every line. 44 if text =~ /^(\s+)/ 45 indent = $1 46 return text.gsub(/^#{indent}/,'') 47 else 48 return text 49 end 50 end