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